add g.Listen for automatic signal handling feature

This commit is contained in:
John Guo
2021-01-22 12:57:21 +08:00
parent da0669c739
commit f1ed9b31b6
2 changed files with 10 additions and 3 deletions

View File

@ -19,11 +19,16 @@ func NewVar(i interface{}, safe ...bool) *Var {
return gvar.New(i, safe...)
}
// Wait blocks until:
// 1. All the web servers shutdown, it does nothing if there's no running web server.
// 2. Shutdown signals received and all registered shutdown handlers done.
// Wait is an alias of ghttp.Wait, which blocks until all the web servers shutdown.
// It's commonly used in multiple servers situation.
func Wait() {
ghttp.Wait()
}
// Listen is an alias of gproc.Listen, which handles the signals received and automatically
// calls registered signal handler functions.
// It blocks until shutdown signals received and all registered shutdown handlers done.
func Listen() {
gproc.Listen()
}

View File

@ -7,6 +7,7 @@
package gproc
import (
"github.com/gogf/gf/internal/intlog"
"os"
"os/signal"
"sync"
@ -64,6 +65,7 @@ func Listen() {
for {
wg := sync.WaitGroup{}
sig = <-sigChan
intlog.Printf(`signal received: %s`, sig.String())
if handlers, ok := signalHandlerMap[sig]; ok {
for _, handler := range handlers {
wg.Add(1)