From c36e0b3b06b9c8fd9a182a2a6f19a4fa4527de25 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 19 Jul 2017 14:11:45 +0200 Subject: [PATCH] refactor: add Safe everywhere is needing. --- cluster/datastore.go | 4 ++-- provider/dynamodb/dynamodb.go | 4 ++-- provider/ecs/ecs.go | 4 ++-- provider/eureka/eureka.go | 4 ++-- provider/file/file_test.go | 4 ++-- provider/kubernetes/client.go | 21 +++++++++++++++------ provider/rancher/metadata.go | 4 +++- server/web.go | 4 ++-- 8 files changed, 30 insertions(+), 19 deletions(-) diff --git a/cluster/datastore.go b/cluster/datastore.go index 96e2c9a2e..ee8f56d9d 100644 --- a/cluster/datastore.go +++ b/cluster/datastore.go @@ -80,7 +80,7 @@ func (d *Datastore) watchChanges() error { if err != nil { return err } - go func() { + safe.Go(func() { ctx, cancel := context.WithCancel(d.ctx) operation := func() error { for { @@ -113,7 +113,7 @@ func (d *Datastore) watchChanges() error { if err != nil { log.Errorf("Error in watch datastore: %v", err) } - }() + }) return nil } diff --git a/provider/dynamodb/dynamodb.go b/provider/dynamodb/dynamodb.go index 6dd9a297e..916f5ab67 100644 --- a/provider/dynamodb/dynamodb.go +++ b/provider/dynamodb/dynamodb.go @@ -155,12 +155,12 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s pool.Go(func(stop chan bool) { ctx, cancel := context.WithCancel(context.Background()) - go func() { + safe.Go(func() { select { case <-stop: cancel() } - }() + }) operation := func() error { aws, err := p.createClient() diff --git a/provider/ecs/ecs.go b/provider/ecs/ecs.go index 5ee72f8ff..836d1057a 100644 --- a/provider/ecs/ecs.go +++ b/provider/ecs/ecs.go @@ -112,12 +112,12 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s pool.Go(func(stop chan bool) { ctx, cancel := context.WithCancel(context.Background()) - go func() { + safe.Go(func() { select { case <-stop: cancel() } - }() + }) operation := func() error { aws, err := p.createClient() diff --git a/provider/eureka/eureka.go b/provider/eureka/eureka.go index 54da7b35e..8e9b6b881 100644 --- a/provider/eureka/eureka.go +++ b/provider/eureka/eureka.go @@ -52,7 +52,7 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s } ticker := time.NewTicker(delay) - go func() { + safe.Go(func() { for t := range ticker.C { log.Debug("Refreshing Provider " + t.String()) @@ -68,7 +68,7 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s Configuration: configuration, } } - }() + }) return nil } diff --git a/provider/file/file_test.go b/provider/file/file_test.go index 33bde6724..7b829eb26 100644 --- a/provider/file/file_test.go +++ b/provider/file/file_test.go @@ -148,7 +148,7 @@ func createConfigurationRoutine(t *testing.T, expectedNumFrontends *int, expecte configurationChan := make(chan types.ConfigMessage) signal := make(chan interface{}) - go func() { + safe.Go(func() { for { data := <-configurationChan assert.Equal(t, "file", data.ProviderName) @@ -156,7 +156,7 @@ func createConfigurationRoutine(t *testing.T, expectedNumFrontends *int, expecte assert.Len(t, data.Configuration.Backends, *expectedNumBackends) signal <- nil } - }() + }) return configurationChan, signal } diff --git a/provider/kubernetes/client.go b/provider/kubernetes/client.go index 971efe033..45be10f20 100644 --- a/provider/kubernetes/client.go +++ b/provider/kubernetes/client.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "time" + "github.com/containous/traefik/safe" "k8s.io/client-go/kubernetes" "k8s.io/client-go/pkg/api" "k8s.io/client-go/pkg/api/v1" @@ -125,7 +126,9 @@ func (c *clientImpl) WatchIngresses(labelSelector labels.Selector, watchCh chan< &v1beta1.Ingress{}, resyncPeriod, newResourceEventHandlerFuncs(watchCh)) - go c.ingController.Run(stopCh) + safe.Go(func() { + c.ingController.Run(stopCh) + }) } // eventHandlerFunc will pass the obj on to the events channel or drop it @@ -180,7 +183,9 @@ func (c *clientImpl) WatchServices(watchCh chan<- interface{}, stopCh <-chan str &v1.Service{}, resyncPeriod, newResourceEventHandlerFuncs(watchCh)) - go c.svcController.Run(stopCh) + safe.Go(func() { + c.svcController.Run(stopCh) + }) } // GetEndpoints returns the named Endpoints @@ -209,7 +214,9 @@ func (c *clientImpl) WatchEndpoints(watchCh chan<- interface{}, stopCh <-chan st &v1.Endpoints{}, resyncPeriod, newResourceEventHandlerFuncs(watchCh)) - go c.epController.Run(stopCh) + safe.Go(func() { + c.epController.Run(stopCh) + }) } func (c *clientImpl) WatchSecrets(watchCh chan<- interface{}, stopCh <-chan struct{}) { @@ -224,7 +231,9 @@ func (c *clientImpl) WatchSecrets(watchCh chan<- interface{}, stopCh <-chan stru &v1.Secret{}, resyncPeriod, newResourceEventHandlerFuncs(watchCh)) - go c.secController.Run(stopCh) + safe.Go(func() { + c.secController.Run(stopCh) + }) } // WatchAll returns events in the cluster and updates the stores via informer @@ -243,7 +252,7 @@ func (c *clientImpl) WatchAll(labelSelector string, stopCh <-chan struct{}) (<-c c.WatchEndpoints(eventCh, stopCh) c.WatchSecrets(eventCh, stopCh) - go func() { + safe.Go(func() { defer close(watchCh) defer close(eventCh) @@ -255,7 +264,7 @@ func (c *clientImpl) WatchAll(labelSelector string, stopCh <-chan struct{}) (<-c c.fireEvent(event, watchCh) } } - }() + }) return watchCh, nil } diff --git a/provider/rancher/metadata.go b/provider/rancher/metadata.go index 606d0321a..2a88abee0 100644 --- a/provider/rancher/metadata.go +++ b/provider/rancher/metadata.go @@ -112,7 +112,9 @@ func (p *Provider) longPoll(client rancher.Client, updateConfiguration func(stri // Holds the connection until there is either a change in the metadata // repository or `p.RefreshSeconds` has elapsed. Long polling should be // favoured for the most accurate configuration updates. - go client.OnChange(p.RefreshSeconds, updateConfiguration) + safe.Go(func() { + client.OnChange(p.RefreshSeconds, updateConfiguration) + }) <-stop } diff --git a/server/web.go b/server/web.go index 89c27db07..a55fe7dc8 100644 --- a/server/web.go +++ b/server/web.go @@ -134,7 +134,7 @@ func (provider *WebProvider) Provide(configurationChan chan<- types.ConfigMessag systemRouter.Methods("GET").Path(provider.Path + "debug/vars").HandlerFunc(expvarHandler) } - go func() { + safe.Go(func() { var err error var negroni = negroni.New() if provider.Auth != nil { @@ -155,7 +155,7 @@ func (provider *WebProvider) Provide(configurationChan chan<- types.ConfigMessag if err != nil { log.Fatal("Error creating server: ", err) } - }() + }) return nil }