diff --git a/network/proxy.go b/network/proxy.go index 373186ec..45cca32e 100644 --- a/network/proxy.go +++ b/network/proxy.go @@ -40,6 +40,8 @@ type Proxy struct { var _ IProxy = &Proxy{} // NewProxy creates a new proxy. +// +//nolint:funlen func NewProxy( connPool pool.IPool, hookConfig *hook.Config, elastic, reuseElasticClients bool, @@ -71,7 +73,8 @@ func NewProxy( // Schedule the client health check. if _, err := proxy.scheduler.Every(proxy.HealthCheckPeriod).SingletonMode().StartAt(startDelay).Do( func() { - logger.Debug().Msg("Running the client health check, and might recycle connection(s).") + now := time.Now() + logger.Debug().Msg("Running the client health check to recycle connection(s).") proxy.availableConnections.ForEach(func(_, value interface{}) bool { if client, ok := value.(*Client); ok { // Connection is probably dead by now. @@ -81,10 +84,14 @@ func NewProxy( client = NewClient(proxy.ClientConfig, proxy.logger) if err := proxy.availableConnections.Put(client.ID, client); err != nil { proxy.logger.Err(err).Msg("Failed to update the client connection") + // Close the client, because we don't want to have orphaned connections. + client.Close() } } return true }) + logger.Debug().Str("duration", time.Since(now).String()).Msg( + "Finished the client health check") }, ); err != nil { proxy.logger.Error().Err(err).Msg("Failed to schedule the client health check")