graceful

package module
v0.0.0-...-aca16ed Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2023 License: MIT Imports: 9 Imported by: 0

README

graceful

优雅启停服务

说明
  • 用于优雅的启停多个服务,对多服务应用非常有效
  • 以下只需要简单的封装即可实现多服务兼容
  • http.Server
  • gin
  • rpcx
  • gnet
  • cron
  • 更多
用法、示例
  • main.go
package main

import (
	"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
package main

import (
	"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
package main

import (
	"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
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRunning = errors.New(`graceful is running`)
	ErrStopped = errors.New(`graceful is stopped`)
)

Functions

func Add

func Add(name string, svr Service) error

Add 添加服务

func DefaultListen

func DefaultListen() os.Signal

func Run

func Run() error

Run 运行所有服务

func SetDefaultLog

func SetDefaultLog(infoLog, errLog *log.Logger, callDepth ...int)

func Stop

func Stop() error

Stop 停止所有服务

Types

type Graceful

type Graceful struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) *Graceful

New 创建服务维护对象

func (*Graceful) Add

func (g *Graceful) Add(name string, svr Service) error

Add 添加服务

func (*Graceful) Run

func (g *Graceful) Run() error

Run 运行所有服务

func (*Graceful) Stop

func (g *Graceful) Stop() error

Stop 停止所有服务

type Listen

type Listen func() os.Signal

type Option

type Option func(g *Graceful)

func WithListenFunc

func WithListenFunc(f Listen) Option

WithListenFunc 自定义信号监听方法

func WithListenLog

func WithListenLog(infoLog, errLog *log.Logger, callDepth ...int) Option

WithListenLog 自定义日志输出

type Service

type Service interface {
	Service() error
	Shutdown(ctx context.Context) error
}

Service 服务方法

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL