@@ -29,19 +29,20 @@ func NewServerWithConfig(config *ServerConfig) *Server {
2929 }
3030
3131 server := & Server {
32- Address : config .Address ,
33- TLSConfig : config .TLSConfig ,
34- ReadTimeout : config .ReadTimeout ,
35- WriteTimeout : config .WriteTimeout ,
36- IdleTimeout : config .IdleTimeout ,
37- MaxConnections : config .MaxConnections ,
38- Logger : config .Logger ,
39- ConnStateHook : config .ConnStateHook ,
40- handlers : make (map [string ]CommandHandler ),
41- middlewareChain : NewMiddlewareChain (),
42- activeConns : make (map [* Connection ]struct {}),
43- ctx : ctx ,
44- cancel : cancel ,
32+ Address : config .Address ,
33+ TLSConfig : config .TLSConfig ,
34+ ReadTimeout : config .ReadTimeout ,
35+ WriteTimeout : config .WriteTimeout ,
36+ IdleTimeout : config .IdleTimeout ,
37+ IdleCheckFrequency : config .IdleCheckFrequency ,
38+ MaxConnections : config .MaxConnections ,
39+ Logger : config .Logger ,
40+ ConnStateHook : config .ConnStateHook ,
41+ handlers : make (map [string ]CommandHandler ),
42+ middlewareChain : NewMiddlewareChain (),
43+ activeConns : make (map [* Connection ]struct {}),
44+ ctx : ctx ,
45+ cancel : cancel ,
4546 }
4647
4748 server .registerDefaultHandlers ()
@@ -170,10 +171,11 @@ func (s *Server) Shutdown(ctx context.Context) error {
170171 }
171172 s .mu .RUnlock ()
172173
174+ var firstErr error
173175 for _ , conn := range conns {
174- err := conn .Close ()
175- if err != nil {
176- return err
176+ if err := conn .Close (); err != nil && firstErr == nil {
177+ firstErr = err
178+ s . Logger . Warn ( "Error closing connection during shutdown: %v" , err )
177179 }
178180 }
179181
@@ -195,7 +197,7 @@ func (s *Server) Shutdown(ctx context.Context) error {
195197 case <- ctx .Done ():
196198 return ctx .Err ()
197199 case <- done :
198- return nil
200+ return firstErr
199201 }
200202}
201203
@@ -215,7 +217,7 @@ func (s *Server) handleConnectionInternal(netConn net.Conn) {
215217 lastUsed : time .Now (),
216218 }
217219
218- conn .state . Store ( int32 ( StateNew ) )
220+ conn .setState ( StateNew )
219221
220222 s .mu .Lock ()
221223 s .activeConns [conn ] = struct {}{}
@@ -228,14 +230,7 @@ func (s *Server) handleConnectionInternal(netConn net.Conn) {
228230 s .mu .Unlock ()
229231 }()
230232
231- if s .ConnStateHook != nil {
232- s .ConnStateHook (netConn , StateNew )
233- }
234-
235233 conn .setState (StateActive )
236- if s .ConnStateHook != nil {
237- s .ConnStateHook (netConn , StateActive )
238- }
239234
240235 s .Logger .Debug ("New connection from %s" , netConn .RemoteAddr ())
241236
@@ -356,7 +351,11 @@ func (s *Server) TriggerIdleCheck() {
356351// startIdleChecker starts a background goroutine to check for idle connections
357352func (s * Server ) startIdleChecker () {
358353 go func () {
359- ticker := time .NewTicker (30 * time .Second )
354+ checkInterval := s .IdleCheckFrequency
355+ if checkInterval <= 0 {
356+ checkInterval = 30 * time .Second
357+ }
358+ ticker := time .NewTicker (checkInterval )
360359 defer ticker .Stop ()
361360
362361 for {
0 commit comments