(key: string, connector: McpConnector, forceFresh: boolean)
| 58 | const idle = new Map<string, IdleConnection>(); |
| 59 | |
| 60 | const acquire = (key: string, connector: McpConnector, forceFresh: boolean) => |
| 61 | Effect.gen(function* () { |
| 62 | if (forceFresh) { |
| 63 | const connection = yield* connector; |
| 64 | return { connection, reused: false } satisfies ConnectionLease; |
| 65 | } |
| 66 | |
| 67 | const taken = yield* Effect.sync(() => { |
| 68 | const entry = idle.get(key); |
| 69 | if (!entry) return { connection: undefined, expired: undefined }; |
| 70 | idle.delete(key); |
| 71 | if (Date.now() - entry.idleSince >= IDLE_TTL_MS) { |
| 72 | return { connection: undefined, expired: entry.connection }; |
| 73 | } |
| 74 | return { connection: entry.connection, expired: undefined }; |
| 75 | }); |
| 76 | |
| 77 | if (taken.expired) yield* closeQuietly(taken.expired); |
| 78 | if (taken.connection) { |
| 79 | return { connection: taken.connection, reused: true } satisfies ConnectionLease; |
| 80 | } |
| 81 | |
| 82 | const connection = yield* connector; |
| 83 | return { connection, reused: false } satisfies ConnectionLease; |
| 84 | }); |
| 85 | |
| 86 | const release = <A, E>(key: string, lease: ConnectionLease, exit: Exit.Exit<A, E>) => |
| 87 | Effect.gen(function* () { |
no test coverage detected