2012-06-05 06:21:10 +08:00
|
|
|
// Command gracedemo implements a demo server showing how to gracefully
|
2013-03-26 07:37:01 +08:00
|
|
|
// terminate an HTTP server using go.grace.
|
2012-06-05 06:21:10 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2012-07-08 05:38:18 +08:00
|
|
|
"github.com/daaku/go.grace/gracehttp"
|
2012-06-05 06:21:10 +08:00
|
|
|
"net/http"
|
2013-03-26 05:54:13 +08:00
|
|
|
"os"
|
2012-06-05 06:21:10 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
address0 = flag.String("a0", ":48567", "Zero address to bind to.")
|
|
|
|
address1 = flag.String("a1", ":48568", "First address to bind to.")
|
|
|
|
address2 = flag.String("a2", ":48569", "Second address to bind to.")
|
|
|
|
now = time.Now()
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
gracehttp.Serve(
|
|
|
|
gracehttp.Handler{*address0, newHandler("Zero ")},
|
|
|
|
gracehttp.Handler{*address1, newHandler("First ")},
|
|
|
|
gracehttp.Handler{*address2, newHandler("Second")},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHandler(name string) http.Handler {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/sleep/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
duration, err := time.ParseDuration(r.FormValue("duration"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 400)
|
2013-03-26 05:59:40 +08:00
|
|
|
return
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
|
|
|
time.Sleep(duration)
|
2013-03-26 05:51:46 +08:00
|
|
|
fmt.Fprintf(
|
|
|
|
w,
|
2013-03-26 05:54:13 +08:00
|
|
|
"%s started at %s slept for %d nanoseconds from pid %d.\n",
|
2012-06-05 06:21:10 +08:00
|
|
|
name,
|
|
|
|
now,
|
2013-03-26 05:51:46 +08:00
|
|
|
duration.Nanoseconds(),
|
2013-03-26 05:54:13 +08:00
|
|
|
os.Getpid(),
|
2013-03-26 05:51:46 +08:00
|
|
|
)
|
2012-06-05 06:21:10 +08:00
|
|
|
})
|
|
|
|
return mux
|
|
|
|
}
|