@@ -5,55 +5,53 @@ type InstanceKey = string
5
5
export type ConnectionPurpose = "consumer" | "publisher"
6
6
7
7
export 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 [ ] > ( )
10
10
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 )
15
14
const proxies = map . get ( key ) || [ ]
16
15
const connection = proxies . at ( - 1 )
17
16
const refCount = connection ?. refCount
18
17
return refCount !== undefined && refCount < getMaxSharedConnectionInstances ( ) ? connection : undefined
19
18
}
20
19
21
- public static cacheConnection (
20
+ public cacheConnection (
22
21
purpose : ConnectionPurpose ,
23
22
streamName : string ,
24
23
vhost : string ,
25
24
host : string ,
26
25
client : Connection
27
26
) {
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 )
31
29
const currentlyCached = map . get ( key ) || [ ]
32
30
currentlyCached . push ( client )
33
31
map . set ( key , currentlyCached )
34
32
}
35
33
36
- public static removeIfUnused ( connection : Connection ) {
34
+ public removeIfUnused ( connection : Connection ) {
37
35
if ( connection . refCount <= 0 ) {
38
- ConnectionPool . removeCachedConnection ( connection )
36
+ this . removeCachedConnection ( connection )
39
37
return true
40
38
}
41
39
return false
42
40
}
43
41
44
- public static removeCachedConnection ( connection : Connection ) {
42
+ public removeCachedConnection ( connection : Connection ) {
45
43
const { leader, streamName, hostname : host , vhost } = connection
46
44
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 )
49
47
const mappedClientList = m . get ( k )
50
48
if ( mappedClientList ) {
51
49
const filtered = mappedClientList . filter ( ( c ) => c !== connection )
52
50
m . set ( k , filtered )
53
51
}
54
52
}
55
53
56
- private static getCacheKey ( streamName : string , vhost : string , host : string ) {
54
+ private getCacheKey ( streamName : string , vhost : string , host : string ) {
57
55
return `${ streamName } @${ vhost } @${ host } `
58
56
}
59
57
}
0 commit comments