newConnectionCache returns a concurrency-safe cache for open connections. Connections should preferably be opened only via the connection cache. It's implementation handles issues such as concurrent open/close/eviction of a connection. It also monitors for hanging connections.
()
| 42 | // It's implementation handles issues such as concurrent open/close/eviction of a connection. |
| 43 | // It also monitors for hanging connections. |
| 44 | func (r *Runtime) newConnectionCache() conncache.Cache { |
| 45 | return conncache.New(conncache.Options{ |
| 46 | MaxIdleConnections: r.opts.ConnectionCacheSize, |
| 47 | OpenTimeout: 10 * time.Minute, |
| 48 | CloseTimeout: 10 * time.Minute, |
| 49 | ErrTTL: 10 * time.Second, |
| 50 | CheckHangingInterval: time.Minute, |
| 51 | OpenFunc: func(ctx context.Context, cfg any) (conncache.Connection, error) { |
| 52 | x := cfg.(cachedConnectionConfig) |
| 53 | return r.openAndMigrate(ctx, x) |
| 54 | }, |
| 55 | KeyFunc: func(cfg any) string { |
| 56 | x := cfg.(cachedConnectionConfig) |
| 57 | return generateKey(x) |
| 58 | }, |
| 59 | HangingFunc: func(cfg any, open bool) { |
| 60 | x := cfg.(cachedConnectionConfig) |
| 61 | r.Logger.Error("connection cache: connection has been working for too long", zap.String("instance_id", x.instanceID), zap.String("driver", x.driver), zap.Bool("open", open)) |
| 62 | }, |
| 63 | Metrics: conncache.Metrics{ |
| 64 | Opens: connCacheOpens, |
| 65 | Closes: connCacheCloses, |
| 66 | SizeTotal: connCacheSizeTotal, |
| 67 | SizeLRU: connCacheSizeLRU, |
| 68 | OpenLatencyMS: connCacheOpenLatencyMS, |
| 69 | CloseLatencyMS: connCacheCloseLatencyMS, |
| 70 | }, |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | // getConnection returns a cached connection for the given driver configuration. |
| 75 | // If instanceID is empty, the connection is considered shared (see drivers.Open for details). |
no test coverage detected