Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling for docker swarm mode #1533

Merged
merged 1 commit into from
Jun 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions provider/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,18 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
if p.Watch {
ctx, cancel := context.WithCancel(ctx)
if p.SwarmMode {
errChan := make(chan error)
// TODO: This need to be change. Linked to Swarm events docker/docker#23827
ticker := time.NewTicker(SwarmDefaultWatchTime)
pool.Go(func(stop chan bool) {
defer close(errChan)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you put the defer here?
I think you must move this line under the line 156.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The receiver at line 185 blocks the execution until the channel is closed or received an error.

Copy link
Member

@emilevauge emilevauge Jun 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @tanyadegurechaff on this. But it would be better to use this syntax instead:

if err, ok := <-errChan; ok {
  return err
} else {
  // channel closed
  ...
}

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but I got an error from golint:

provider/docker/docker.go:187:13: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)

Copy link
Contributor

@ldez ldez Jun 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just do:

if err, ok := <-errChan; ok {
  return err
}
// channel closed
// ...

for {
select {
case <-ticker.C:
services, err := p.listServices(ctx, dockerClient)
if err != nil {
log.Errorf("Failed to list services for docker, error %s", err)
errChan <- err
return
}
configuration := p.loadDockerConfig(services)
Expand All @@ -179,6 +182,10 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
}
}
})
if err, ok := <-errChan; ok {
return err
}
// channel closed

} else {
pool.Go(func(stop chan bool) {
Expand Down