| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/facebookgo/httpdown" |
| 11 | ) |
| 12 | |
| 13 | func handler(w http.ResponseWriter, r *http.Request) { |
| 14 | duration, err := time.ParseDuration(r.FormValue("duration")) |
| 15 | if err != nil { |
| 16 | http.Error(w, err.Error(), 400) |
| 17 | return |
| 18 | } |
| 19 | fmt.Fprintf(w, "going to sleep %s with pid %d\n", duration, os.Getpid()) |
| 20 | w.(http.Flusher).Flush() |
| 21 | time.Sleep(duration) |
| 22 | fmt.Fprintf(w, "slept %s with pid %d\n", duration, os.Getpid()) |
| 23 | } |
| 24 | |
| 25 | func main() { |
| 26 | server := &http.Server{ |
| 27 | Addr: "127.0.0.1:8080", |
| 28 | Handler: http.HandlerFunc(handler), |
| 29 | } |
| 30 | hd := &httpdown.HTTP{ |
| 31 | StopTimeout: 10 * time.Second, |
| 32 | KillTimeout: 1 * time.Second, |
| 33 | } |
| 34 | |
| 35 | flag.StringVar(&server.Addr, "addr", server.Addr, "http address") |
| 36 | flag.DurationVar(&hd.StopTimeout, "stop-timeout", hd.StopTimeout, "stop timeout") |
| 37 | flag.DurationVar(&hd.KillTimeout, "kill-timeout", hd.KillTimeout, "kill timeout") |
| 38 | flag.Parse() |
| 39 | |
| 40 | if err := httpdown.ListenAndServe(server, hd); err != nil { |
| 41 | panic(err) |
| 42 | } |
| 43 | } |