@@ -5,55 +5,53 @@ type InstanceKey = string
55export type ConnectionPurpose = "consumer" | "publisher"
66
77export class ConnectionPool {
8- private static consumerConnectionProxies = new Map < InstanceKey , Connection [ ] > ( )
9- private static publisherConnectionProxies = new Map < InstanceKey , Connection [ ] > ( )
8+ private consumerConnectionProxies : Map < InstanceKey , Connection [ ] > = new Map < InstanceKey , Connection [ ] > ( )
9+ private publisherConnectionProxies : Map < InstanceKey , Connection [ ] > = new Map < InstanceKey , Connection [ ] > ( )
1010
11- public static getUsableCachedConnection ( purpose : ConnectionPurpose , streamName : string , vhost : string , host : string ) {
12- const map =
13- purpose === "publisher" ? ConnectionPool . publisherConnectionProxies : ConnectionPool . consumerConnectionProxies
14- const key = ConnectionPool . getCacheKey ( streamName , vhost , host )
11+ public getCachedConnection ( purpose : ConnectionPurpose , streamName : string , vhost : string , host : string ) {
12+ const map = purpose === "publisher" ? this . publisherConnectionProxies : this . consumerConnectionProxies
13+ const key = this . getCacheKey ( streamName , vhost , host )
1514 const proxies = map . get ( key ) || [ ]
1615 const connection = proxies . at ( - 1 )
1716 const refCount = connection ?. refCount
1817 return refCount !== undefined && refCount < getMaxSharedConnectionInstances ( ) ? connection : undefined
1918 }
2019
21- public static cacheConnection (
20+ public cacheConnection (
2221 purpose : ConnectionPurpose ,
2322 streamName : string ,
2423 vhost : string ,
2524 host : string ,
2625 client : Connection
2726 ) {
28- const map =
29- purpose === "publisher" ? ConnectionPool . publisherConnectionProxies : ConnectionPool . consumerConnectionProxies
30- const key = ConnectionPool . getCacheKey ( streamName , vhost , host )
27+ const map = purpose === "publisher" ? this . publisherConnectionProxies : this . consumerConnectionProxies
28+ const key = this . getCacheKey ( streamName , vhost , host )
3129 const currentlyCached = map . get ( key ) || [ ]
3230 currentlyCached . push ( client )
3331 map . set ( key , currentlyCached )
3432 }
3533
36- public static removeIfUnused ( connection : Connection ) {
34+ public removeIfUnused ( connection : Connection ) {
3735 if ( connection . refCount <= 0 ) {
38- ConnectionPool . removeCachedConnection ( connection )
36+ this . removeCachedConnection ( connection )
3937 return true
4038 }
4139 return false
4240 }
4341
44- public static removeCachedConnection ( connection : Connection ) {
42+ public removeCachedConnection ( connection : Connection ) {
4543 const { leader, streamName, hostname : host , vhost } = connection
4644 if ( streamName === undefined ) return
47- const m = leader ? ConnectionPool . publisherConnectionProxies : ConnectionPool . consumerConnectionProxies
48- const k = ConnectionPool . getCacheKey ( streamName , vhost , host )
45+ const m = leader ? this . publisherConnectionProxies : this . consumerConnectionProxies
46+ const k = this . getCacheKey ( streamName , vhost , host )
4947 const mappedClientList = m . get ( k )
5048 if ( mappedClientList ) {
5149 const filtered = mappedClientList . filter ( ( c ) => c !== connection )
5250 m . set ( k , filtered )
5351 }
5452 }
5553
56- private static getCacheKey ( streamName : string , vhost : string , host : string ) {
54+ private getCacheKey ( streamName : string , vhost : string , host : string ) {
5755 return `${ streamName } @${ vhost } @${ host } `
5856 }
5957}
0 commit comments