diff --git a/init.go b/init.go index b5b39d1..4ed27e8 100644 --- a/init.go +++ b/init.go @@ -1,4 +1,4 @@ -// grace provides for graceful restart for go http servers. +// Package grace provides for graceful restart for go http servers. // There are 2 parts to graceful restarts // 1. Share listening sockets (this is done via socketmaster binary) // 2. Close listener gracefully (via graceful) @@ -23,6 +23,13 @@ import ( var listenPort string var cfgtestFlag bool +// Config grace package config +type Config struct { + Timeout time.Duration + HTTPReadTimeout time.Duration + HTTPWriteTimeout time.Duration +} + // add -p flag to the list of flags supported by the app, // and allow it to over-ride default listener port in config/app func init() { @@ -30,12 +37,13 @@ func init() { flag.BoolVar(&cfgtestFlag, "t", false, "config test") } -// applications need some way to access the port +// GetListenPort applications need some way to access the port // TODO: this method will work only after grace.Serve is called. func GetListenPort(hport string) string { return listenPort } +// ServerFastHTTP use fasthttp server func ServerFastHTTP(hport string, handler fasthttp.RequestHandler) error { var l net.Listener var err error @@ -80,10 +88,20 @@ func ServerFastHTTP(hport string, handler fasthttp.RequestHandler) error { } -// start serving on hport. If running via socketmaster, the hport argument is +// Serve start serving on hport. If running via socketmaster, the hport argument is // ignored. Also, if a port was specified via -p, it takes precedence on hport func Serve(hport string, handler http.Handler) error { + config := Config{ + Timeout: 10 * time.Second, + HTTPReadTimeout: 5 * time.Second, + HTTPWriteTimeout: 10 * time.Second, + } + + return ServeWithConfig(hport, config, handler) +} +// ServeWithConfig serve using package config +func ServeWithConfig(hport string, config Config, handler http.Handler) error { checkConfigTest() l, err := Listen(hport) @@ -92,11 +110,11 @@ func Serve(hport string, handler http.Handler) error { } srv := &graceful.Server{ - Timeout: 10 * time.Second, + Timeout: config.Timeout, Server: &http.Server{ Handler: handler, - ReadTimeout: 5 * time.Second, - WriteTimeout: 10 * time.Second, + ReadTimeout: config.HTTPReadTimeout, + WriteTimeout: config.HTTPWriteTimeout, }, } @@ -104,7 +122,17 @@ func Serve(hport string, handler http.Handler) error { return srv.Serve(l) } -// This method can be used for any TCP Listener, e.g. non HTTP +// Run exports Run() from grace package +func Run(addr string, timeout time.Duration, n http.Handler) { + graceful.Run(addr, timeout, n) +} + +// RunWithErr exports RunWithErr from grace package +func RunWithErr(addr string, timeout time.Duration, n http.Handler) error { + return graceful.RunWithErr(addr, timeout, n) +} + +// Listen This method can be used for any TCP Listener, e.g. non HTTP func Listen(hport string) (net.Listener, error) { var l net.Listener diff --git a/init_test.go b/init_test.go index d978442..0f59cc6 100644 --- a/init_test.go +++ b/init_test.go @@ -1,15 +1,15 @@ package grace import ( - "net/http" - "log" + "log" + "net/http" ) func ExampleServe() { - http.HandleFunc("/foo/bar", foobarHandler) - log.Fatal(grace.Serve(":9000", nil)) + http.HandleFunc("/foo/bar", foobarHandler) + log.Fatal(Serve(":9000", nil)) } func foobarHandler(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("foobar")) + w.Write([]byte("foobar")) }