go.temporal.io/server/common/client_cache.go
146 LOC · 40 covered · 106 uncovered · 9 ranges · 64 concepts · 5 introducers · 12 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package common
import (
"sync"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
)
type (
// ClientCache store initialized clients
ClientCache interface {
Lookup(key string, index int) (string, error) // pass through to keyResolver
GetClientForKey(key string, index int) (any, error)
GetClientForClientKey(clientKey string) (any, error)
GetAllClients() ([]any, error)
// Evict removes the cached entry for the given key and runs its
// release fn.
Evict(clientKey string)
// EvictAll removes every cached entry and runs each one's release fn.
// Used to deterministically release cached gRPC connections on shutdown.
EvictAll()
}
keyResolver interface {
Lookup(key string, index int) (string, error)
GetAllAddresses() ([]string, error)
}
// The returned release fn (if non-nil) is invoked when the entry is evicted.
clientProvider func(clientKey string) (any, func() error, error)
cachedEntry struct {
client any
release func() error
}
clientCacheImpl struct {
keyResolver keyResolver
clientProvider clientProvider
cacheLock sync.RWMutex
clients map[string]cachedEntry
logger log.Logger
}
)
// NewClientCache creates a new client cache based on membership
func NewClientCache(
keyResolver keyResolver,
clientProvider clientProvider,
logger log.Logger,
return &clientCacheImpl{
keyResolver: keyResolver,
clientProvider: clientProvider,
clients: make(map[string]cachedEntry),
logger: logger,
}
}
return c.keyResolver.Lookup(key, index)
}
func (c *clientCacheImpl) GetClientForKey(key string, index int) (any, error) {
clientKey, err := c.Lookup(key, index)
if err != nil {
return nil, err
}
return c.GetClientForClientKey(clientKey)
}
func (c *clientCacheImpl) GetClientForClientKey(clientKey string) (any, error) {
service_grpc.pb.go ×20
c.cacheLock.RLock()
entry, ok := c.clients[clientKey]
c.cacheLock.RUnlock()
if ok {
return entry.client, nil
}
defer c.cacheLock.Unlock()
entry, ok = c.clients[clientKey]
if ok {
}
if err != nil {
return nil, err
}
return client, nil
}
func (c *clientCacheImpl) GetAllClients() ([]any, error) {
var result []any
allAddresses, err := c.keyResolver.GetAllAddresses()
if err != nil {
return nil, err
}
for _, addr := range allAddresses {
client, err := c.GetClientForClientKey(addr)
if err != nil {
return nil, err
}
result = append(result, client)
}
return result, nil
}
func (c *clientCacheImpl) Evict(clientKey string) {
c.cacheLock.Lock()
entry, ok := c.clients[clientKey]
if ok {
delete(c.clients, clientKey)
}
c.cacheLock.Unlock()
if ok && entry.release != nil {
if err := entry.release(); err != nil {
c.logger.Warn("Error releasing evicted client resource", tag.Error(err))
}
}
}
c.cacheLock.Lock()
entries := c.clients
c.clients = make(map[string]cachedEntry)
c.cacheLock.Unlock()
for _, entry := range entries {
if err := entry.release(); err != nil {
c.logger.Warn("Error releasing evicted client resource", tag.Error(err))
}
}
}
}