improved docker reconnect mechanism, removed redundant checkings, refactor

This commit is contained in:
yusing
2025-02-24 07:50:23 +08:00
parent 5f1b78ec84
commit bda547198e
9 changed files with 84 additions and 86 deletions

View File

@@ -1,8 +1,10 @@
package docker
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"sync"
"time"
@@ -23,11 +25,14 @@ type (
key string
refCount uint32
closedOn int64
addr string
dial func(ctx context.Context) (net.Conn, error)
}
)
var (
clientMap = make(map[string]*SharedClient, 5)
clientMap = make(map[string]*SharedClient, 10)
clientMapMu sync.RWMutex
clientOptEnvHost = []client.Opt{
@@ -64,9 +69,7 @@ func init() {
for _, c := range clientMap {
delete(clientMap, c.key)
if c.Connected() {
c.Client.Close()
}
c.Client.Close()
}
})
}
@@ -78,10 +81,6 @@ func closeTimedOutClients() {
now := time.Now().Unix()
for _, c := range clientMap {
if !c.Connected() {
delete(clientMap, c.key)
continue
}
if c.closedOn == 0 {
continue
}
@@ -93,8 +92,17 @@ func closeTimedOutClients() {
}
}
func (c *SharedClient) Connected() bool {
return c != nil && c.Client != nil
func (c *SharedClient) Address() string {
return c.addr
}
func (c *SharedClient) CheckConnection(ctx context.Context) error {
conn, err := c.dial(ctx)
if err != nil {
return err
}
conn.Close()
return nil
}
// if the client is still referenced, this is no-op.
@@ -103,7 +111,7 @@ func (c *SharedClient) Close() {
c.refCount--
}
// ConnectClient creates a new Docker client connection to the specified host.
// NewClient creates a new Docker client connection to the specified host.
//
// Returns existing client if available.
//
@@ -113,7 +121,7 @@ func (c *SharedClient) Close() {
// Returns:
// - Client: the Docker client connection.
// - error: an error if the connection failed.
func ConnectClient(host string) (*SharedClient, error) {
func NewClient(host string) (*SharedClient, error) {
clientMapMu.Lock()
defer clientMapMu.Unlock()
@@ -125,6 +133,8 @@ func ConnectClient(host string) (*SharedClient, error) {
// create client
var opt []client.Opt
var addr string
var dial func(ctx context.Context) (net.Conn, error)
if agent.IsDockerHostAgent(host) {
cfg, ok := config.GetInstance().GetAgent(host)
@@ -136,6 +146,8 @@ func ConnectClient(host string) (*SharedClient, error) {
client.WithHTTPClient(cfg.NewHTTPClient()),
client.WithAPIVersionNegotiation(),
}
addr = "tcp://" + cfg.Addr
dial = cfg.DialContext
} else {
switch host {
case "":
@@ -177,9 +189,16 @@ func ConnectClient(host string) (*SharedClient, error) {
Client: client,
key: host,
refCount: 1,
addr: addr,
dial: dial,
}
defer logging.Debug().Str("host", host).Msg("docker client connected")
// non-agent client
if c.dial == nil {
c.dial = client.Dialer()
}
defer logging.Debug().Str("host", host).Msg("docker client initialized")
clientMap[c.key] = c
return c, nil

View File

@@ -8,7 +8,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/rs/zerolog"
D "github.com/yusing/go-proxy/internal/docker"
"github.com/yusing/go-proxy/internal/docker"
idlewatcher "github.com/yusing/go-proxy/internal/docker/idlewatcher/types"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/logging"
@@ -29,7 +29,7 @@ type (
*idlewatcher.Config
*waker
client *D.SharedClient
client *docker.SharedClient
stopByMethod StopCallback // send a docker command w.r.t. `stop_method`
ticker *time.Ticker
lastReset time.Time
@@ -70,7 +70,7 @@ func registerWatcher(watcherTask *task.Task, route route.Route, waker *waker) (*
return w, nil
}
client, err := D.ConnectClient(cfg.DockerHost)
client, err := docker.NewClient(cfg.DockerHost)
if err != nil {
return nil, err
}
@@ -146,9 +146,6 @@ func (w *Watcher) containerStart(ctx context.Context) error {
}
func (w *Watcher) containerStatus() (string, error) {
if !w.client.Connected() {
return "", errors.New("docker client not connected")
}
ctx, cancel := context.WithTimeoutCause(w.task.Context(), dockerReqTimeout, errors.New("docker request timeout"))
defer cancel()
json, err := w.client.ContainerInspect(ctx, w.ContainerID)
@@ -242,7 +239,7 @@ func (w *Watcher) getEventCh(dockerWatcher *watcher.DockerWatcher) (eventCh <-ch
// it exits only if the context is canceled, the container is destroyed,
// errors occurred on docker client, or route provider died (mainly caused by config reload).
func (w *Watcher) watchUntilDestroy() (returnCause error) {
dockerWatcher := watcher.NewDockerWatcherWithClient(w.client)
dockerWatcher := watcher.NewDockerWatcher(w.Config.DockerHost)
dockerEventCh, dockerEventErrCh := w.getEventCh(dockerWatcher)
for {

View File

@@ -7,7 +7,7 @@ import (
)
func Inspect(dockerHost string, containerID string) (*Container, error) {
client, err := ConnectClient(dockerHost)
client, err := NewClient(dockerHost)
if err != nil {
return nil, err
}

View File

@@ -5,7 +5,6 @@ import (
"errors"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
)
@@ -22,8 +21,8 @@ var listOptions = container.ListOptions{
All: true,
}
func ListContainers(clientHost string) ([]types.Container, error) {
dockerClient, err := ConnectClient(clientHost)
func ListContainers(clientHost string) ([]container.Summary, error) {
dockerClient, err := NewClient(clientHost)
if err != nil {
return nil, err
}