Giter Site home page Giter Site logo

graceful's Introduction

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
}

graceful's People

Contributors

azeroth-sha avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.