Skip to content

Commit c0d8bb8

Browse files
committed
fix: memory leak in Agent
1 parent 2f6cc66 commit c0d8bb8

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

lib/dispatcher/agent.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,10 @@ class Agent extends DispatcherBase {
4545
}
4646

4747
this[kOnConnect] = (origin, targets) => {
48-
const result = this[kClients].get(origin)
49-
if (result) {
50-
result.count += 1
51-
}
5248
this.emit('connect', origin, [this, ...targets])
5349
}
5450

5551
this[kOnDisconnect] = (origin, targets, err) => {
56-
const result = this[kClients].get(origin)
57-
if (result) {
58-
result.count -= 1
59-
if (result.count <= 0) {
60-
this[kClients].delete(origin)
61-
result.dispatcher.destroy()
62-
}
63-
}
6452
this.emit('disconnect', origin, [this, ...targets], err)
6553
}
6654

@@ -91,7 +79,23 @@ class Agent extends DispatcherBase {
9179
if (!dispatcher) {
9280
dispatcher = this[kFactory](opts.origin, this[kOptions])
9381
.on('drain', this[kOnDrain])
82+
.on('connect', () => {
83+
const result = this[kClients].get(key)
84+
if (result) {
85+
result.count += 1
86+
}
87+
})
9488
.on('connect', this[kOnConnect])
89+
.on('disconnect', () => {
90+
const result = this[kClients].get(key)
91+
if (result) {
92+
result.count -= 1
93+
if (result.count <= 0) {
94+
this[kClients].delete(key)
95+
result.dispatcher.destroy()
96+
}
97+
}
98+
})
9599
.on('disconnect', this[kOnDisconnect])
96100
.on('connectionError', this[kOnConnectionError])
97101

0 commit comments

Comments
 (0)