Files
mxaccessgw/clients/go/cmd/mxgw-go/batchcache.go
T
Joseph Doherty 5a207f622a fix(client-go): keepalive, stream error classification, auth composition, CLI batch reuse (CGO-001..008, #257)
- Dial/DialGalaxy set HTTP/2 keepalive (30 s / 10 s / PermitWithoutStream), configurable via
  Options.KeepAliveTime/KeepAliveTimeout (CGO-001, D7)
- auth installed with chain interceptors and attached per call by Client/GalaxyClient (CGO-002)
- a server-sent Canceled status or a context deadline ends a stream with a terminal error;
  only the caller's own cancellation is silent (CGO-003)
- stream context cancelled when the stream ends (CGO-004)
- dialConnected keeps the last connection error in the returned error (CGO-005)
- WatchDeployEvents closes events before errs; CLI drains buffered events (CGO-006)
- Register/AddItem/AddItem2 return MalformedReplyError on a payload-less reply (CGO-007)
- CLI batch reuses dialed connections; bench-read-bulk backs off on failures (CGO-008)
2026-12-03 09:51:57 -05:00

135 lines
3.3 KiB
Go

package main
import (
"context"
"sync"
"time"
mxgateway "gitea.dohertylan.com/dohertj2/mxaccessgw/clients/go/mxgateway"
)
type batchCacheKey struct{}
// batchConnection identifies one distinct connection configuration.
type batchConnection struct {
endpoint string
apiKey string
plaintext bool
caCertFile string
serverName string
requireTLS bool
timeout time.Duration
}
func newBatchConnection(options commonOptions) batchConnection {
return batchConnection{
endpoint: options.Endpoint,
apiKey: options.apiKeyValue,
plaintext: options.Plaintext,
caCertFile: options.CACertFile,
serverName: options.ServerName,
requireTLS: options.RequireCertificateValidation,
timeout: options.timeout,
}
}
// batchClientCache keeps one dialed client per connection configuration for the
// lifetime of a batch run, so consecutive lines do not each pay a connection setup.
type batchClientCache struct {
mu sync.Mutex
clients map[batchConnection]*mxgateway.Client
galaxy map[batchConnection]*mxgateway.GalaxyClient
}
func batchCacheFrom(ctx context.Context) *batchClientCache {
cache, _ := ctx.Value(batchCacheKey{}).(*batchClientCache)
return cache
}
func (c *batchClientCache) client(ctx context.Context, options commonOptions) (*mxgateway.Client, error) {
key := newBatchConnection(options)
c.mu.Lock()
defer c.mu.Unlock()
if client, ok := c.clients[key]; ok {
return client, nil
}
client, err := mxgateway.Dial(ctx, options.dialOptions())
if err != nil {
return nil, err
}
if c.clients == nil {
c.clients = map[batchConnection]*mxgateway.Client{}
}
c.clients[key] = client
return client, nil
}
func (c *batchClientCache) galaxyClient(ctx context.Context, options commonOptions) (*mxgateway.GalaxyClient, error) {
key := newBatchConnection(options)
c.mu.Lock()
defer c.mu.Unlock()
if client, ok := c.galaxy[key]; ok {
return client, nil
}
client, err := mxgateway.DialGalaxy(ctx, options.dialOptions())
if err != nil {
return nil, err
}
if c.galaxy == nil {
c.galaxy = map[batchConnection]*mxgateway.GalaxyClient{}
}
c.galaxy[key] = client
return client, nil
}
func (c *batchClientCache) ownsClient(client *mxgateway.Client) bool {
c.mu.Lock()
defer c.mu.Unlock()
for _, cached := range c.clients {
if cached == client {
return true
}
}
return false
}
func (c *batchClientCache) ownsGalaxyClient(client *mxgateway.GalaxyClient) bool {
c.mu.Lock()
defer c.mu.Unlock()
for _, cached := range c.galaxy {
if cached == client {
return true
}
}
return false
}
func (c *batchClientCache) closeAll() {
c.mu.Lock()
defer c.mu.Unlock()
for _, client := range c.clients {
_ = client.Close()
}
for _, client := range c.galaxy {
_ = client.Close()
}
c.clients = nil
c.galaxy = nil
}
// releaseClient closes a command's client unless the batch cache owns it.
func releaseClient(ctx context.Context, client *mxgateway.Client) {
if cache := batchCacheFrom(ctx); cache != nil && cache.ownsClient(client) {
return
}
_ = client.Close()
}
// releaseGalaxyClient closes a command's Galaxy client unless the batch cache owns it.
func releaseGalaxyClient(ctx context.Context, client *mxgateway.GalaxyClient) {
if cache := batchCacheFrom(ctx); cache != nil && cache.ownsGalaxyClient(client) {
return
}
_ = client.Close()
}