Provider docker shutdown problem

This commit is contained in:
SALLEYRON Julien 2018-10-29 15:30:04 +01:00 committed by Traefiker Bot
parent 21c94141ba
commit 7eeecd23ac

View file

@ -121,18 +121,17 @@ func (p *Provider) createClient() (client.APIClient, error) {
// Provide allows the docker provider to provide configurations to traefik // Provide allows the docker provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool) error { func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool) error {
// TODO register this routine in pool, and watch for stop channel pool.GoCtx(func(routineCtx context.Context) {
safe.Go(func() {
operation := func() error { operation := func() error {
var err error var err error
ctx, cancel := context.WithCancel(routineCtx)
defer cancel()
dockerClient, err := p.createClient() dockerClient, err := p.createClient()
if err != nil { if err != nil {
log.Errorf("Failed to create a client for docker, error: %s", err) log.Errorf("Failed to create a client for docker, error: %s", err)
return err return err
} }
ctx := context.Background()
serverVersion, err := dockerClient.ServerVersion(ctx) serverVersion, err := dockerClient.ServerVersion(ctx)
if err != nil { if err != nil {
log.Errorf("Failed to retrieve information of the docker client and server host: %s", err) log.Errorf("Failed to retrieve information of the docker client and server host: %s", err)
@ -160,12 +159,11 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
Configuration: configuration, Configuration: configuration,
} }
if p.Watch { if p.Watch {
ctx, cancel := context.WithCancel(ctx)
if p.SwarmMode { if p.SwarmMode {
errChan := make(chan error) errChan := make(chan error)
// TODO: This need to be change. Linked to Swarm events docker/docker#23827 // TODO: This need to be change. Linked to Swarm events docker/docker#23827
ticker := time.NewTicker(SwarmDefaultWatchTime) ticker := time.NewTicker(SwarmDefaultWatchTime)
pool.Go(func(stop chan bool) { pool.GoCtx(func(ctx context.Context) {
defer close(errChan) defer close(errChan)
for { for {
select { select {
@ -184,9 +182,8 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
} }
} }
case <-stop: case <-ctx.Done():
ticker.Stop() ticker.Stop()
cancel()
return return
} }
} }
@ -197,10 +194,6 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
// channel closed // channel closed
} else { } else {
pool.Go(func(stop chan bool) {
<-stop
cancel()
})
f := filters.NewArgs() f := filters.NewArgs()
f.Add("type", "container") f.Add("type", "container")
options := dockertypes.EventsOptions{ options := dockertypes.EventsOptions{
@ -213,7 +206,6 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
if err != nil { if err != nil {
log.Errorf("Failed to list containers for docker, error %s", err) log.Errorf("Failed to list containers for docker, error %s", err)
// Call cancel to get out of the monitor // Call cancel to get out of the monitor
cancel()
return return
} }
configuration := p.buildConfiguration(containers) configuration := p.buildConfiguration(containers)
@ -238,8 +230,9 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
if err == io.EOF { if err == io.EOF {
log.Debug("Provider event stream closed") log.Debug("Provider event stream closed")
} }
return err return err
case <-ctx.Done():
return nil
} }
} }
} }
@ -249,7 +242,7 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
notify := func(err error, time time.Duration) { notify := func(err error, time time.Duration) {
log.Errorf("Provider connection error %+v, retrying in %s", err, time) log.Errorf("Provider connection error %+v, retrying in %s", err, time)
} }
err := backoff.RetryNotify(safe.OperationWithRecover(operation), job.NewBackOff(backoff.NewExponentialBackOff()), notify) err := backoff.RetryNotify(safe.OperationWithRecover(operation), backoff.WithContext(job.NewBackOff(backoff.NewExponentialBackOff()), routineCtx), notify)
if err != nil { if err != nil {
log.Errorf("Cannot connect to docker server %+v", err) log.Errorf("Cannot connect to docker server %+v", err)
} }