remove do once block when creating a new docker client

only set cached connections upon no error
This commit is contained in:
Chelsea Holland Komlo 2018-04-05 11:47:56 -04:00
parent d0d793fc23
commit e8743f1f7b
1 changed files with 31 additions and 36 deletions

View File

@ -38,9 +38,6 @@ import (
)
var (
// We store the clients globally to cache the connection to the docker daemon.
createClients sync.Once
// client is a docker client with a timeout of 5 minutes. This is for doing
// all operations with the docker daemon besides which are not long running
// such as creating, killing containers, etc.
@ -1028,7 +1025,6 @@ func (d *DockerDriver) newDockerClient(timeout time.Duration) (*docker.Client, e
var merr multierror.Error
var newClient *docker.Client
createClients.Do(func() {
// Default to using whatever is configured in docker.endpoint. If this is
// not specified we'll fall back on NewClientFromEnv which reads config from
// the DOCKER_* environment variables DOCKER_HOST, DOCKER_TLS_VERIFY, and
@ -1053,22 +1049,17 @@ func (d *DockerDriver) newDockerClient(timeout time.Duration) (*docker.Client, e
merr.Errors = append(merr.Errors, err)
}
}
if timeout != 0 {
newClient.SetTimeout(timeout)
}
return
}
} else {
d.logger.Println("[DEBUG] driver.docker: using client connection initialized from environment")
newClient, err = docker.NewClientFromEnv()
if err != nil {
merr.Errors = append(merr.Errors, err)
}
}
if timeout != 0 {
newClient.SetTimeout(timeout)
}
})
return newClient, merr.ErrorOrNil()
}
@ -1083,14 +1074,18 @@ func (d *DockerDriver) dockerClients() (*docker.Client, *docker.Client, error) {
var merr multierror.Error
client, err := d.newDockerClient(dockerTimeout)
newClient, err := d.newDockerClient(dockerTimeout)
if err != nil {
merr.Errors = append(merr.Errors, err)
} else {
client = newClient
}
waitClient, err := d.newDockerClient(0 * time.Minute)
newWaitClient, err := d.newDockerClient(0 * time.Minute)
if err != nil {
merr.Errors = append(merr.Errors, err)
} else {
waitClient = newWaitClient
}
return client, waitClient, merr.ErrorOrNil()