@@ -45,22 +45,35 @@ class Agent extends DispatcherBase {
45
45
}
46
46
47
47
this[kOnConnect] = (origin, targets) => {
48
+
const result = this[kClients].get(origin)
49
+
if (result) {
50
+
result.count += 1
51
+
}
48
52
this.emit('connect', origin, [this, ...targets])
49
53
}
50
54
51
55
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
+
}
52
64
this.emit('disconnect', origin, [this, ...targets], err)
53
65
}
54
66
55
67
this[kOnConnectionError] = (origin, targets, err) => {
68
+
// TODO: should this decrement result.count here?
56
69
this.emit('connectionError', origin, [this, ...targets], err)
57
70
}
58
71
}
59
72
60
73
get [kRunning] () {
61
74
let ret = 0
62
-
for (const client of this[kClients].values()) {
63
-
ret += client[kRunning]
75
+
for (const { dispatcher } of this[kClients].values()) {
76
+
ret += dispatcher[kRunning]
64
77
}
65
78
return ret
66
79
}
@@ -73,28 +86,25 @@ class Agent extends DispatcherBase {
73
86
throw new InvalidArgumentError('opts.origin must be a non-empty string or URL.')
74
87
}
75
88
76
-
let dispatcher = this[kClients].get(key)
77
-
89
+
const result = this[kClients].get(key)
90
+
let dispatcher = result && result.dispatcher
78
91
if (!dispatcher) {
79
92
dispatcher = this[kFactory](opts.origin, this[kOptions])
80
93
.on('drain', this[kOnDrain])
81
94
.on('connect', this[kOnConnect])
82
95
.on('disconnect', this[kOnDisconnect])
83
96
.on('connectionError', this[kOnConnectionError])
84
97
85
-
// This introduces a tiny memory leak, as dispatchers are never removed from the map.
86
-
// TODO(mcollina): remove te timer when the client/pool do not have any more
87
-
// active connections.
88
-
this[kClients].set(key, dispatcher)
98
+
this[kClients].set(key, { count: 0, dispatcher })
89
99
}
90
100
91
101
return dispatcher.dispatch(opts, handler)
92
102
}
93
103
94
104
async [kClose] () {
95
105
const closePromises = []
96
-
for (const client of this[kClients].values()) {
97
-
closePromises.push(client.close())
106
+
for (const { dispatcher } of this[kClients].values()) {
107
+
closePromises.push(dispatcher.close())
98
108
}
99
109
this[kClients].clear()
100
110
@@ -103,8 +113,8 @@ class Agent extends DispatcherBase {
103
113
104
114
async [kDestroy] (err) {
105
115
const destroyPromises = []
106
-
for (const client of this[kClients].values()) {
107
-
destroyPromises.push(client.destroy(err))
116
+
for (const { dispatcher } of this[kClients].values()) {
117
+
destroyPromises.push(dispatcher.destroy(err))
108
118
}
109
119
this[kClients].clear()
110
120
@@ -113,9 +123,9 @@ class Agent extends DispatcherBase {
113
123
114
124
get stats () {
115
125
const allClientStats = {}
116
-
for (const client of this[kClients].values()) {
117
-
if (client.stats) {
118
-
allClientStats[client[kUrl].origin] = client.stats
126
+
for (const { dispatcher } of this[kClients].values()) {
127
+
if (dispatcher.stats) {
128
+
allClientStats[dispatcher[kUrl].origin] = dispatcher.stats
119
129
}
120
130
}
121
131
return allClientStats
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4