1 Commits

Author SHA1 Message Date
James Mills
9533f2445d Fixed graceful shutdown 2017-11-26 14:57:11 -08:00

View File

@@ -44,13 +44,16 @@ type Server struct {
accounts PasswordStore accounts PasswordStore
password []byte password []byte
signals chan os.Signal signals chan os.Signal
done chan bool
whoWas *WhoWasList whoWas *WhoWasList
ids map[string]*Identity ids map[string]*Identity
} }
var ( var (
SERVER_SIGNALS = []os.Signal{syscall.SIGINT, syscall.SIGHUP, SERVER_SIGNALS = []os.Signal{
syscall.SIGTERM, syscall.SIGQUIT} syscall.SIGINT, syscall.SIGHUP,
syscall.SIGTERM, syscall.SIGQUIT,
}
) )
func NewServer(config *Config) *Server { func NewServer(config *Config) *Server {
@@ -70,6 +73,7 @@ func NewServer(config *Config) *Server {
operators: config.Operators(), operators: config.Operators(),
accounts: NewMemoryPasswordStore(config.Accounts(), PasswordStoreOpts{}), accounts: NewMemoryPasswordStore(config.Accounts(), PasswordStoreOpts{}),
signals: make(chan os.Signal, len(SERVER_SIGNALS)), signals: make(chan os.Signal, len(SERVER_SIGNALS)),
done: make(chan bool),
whoWas: NewWhoWasList(100), whoWas: NewWhoWasList(100),
ids: make(map[string]*Identity), ids: make(map[string]*Identity),
} }
@@ -192,12 +196,17 @@ func (server *Server) Shutdown() {
} }
func (server *Server) Run() { func (server *Server) Run() {
done := false for {
for !done {
select { select {
case <-server.done:
return
case <-server.signals: case <-server.signals:
server.Shutdown() server.Shutdown()
done = true // Give at least 1s for clients to see the shutdown
go func() {
time.Sleep(1 * time.Second)
server.done <- true
}()
case conn := <-server.newConns: case conn := <-server.newConns:
go NewClient(server, conn) go NewClient(server, conn)