refactor(docker): simplify docker client initialization in api

This commit is contained in:
yusing
2025-10-07 21:26:52 +08:00
parent b46b464e65
commit c27bc0e129
2 changed files with 13 additions and 69 deletions

View File

@@ -84,12 +84,23 @@ func closeTimedOutClients() {
}
}
// Clients return a map of currently connected clients.
// Close() must be called on all these clients after use.
func Clients() map[string]*SharedClient {
clientMapMu.RLock()
defer clientMapMu.RUnlock()
clients := make(map[string]*SharedClient, len(clientMap))
maps.Copy(clients, clientMap)
clientMapMu.RUnlock()
// add 1 ref count to prevent them from
// being closed before caller finished using them
for _, c := range clients {
// last Close() has been called, reset closeOn
if atomic.AddUint32(&c.refCount, 1) == 1 {
atomic.StoreInt64(&c.closedOn, 0)
}
}
return clients
}