|
| 1 | +import { RequestContext } from "@aws-sdk/types"; |
| 2 | +import { ConnectConfiguration } from "@aws-sdk/types/src/connection/config"; |
| 3 | +import { ConnectionManager, ConnectionManagerConfiguration } from "@aws-sdk/types/src/connection/manager"; |
| 4 | +import http2, { ClientHttp2Session } from "http2"; |
| 5 | + |
| 6 | +import { NodeHttp2ConnectionPool } from "./node-http2-connection-pool"; |
| 7 | + |
| 8 | +export class NodeHttp2ConnectionManager implements ConnectionManager<ClientHttp2Session> { |
| 9 | + constructor(config: ConnectionManagerConfiguration) { |
| 10 | + this.config = config; |
| 11 | + |
| 12 | + if (this.config.maxConcurrency && this.config.maxConcurrency <= 0) { |
| 13 | + throw new RangeError("maxConcurrency must be greater than zero."); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + private config: ConnectionManagerConfiguration; |
| 18 | + |
| 19 | + private readonly sessionCache: Map<string, NodeHttp2ConnectionPool> = new Map<string, NodeHttp2ConnectionPool>(); |
| 20 | + |
| 21 | + public lease(requestContext: RequestContext, connectionConfiguration: ConnectConfiguration): ClientHttp2Session { |
| 22 | + const url = this.getUrlString(requestContext); |
| 23 | + |
| 24 | + const existingPool = this.sessionCache.get(url); |
| 25 | + |
| 26 | + if (existingPool) { |
| 27 | + const existingSession = existingPool.poll(); |
| 28 | + if (existingSession && !this.config.disableConcurrency) { |
| 29 | + return existingSession; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const session = http2.connect(url); |
| 34 | + |
| 35 | + if (this.config.maxConcurrency) { |
| 36 | + session.settings({ maxConcurrentStreams: this.config.maxConcurrency }, (err) => { |
| 37 | + if (err) { |
| 38 | + throw new Error( |
| 39 | + "Fail to set maxConcurrentStreams to " + |
| 40 | + this.config.maxConcurrency + |
| 41 | + "when creating new session for " + |
| 42 | + requestContext.destination.toString() |
| 43 | + ); |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + // AWS SDK does not expect server push streams, don't keep node alive without a request. |
| 49 | + session.unref(); |
| 50 | + |
| 51 | + const destroySessionCb = () => { |
| 52 | + session.destroy(); |
| 53 | + this.deleteSession(url, session); |
| 54 | + }; |
| 55 | + session.on("goaway", destroySessionCb); |
| 56 | + session.on("error", destroySessionCb); |
| 57 | + session.on("frameError", destroySessionCb); |
| 58 | + session.on("close", () => this.deleteSession(url, session)); |
| 59 | + |
| 60 | + if (connectionConfiguration.requestTimeout) { |
| 61 | + session.setTimeout(connectionConfiguration.requestTimeout, destroySessionCb); |
| 62 | + } |
| 63 | + |
| 64 | + const connectionPool = this.sessionCache.get(url) || new NodeHttp2ConnectionPool(); |
| 65 | + |
| 66 | + connectionPool.offerLast(session); |
| 67 | + |
| 68 | + this.sessionCache.set(url, connectionPool); |
| 69 | + |
| 70 | + return session; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Delete a session from the connection pool. |
| 75 | + * @param authority The authority of the session to delete. |
| 76 | + * @param session The session to delete. |
| 77 | + */ |
| 78 | + public deleteSession(authority: string, session: ClientHttp2Session): void { |
| 79 | + const existingConnectionPool = this.sessionCache.get(authority); |
| 80 | + |
| 81 | + if (!existingConnectionPool) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (!existingConnectionPool.contains(session)) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + existingConnectionPool.remove(session); |
| 90 | + |
| 91 | + this.sessionCache.set(authority, existingConnectionPool); |
| 92 | + } |
| 93 | + |
| 94 | + public release(requestContext: RequestContext, session: ClientHttp2Session): void { |
| 95 | + const cacheKey = this.getUrlString(requestContext); |
| 96 | + this.sessionCache.get(cacheKey)?.offerLast(session); |
| 97 | + } |
| 98 | + |
| 99 | + public destroy(): void { |
| 100 | + for (const [key, connectionPool] of this.sessionCache) { |
| 101 | + for (const session of connectionPool) { |
| 102 | + if (!session.destroyed) { |
| 103 | + session.destroy(); |
| 104 | + } |
| 105 | + connectionPool.remove(session); |
| 106 | + } |
| 107 | + this.sessionCache.delete(key); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public setMaxConcurrentStreams(maxConcurrentStreams: number) { |
| 112 | + if (this.config.maxConcurrency && this.config.maxConcurrency <= 0) { |
| 113 | + throw new RangeError("maxConcurrentStreams must be greater than zero."); |
| 114 | + } |
| 115 | + this.config.maxConcurrency = maxConcurrentStreams; |
| 116 | + } |
| 117 | + |
| 118 | + public setDisableConcurrentStreams(disableConcurrentStreams: boolean) { |
| 119 | + this.config.disableConcurrency = disableConcurrentStreams; |
| 120 | + } |
| 121 | + |
| 122 | + private getUrlString(request: RequestContext): string { |
| 123 | + return request.destination.toString(); |
| 124 | + } |
| 125 | +} |
0 commit comments