2025-09-25 15:40:25 +02:00
package aibridged
import (
"context"
"net/http"
"sync"
"time"
"github.com/dgraph-io/ristretto/v2"
2025-12-05 15:59:52 +01:00
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
2025-09-25 15:40:25 +02:00
"golang.org/x/xerrors"
"tailscale.com/util/singleflight"
2026-01-08 10:29:41 +04:00
"cdr.dev/slog/v3"
2026-04-22 17:01:01 +02:00
"github.com/coder/coder/v2/aibridge"
"github.com/coder/coder/v2/aibridge/mcp"
"github.com/coder/coder/v2/aibridge/tracing"
2025-09-25 15:40:25 +02:00
)
const (
cacheCost = 1 // We can't know the actual size in bytes of the value (it'll change over time).
)
// Pooler describes a pool of [*aibridge.RequestBridge] instances from which instances can be retrieved.
// One [*aibridge.RequestBridge] instance is created per given key.
type Pooler interface {
2025-09-25 16:01:56 +02:00
Acquire ( ctx context . Context , req Request , clientFn ClientFunc , mcpBootstrapper MCPProxyBuilder ) ( http . Handler , error )
2025-09-25 15:40:25 +02:00
Shutdown ( ctx context . Context ) error
}
type PoolMetrics interface {
Hits () uint64
Misses () uint64
KeysAdded () uint64
KeysEvicted () uint64
}
type PoolOptions struct {
MaxItems int64
TTL time . Duration
}
2025-12-30 13:44:34 +02:00
var DefaultPoolOptions = PoolOptions { MaxItems : 5000 , TTL : time . Minute * 15 }
2025-09-25 15:40:25 +02:00
var _ Pooler = & CachedBridgePool {}
type CachedBridgePool struct {
cache * ristretto . Cache [ string , * aibridge . RequestBridge ]
providers [] aibridge . Provider
logger slog . Logger
options PoolOptions
singleflight * singleflight . Group [ string , * aibridge . RequestBridge ]
2025-11-24 18:16:06 +02:00
metrics * aibridge . Metrics
2025-12-05 15:59:52 +01:00
tracer trace . Tracer
2025-11-24 18:16:06 +02:00
2025-09-25 15:40:25 +02:00
shutDownOnce sync . Once
shuttingDownCh chan struct {}
}
2025-12-05 15:59:52 +01:00
func NewCachedBridgePool ( options PoolOptions , providers [] aibridge . Provider , logger slog . Logger , metrics * aibridge . Metrics , tracer trace . Tracer ) ( * CachedBridgePool , error ) {
2025-09-25 15:40:25 +02:00
cache , err := ristretto . NewCache ( & ristretto . Config [ string , * aibridge . RequestBridge ]{
NumCounters : options . MaxItems * 10 , // Docs suggest setting this 10x number of keys.
MaxCost : options . MaxItems * cacheCost , // Up to n instances.
IgnoreInternalCost : true , // Don't try estimate cost using bytes (ristretto does this naïvely anyway, just using the size of the value struct not the REAL memory usage).
BufferItems : 64 , // Sticking with recommendation from docs.
Metrics : true , // Collect metrics (only used in tests, for now).
OnEvict : func ( item * ristretto . Item [ * aibridge . RequestBridge ]) {
if item == nil || item . Value == nil {
return
}
shutdownCtx , shutdownCancel := context . WithTimeout ( context . Background (), time . Second * 5 )
defer shutdownCancel ()
// Run the eviction in the background since ristretto blocks sets until a free slot is available.
go func () {
2025-09-25 16:37:28 +02:00
_ = item . Value . Shutdown ( shutdownCtx )
2025-09-25 15:40:25 +02:00
}()
},
})
if err != nil {
return nil , xerrors . Errorf ( "create cache: %w" , err )
}
return & CachedBridgePool {
cache : cache ,
providers : providers ,
options : options ,
2025-12-05 15:59:52 +01:00
metrics : metrics ,
tracer : tracer ,
logger : logger ,
2025-09-25 15:40:25 +02:00
singleflight : & singleflight . Group [ string , * aibridge . RequestBridge ]{},
shuttingDownCh : make ( chan struct {}),
}, nil
}
// Acquire retrieves or creates a [*aibridge.RequestBridge] instance per given key.
//
// Each returned [*aibridge.RequestBridge] is safe for concurrent use.
// Each [*aibridge.RequestBridge] is stateful because it has MCP clients which maintain sessions to the configured MCP server.
2025-12-05 15:59:52 +01:00
func ( p * CachedBridgePool ) Acquire ( ctx context . Context , req Request , clientFn ClientFunc , mcpProxyFactory MCPProxyBuilder ) ( _ http . Handler , outErr error ) {
spanAttrs := [] attribute . KeyValue {
attribute . String ( tracing . InitiatorID , req . InitiatorID . String ()),
attribute . String ( tracing . APIKeyID , req . APIKeyID ),
}
ctx , span := p . tracer . Start ( ctx , "CachedBridgePool.Acquire" , trace . WithAttributes ( spanAttrs ... ))
defer tracing . EndSpanErr ( span , & outErr )
ctx = tracing . WithRequestBridgeAttributesInContext ( ctx , spanAttrs )
2025-09-25 15:40:25 +02:00
if err := ctx . Err (); err != nil {
return nil , xerrors . Errorf ( "acquire: %w" , err )
}
select {
case <- p . shuttingDownCh :
return nil , xerrors . New ( "pool shutting down" )
default :
}
// Wait for all buffered writes to be applied, otherwise multiple calls in quick succession
// may visit the slow path unnecessarily.
defer p . cache . Wait ()
// Fast path.
2025-11-10 07:46:41 -05:00
cacheKey := req . InitiatorID . String () + "|" + req . APIKeyID
bridge , ok := p . cache . Get ( cacheKey )
2025-09-25 15:40:25 +02:00
if ok && bridge != nil {
// TODO: future improvement:
// Once we can detect token expiry against an MCP server, we no longer need to let these instances
// expire after the original TTL; we can extend the TTL on each Acquire() call.
// For now, we need to let the instance expiry to keep the MCP connections fresh.
2025-12-05 15:59:52 +01:00
span . AddEvent ( "cache_hit" )
2025-09-25 15:40:25 +02:00
return bridge , nil
}
2025-12-05 15:59:52 +01:00
span . AddEvent ( "cache_miss" )
recorder := aibridge . NewRecorder ( p . logger . Named ( "recorder" ), p . tracer , func () ( aibridge . Recorder , error ) {
2025-11-10 07:46:41 -05:00
client , err := clientFn ()
if err != nil {
return nil , xerrors . Errorf ( "acquire client: %w" , err )
}
return & recorderTranslation { apiKeyID : req . APIKeyID , client : client }, nil
})
2025-09-25 15:40:25 +02:00
// Slow path.
// Creating an *aibridge.RequestBridge may take some time, so gate all subsequent callers behind the initial request and return the resulting value.
// TODO: track startup time since it adds latency to first request (histogram count will also help us see how often this occurs).
instance , err , _ := p . singleflight . Do ( req . InitiatorID . String (), func () ( * aibridge . RequestBridge , error ) {
2025-09-25 16:01:56 +02:00
var (
mcpServers mcp . ServerProxier
err error
)
2025-12-05 15:59:52 +01:00
mcpServers , err = mcpProxyFactory . Build ( ctx , req , p . tracer )
2025-09-25 16:01:56 +02:00
if err != nil {
p . logger . Warn ( ctx , "failed to create MCP server proxiers" , slog . Error ( err ))
// Don't fail here; MCP server injection can gracefully degrade.
}
if mcpServers != nil {
// This will block while connections are established with upstream MCP server(s), and tools are listed.
if err := mcpServers . Init ( ctx ); err != nil {
p . logger . Warn ( ctx , "failed to initialize MCP server proxier(s)" , slog . Error ( err ))
}
}
2025-12-05 15:59:52 +01:00
bridge , err := aibridge . NewRequestBridge ( ctx , p . providers , recorder , mcpServers , p . logger , p . metrics , p . tracer )
2025-09-25 15:40:25 +02:00
if err != nil {
return nil , xerrors . Errorf ( "create new request bridge: %w" , err )
}
2025-11-10 07:46:41 -05:00
p . cache . SetWithTTL ( cacheKey , bridge , cacheCost , p . options . TTL )
2025-09-25 15:40:25 +02:00
return bridge , nil
})
return instance , err
}
2025-11-24 18:16:06 +02:00
func ( p * CachedBridgePool ) CacheMetrics () PoolMetrics {
2025-09-25 15:40:25 +02:00
if p . cache == nil {
return nil
}
return p . cache . Metrics
}
// Shutdown will close the cache which will trigger eviction of all the Bridge entries.
func ( p * CachedBridgePool ) Shutdown ( _ context . Context ) error {
p . shutDownOnce . Do ( func () {
// Prevent new requests from being served.
close ( p . shuttingDownCh )
p . cache . Close ()
})
return nil
}