Giter Site home page Giter Site logo

graceful's Introduction

graceful

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

Prerequisite

  • golang 1.8+
  • linux/darwin(windows not supported)

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 main(){
        server := graceful.NewServer()
        server.Register("0.0.0.0:9223", &handler{})
        server.Register("0.0.0.0:9224", &handler{})
        server.Register("0.0.0.0:9225", &handler{})
        err := server.Run()
        fmt.Printf("error: %v\n", err)
    }

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

graceful's Issues

WithStopSignals has bug?

It seems WithStopSignals just adds a new stop signal, other existed stop signals are not canceled.

graceful.WithStopSignals([]syscall.Signal{syscall.SIGKILL})
after setting stop signal to SIGKILL, SIGTERM and SIGINT can still stop the server.

I add this to avoid this problem
signal.Ignore(syscall.SIGTERM, syscall.SIGINT)

Way to check if process is the master (parent) or worker (child)

We've been using this library in prod and it's working great. One thing I ended up hacking a bit in our usage was making some initialization only take place in the child worker processes and not the master parent process. Specifically we were getting duplicate initialization logs and startup metrics.

In production our service runs with systemd so we just check if the parent process id == 1 to determine if we are the master. This doesn't work when running as user processes in development.

I'd like to be able to call a package function to say whether it's the master or worker before calling server.Run.

Old connections drop while in use.

It seems that there's an undocumented 20 second grace limit for completing old requests.

It would be great to let folks know that this behavior is present and how to override it. Perhaps toward the end of the README.

e.g.
In place of

	if err := graceful.ListenAndServe(addr, handler); err != nil {
		log.Fatal(err)
	}

do

	server := graceful.NewServer(graceful.WithStopTimeout(time.Duration(4 * time.Hour)))
	server.Register(addr, handler)
	if err := server.Run(); err != nil {
		log.Fatal(err)
	}

daemon 有两个线程

代码:
var tick1Interval=3
var cronGenerateTaskTick=time.NewTimer(time.Duration(tick1Interval)*time.Second)
func main() {
mux:=http.NewServeMux()
mux.HandleFunc("/write",HTTPSWrite)
go ss()
graceful.ListenAndServe(fmt.Sprintf(":9090"), mux)
}

func ss() {
for {
select {
case <-cronGenerateTaskTick.C:
fmt.Printf("%s:%d\n",time.Now().Format("15:04:05"),tick1Interval)
cronGenerateTaskTick.Reset(0*time.Second)
cronGenerateTaskTick=time.NewTimer(time.Duration(tick1Interval)time.Second)
time.Sleep(1
time.Second)
}
}
}

输出结果:
12:31:30:3
12:31:30:3
12:31:33:3
12:31:33:3
12:31:36:3

每次都有两个,如果使用http.ListenAndServe("0.0.0.0:9090",mux) 就不会有这个问题?
请教一下这个是怎么回事?

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.