一个随便糊的用于模拟高延迟接口的go程序

来自三线的随记
package main

import (
   "fmt"
   "net/http"
   "time"
)

func HelloHandler(w http.ResponseWriter, r *http.Request) {
   fmt.Println(r)
   min := 5
   s := min * 60
   fmt.Println(r.RemoteAddr, ": going to sleep",s ,"s(", min, " min)")
   for i := 1;i <= min; i++ {
     fmt.Println(r.RemoteAddr, ": sleeping", i, "* 60s")
     time.Sleep(60 *time.Second)
   }
   fmt.Fprintf(w, "Hello World")
   fmt.Println(r.RemoteAddr, ": End of sleep",r)
}

func common(w http.ResponseWriter, r *http.Request) {
  fmt.Println(r)
  fmt.Fprintf(w, "common - Hello World")
}

func main () {
   fmt.Println("listening: 8000")
   http.HandleFunc("/", HelloHandler)
   http.HandleFunc("/sleeper-sanxian/common", common)
   http.ListenAndServe(":8000", nil)
}

食用方法:

go run sleep.go

ps: 也可以通过tc命令实现

例如

[root@test ~]# tc qdisc add dev calic1c981f0768 root netem delay 2980ms
[root@test ~]# tc qdisc add dev calide0993eb080 root netem delay 3010ms