Skip to content

a golang package for restart your application graceful, support with supervisor, systemd

License

Notifications You must be signed in to change notification settings

sword-jin/go-graceful

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-graceful

Go Report Card

Inspired by graceful and overseer, for support some framework.

>= go1.8

Feature

  • multi service, port
  • worker with framework
  • support supervisor, systemd
  • connection limit

use master-worker model because supervisor should keep master pid.

Example

std http server

type myHandler struct{}
func (*myHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
	_, _ = w.Write([]byte(fmt.Sprintf("Hello world, pid = %d, ppid=%d", syscall.Getpid(), syscall.Getppid())))
}

func main() {
	mux := http.NewServeMux()
	mux.Handle("/", &myHandler{})

	srv := &http.Server{Handler: mux}

	grace := graceful.New()
	grace.RegisterService(graceful.NewAddress("127.0.0.1:8124", "tcp"), func(ln net.Listener) error {
		return srv.Serve(ln)
	}, func() error {
		return srv.Shutdown(context.Background())
	})

	grace.Run()
	err := grace.Run()
	if err != nil {
		log.Fatal(err)
	}
}

default reload signals syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGUSR2

so crul 127.0.0.1:8124

$ Hello world, pid = 13435, ppid=13434

$ kill -USR1 13434

$ curl 127.0.0.1:8124
Hello world, pid = 13479, ppid=13434

use iris

app := iris.New()

app.Get("/", func(c context2.Context) {
    _, _ = c.Writef("hello world! pid=%d, ppid=%d", syscall.Getpid(), syscall.Getppid())
})

grace := graceful.New()
grace.RegisterService(graceful.NewAddress("127.0.0.1:8124", "tcp"), func(ln net.Listener) error {
    return app.Run(iris.Listener(ln))
}, func() error {
    return app.Shutdown(context.Background())
})
err := grace.Run()
if err != nil {
    log.Fatal(err)
}

alse fasthttp example and other framework.