server: close queued up connections on shutdown

Closes: https://todo.sr.ht/~emersion/soju/204
This commit is contained in:
Simon Ser 2022-09-18 21:37:45 +02:00
parent c5f6a41d6c
commit d354c73933

View File

@ -156,6 +156,7 @@ type Server struct {
lock sync.Mutex lock sync.Mutex
listeners map[net.Listener]struct{} listeners map[net.Listener]struct{}
users map[string]*user users map[string]*user
shutdown bool
metrics struct { metrics struct {
downstreams int64Gauge downstreams int64Gauge
@ -343,6 +344,7 @@ func (s *Server) sendWebPush(ctx context.Context, sub *webpush.Subscription, vap
func (s *Server) Shutdown() { func (s *Server) Shutdown() {
s.lock.Lock() s.lock.Lock()
s.shutdown = true
for ln := range s.listeners { for ln := range s.listeners {
if err := ln.Close(); err != nil { if err := ln.Close(); err != nil {
s.Logger.Printf("failed to stop listener: %v", err) s.Logger.Printf("failed to stop listener: %v", err)
@ -426,10 +428,19 @@ func (s *Server) handle(ic ircConn) {
} }
}() }()
s.lock.Lock()
shutdown := s.shutdown
s.lock.Unlock()
s.metrics.downstreams.Add(1) s.metrics.downstreams.Add(1)
id := atomic.AddUint64(&lastDownstreamID, 1) id := atomic.AddUint64(&lastDownstreamID, 1)
dc := newDownstreamConn(s, ic, id) dc := newDownstreamConn(s, ic, id)
if err := dc.runUntilRegistered(); err != nil { if shutdown {
dc.SendMessage(&irc.Message{
Command: "ERROR",
Params: []string{"Server is shutting down"},
})
} else if err := dc.runUntilRegistered(); err != nil {
if !errors.Is(err, io.EOF) { if !errors.Is(err, io.EOF) {
dc.logger.Printf("%v", err) dc.logger.Printf("%v", err)
} }