mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
docker: fix docker client data race on Close()
This commit is contained in:
parent
68929631f2
commit
34a3739545
1 changed files with 6 additions and 8 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/connhelper"
|
"github.com/docker/cli/cli/connhelper"
|
||||||
|
@ -81,10 +82,7 @@ func closeTimedOutClients() {
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
|
||||||
for _, c := range clientMap {
|
for _, c := range clientMap {
|
||||||
if c.closedOn == 0 {
|
if atomic.LoadUint32(&c.refCount) == 0 && now-atomic.LoadInt64(&c.closedOn) > clientTTLSecs {
|
||||||
continue
|
|
||||||
}
|
|
||||||
if c.refCount == 0 && now-c.closedOn > clientTTLSecs {
|
|
||||||
delete(clientMap, c.key)
|
delete(clientMap, c.key)
|
||||||
c.Client.Close()
|
c.Client.Close()
|
||||||
logging.Debug().Str("host", c.key).Msg("docker client closed")
|
logging.Debug().Str("host", c.key).Msg("docker client closed")
|
||||||
|
@ -107,8 +105,8 @@ func (c *SharedClient) CheckConnection(ctx context.Context) error {
|
||||||
|
|
||||||
// if the client is still referenced, this is no-op.
|
// if the client is still referenced, this is no-op.
|
||||||
func (c *SharedClient) Close() {
|
func (c *SharedClient) Close() {
|
||||||
c.closedOn = time.Now().Unix()
|
atomic.StoreInt64(&c.closedOn, time.Now().Unix())
|
||||||
c.refCount--
|
atomic.AddUint32(&c.refCount, ^uint32(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient creates a new Docker client connection to the specified host.
|
// NewClient creates a new Docker client connection to the specified host.
|
||||||
|
@ -126,8 +124,8 @@ func NewClient(host string) (*SharedClient, error) {
|
||||||
defer clientMapMu.Unlock()
|
defer clientMapMu.Unlock()
|
||||||
|
|
||||||
if client, ok := clientMap[host]; ok {
|
if client, ok := clientMap[host]; ok {
|
||||||
client.closedOn = 0
|
atomic.StoreInt64(&client.closedOn, 0)
|
||||||
client.refCount++
|
atomic.AddUint32(&client.refCount, 1)
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue