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() }