Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions init.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -23,19 +23,27 @@ 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() {
flag.StringVar(&listenPort, "p", "", "listener port")
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
Expand Down Expand Up @@ -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)
Expand All @@ -92,19 +110,29 @@ 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,
},
}

log.Println("starting serve on ", hport)
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

Expand Down
10 changes: 5 additions & 5 deletions init_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}