Giter Site home page Giter Site logo

hnlq715 / graceful Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kuangchanglang/graceful

0.0 3.0 0.0 37 KB

graceful reload golang http server, zero downtime, compatible with systemd, supervisor

License: MIT License

Go 100.00%
golang gracefule reload http grpc supervisor systemd

graceful's Introduction

graceful

Inspired by overseer and endless, with minimum codes and handy api to make http and grpc server graceful.

Prerequisite

golang 1.8+

Feature

  • Graceful reload http servers, zero downtime on upgrade.
  • Compatible with systemd, supervisor, etc.
  • Drop-in placement for http.ListenAndServe

Example

    type handler struct {
    }

    func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, port: %v, %q", r.Host, html.EscapeString(r.URL.Path))
    }

    func main(){
        graceful.ListenAndServe(":9222", &handler{})
    }

multi servers:

func listenMultiAddrs() {
	gs := grpc.NewService()
	pb.RegisterGreeterServer(gs.Server(), &server{})
	reflection.Register(gs.Server())

	hs := graceful.NewService()
	hs.Server().Handler = &handler{}

	server := graceful.NewServer()
	server.Register("0.0.0.0:9224", gs)
	server.Register("0.0.0.0:9225", hs)

	err := server.Run()
	fmt.Printf("error: %v\n", err)
}
grpcurl -plaintext -d '{"name":"sophos"}'  192.168.33.10:9224 helloworld.Greeter/SayHello

More example checkout example folder.

Reload

SIGHUP and SIGUSR1 on master proccess are used as default to reload server. server.Reload() func works as well from your code.

Drawbacks

graceful starts a master process to keep pid unchaged for process managers(systemd, supervisor, etc.), and a worker proccess listen to actual addrs. That means graceful starts one more process. Fortunately, master proccess waits for signals and reload worker when neccessary, which is costless since reload is usually low-frequency action.

Default values

  • StopTimeout. Unfinished old connections will be drop in {StopTimeout} seconds, default 20s, after new server is up.
	server := graceful.NewServer(graceful.WithStopTimeout(time.Duration(4 * time.Hour)))
	server.Register(addr, handler)
	if err := server.Run(); err != nil {
		log.Fatal(err)
	}
  • Signals. Default reload signals: syscall.SIGHUP, syscall.SIGUSR1 and stop signals: syscall.SIGKILL, syscall.SIGTERM, syscall.SIGINT could be overwrited with:
	server := graceful.NewServer(graceful.WithStopSignals([]syscall.Signal{syscall.SIGKILL}), graceful.WithReloadSignals([]syscall.Signal{syscall.SIGHUP}))
	server.Register(addr, handler)
	if err := server.Run(); err != nil {
		log.Fatal(err)
	}

TODO

  • ListenAndServeTLS
  • Add alternative api: Run in single process without master-worker

graceful's People

Contributors

kuangchanglang avatar sysulq avatar

Watchers

 avatar  avatar  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.