Skip to content

Commit ece9adc

Browse files
committed
Add IdleCheckFrequency to manage idle connection checks and improve shutdown error handling
1 parent b96d69a commit ece9adc

2 files changed

Lines changed: 43 additions & 42 deletions

File tree

server.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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
357352
func (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 {

types.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,15 @@ type Command struct {
168168
}
169169

170170
type ServerConfig struct {
171-
Address string
172-
TLSConfig *tls.Config
173-
ReadTimeout time.Duration
174-
WriteTimeout time.Duration
175-
IdleTimeout time.Duration
176-
MaxConnections int
177-
Logger Logger
178-
ConnStateHook func(net.Conn, ConnState)
171+
Address string
172+
TLSConfig *tls.Config
173+
ReadTimeout time.Duration
174+
WriteTimeout time.Duration
175+
IdleTimeout time.Duration
176+
IdleCheckFrequency time.Duration
177+
MaxConnections int
178+
Logger Logger
179+
ConnStateHook func(net.Conn, ConnState)
179180
}
180181

181182
func DefaultServerConfig() *ServerConfig {
@@ -190,14 +191,15 @@ func DefaultServerConfig() *ServerConfig {
190191
}
191192

192193
type Server struct {
193-
Address string
194-
TLSConfig *tls.Config
195-
ReadTimeout time.Duration
196-
WriteTimeout time.Duration
197-
IdleTimeout time.Duration
198-
MaxConnections int
199-
Logger Logger
200-
ConnStateHook func(net.Conn, ConnState)
194+
Address string
195+
TLSConfig *tls.Config
196+
ReadTimeout time.Duration
197+
WriteTimeout time.Duration
198+
IdleTimeout time.Duration
199+
IdleCheckFrequency time.Duration
200+
MaxConnections int
201+
Logger Logger
202+
ConnStateHook func(net.Conn, ConnState)
201203

202204
handlers map[string]CommandHandler
203205
middlewareChain *MiddlewareChain

0 commit comments

Comments
 (0)