From 56ef678c097a504bb39a2643d6c6c7132d6214db Mon Sep 17 00:00:00 2001 From: Emile Vauge Date: Thu, 16 Jun 2016 22:49:57 +0200 Subject: [PATCH] Fix memory leak in listenProviders Signed-off-by: Emile Vauge --- provider/docker.go | 18 +++++++++--------- server.go | 18 +++++++----------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/provider/docker.go b/provider/docker.go index d2014b6df..01ed10416 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -107,6 +107,15 @@ func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, po } if provider.Watch { ctx, cancel := context.WithCancel(ctx) + pool.Go(func(stop chan bool) { + for { + select { + case <-stop: + cancel() + return + } + } + }) f := filters.NewArgs() f.Add("type", "container") options := dockertypes.EventsOptions{ @@ -134,15 +143,6 @@ func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, po eventHandler.Handle("die", startStopHandle) errChan := events.MonitorWithHandler(ctx, dockerClient, options, eventHandler) - pool.Go(func(stop chan bool) { - for { - select { - case <-stop: - cancel() - return - } - } - }) if err := <-errChan; err != nil { return err } diff --git a/server.go b/server.go index ecac8c4db..a1411f00f 100644 --- a/server.go +++ b/server.go @@ -149,17 +149,13 @@ func (server *Server) listenProviders(stop chan bool) { server.configurationValidatedChan <- configMsg } else { log.Debugf("Last %s config received less than %s, waiting...", configMsg.ProviderName, server.globalConfiguration.ProvidersThrottleDuration) - server.routinesPool.Go(func(stop chan bool) { - select { - case <-stop: - return - case <-time.After(server.globalConfiguration.ProvidersThrottleDuration): - lastReceivedConfigurationValue := lastReceivedConfiguration.Get().(time.Time) - if time.Now().After(lastReceivedConfigurationValue.Add(time.Duration(server.globalConfiguration.ProvidersThrottleDuration))) { - log.Debugf("Waited for %s config, OK", configMsg.ProviderName) - if lastConfig, ok := lastConfigs.Get(configMsg.ProviderName); ok { - server.configurationValidatedChan <- *lastConfig.(*types.ConfigMessage) - } + safe.Go(func() { + <-time.After(server.globalConfiguration.ProvidersThrottleDuration) + lastReceivedConfigurationValue := lastReceivedConfiguration.Get().(time.Time) + if time.Now().After(lastReceivedConfigurationValue.Add(time.Duration(server.globalConfiguration.ProvidersThrottleDuration))) { + log.Debugf("Waited for %s config, OK", configMsg.ProviderName) + if lastConfig, ok := lastConfigs.Get(configMsg.ProviderName); ok { + server.configurationValidatedChan <- *lastConfig.(*types.ConfigMessage) } } })