Shutdownserver error

This commit is contained in:
adman
2026-04-08 00:31:05 +02:00
parent 891551f9dd
commit fe4a17dce2
+9 -1
View File
@@ -58,12 +58,15 @@ func StartServerWithFactory(listenAddr string, factory Factory) (err error) {
} }
} }
var ShutDownServerErr = errors.New("server shutting down")
func StartServerWithOnConn(listenAddr string, onConn func(c *proto.Conn) (err error)) (err error) { func StartServerWithOnConn(listenAddr string, onConn func(c *proto.Conn) (err error)) (err error) {
l, err := net.Listen("tcp", listenAddr) l, err := net.Listen("tcp", listenAddr)
if err != nil { if err != nil {
return return
} }
for { running := true
for running {
var cNet net.Conn var cNet net.Conn
cNet, err = l.Accept() cNet, err = l.Accept()
if err != nil { if err != nil {
@@ -83,6 +86,11 @@ func StartServerWithOnConn(listenAddr string, onConn func(c *proto.Conn) (err er
slog.Error("Error while kicking client", "error", connErr, "client", c.Conn.RemoteAddr()) slog.Error("Error while kicking client", "error", connErr, "client", c.Conn.RemoteAddr())
} }
} }
if errors.Is(connErr, ShutDownServerErr) {
running = false
}
}() }()
} }
err = ShutDownServerErr
return
} }