Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/azeroth-sha/graceful
优雅启停服务
https://github.com/azeroth-sha/graceful
cron gin gnet go golang graceful rpcx
Last synced: about 3 hours ago
JSON representation
优雅启停服务
- Host: GitHub
- URL: https://github.com/azeroth-sha/graceful
- Owner: azeroth-sha
- License: mit
- Created: 2023-05-23T13:35:23.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-05-24T14:07:44.000Z (over 1 year ago)
- Last Synced: 2024-11-16T15:38:11.555Z (about 21 hours ago)
- Topics: cron, gin, gnet, go, golang, graceful, rpcx
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# graceful
优雅启停服务
### 说明
- 用于优雅的启停多个服务,对多服务应用非常有效
- 以下只需要简单的封装即可实现多服务兼容
- http.Server
- [gin](https://github.com/gin-gonic/gin)
- [rpcx](https://github.com/smallnest/rpcx)
- [gnet](https://github.com/panjf2000/gnet)
- [cron](https://github.com/robfig/cron)
- 更多### 用法、示例
- main.go
```go
package mainimport (
"github.com/azeroth-sha/graceful"
"log"
)func init() {
_ = graceful.Add(`gin`, new(ginSvr))
_ = graceful.Add("cron", new(cronSvr))
}func main() {
if err := graceful.Run(); err != nil {
log.Print(err)
}
if err := graceful.Stop(); err != nil {
log.Print(err)
}
}
```- gin.go
```go
package mainimport (
"context"
"github.com/gin-gonic/gin"
"net/http"
"sync"
"sync/atomic"
)type ginSvr struct {
once sync.Once
running int32
r *gin.Engine
svr *http.Server
}func (g *ginSvr) init() {
g.r = gin.New()
g.svr = &http.Server{
Addr: ":8080",
Handler: g.r,
}
}func (g *ginSvr) Service() error {
if atomic.SwapInt32(&g.running, 1) != 0 {
return nil
}
g.once.Do(g.init)
return g.svr.ListenAndServe()
}func (g *ginSvr) Shutdown(ctx context.Context) error {
if atomic.SwapInt32(&g.running, 0) != 1 {
return nil
}
return g.svr.Shutdown(ctx)
}
```- cron.go
```go
package mainimport (
"context"
cron "github.com/robfig/cron/v3"
"sync"
"sync/atomic"
)type cronSvr struct {
running int32
once sync.Once
svr *cron.Cron
}func (e *cronSvr) init() {
e.svr = cron.New()
}func (e *cronSvr) Service() error {
if atomic.SwapInt32(&e.running, 1) != 0 {
return nil
}
e.once.Do(e.init)
e.svr.Run()
return nil
}func (e *cronSvr) Shutdown(ctx context.Context) error {
if atomic.SwapInt32(&e.running, 0) != 1 {
return nil
}
e.svr.Stop()
return nil
}
```