Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sword-jin/go-graceful

a golang package for restart your application graceful, support with supervisor, systemd
https://github.com/sword-jin/go-graceful

fasthttp gin golang golang-package graceful http iris zero-time

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

# go-graceful

[![Go Report Card](https://goreportcard.com/badge/github.com/rrylee/go-graceful)](https://goreportcard.com/report/github.com/rrylee/go-graceful)

Inspired by [graceful](https://github.com/kuangchanglang/graceful) and [overseer](https://github.com/jpillora/overseer), for support some framework.

\>= go1.8

### Feature

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

![](./image/process.png)

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

### Example

std http server
```go
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`
```bash
$ Hello world, pid = 13435, ppid=13434

$ kill -USR1 13434

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

use iris

```go
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.