Skip to content

Commit ae403dc

Browse files
author
calibr
committed
fix forever option in node > 0.10 request#1709
1 parent 7a42a98 commit ae403dc

3 files changed

Lines changed: 87 additions & 4 deletions

File tree

‎index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ function wrapRequestMethod (method, options, requester, verb) {
8888
var target = {}
8989
extend(true, target, options, params)
9090

91+
target.pool = params.pool || options.pool
92+
9193
if (verb) {
9294
target.method = (verb === 'del' ? 'DELETE' : verb.toUpperCase())
9395
}

‎request.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,9 @@ Request.prototype.init = function (options) {
483483
if (v.major === 0 && v.minor <= 10) {
484484
self.agentClass = protocol === 'http:' ? ForeverAgent : ForeverAgent.SSL
485485
} else {
486-
self.agent = new self.httpModule.Agent({
487-
keepAlive: true,
488-
maxSockets: (options.pool && options.pool.maxSockets) || Infinity
489-
})
486+
self.agentClass = self.httpModule.Agent
487+
self.agentOptions = self.agentOptions || {}
488+
self.agentOptions.keepAlive = true
490489
}
491490
} else {
492491
self.agentClass = self.httpModule.Agent

‎tests/test-pool.js‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,88 @@ tape('forever', function(t) {
5858
})
5959
})
6060

61+
tape('forever, should use same agent in sequential requests', function(t) {
62+
var r = request.defaults({
63+
forever: true
64+
})
65+
var req1 = r('http://localhost:6767')
66+
var req2 = r('http://localhost:6767/somepath')
67+
req1.abort()
68+
req2.abort()
69+
if (typeof req1.agent.destroy === 'function') {
70+
req1.agent.destroy()
71+
}
72+
if (typeof req2.agent.destroy === 'function') {
73+
req2.agent.destroy()
74+
}
75+
t.equal(req1.agent, req2.agent)
76+
t.end()
77+
})
78+
79+
tape('forever, should use same agent in sequential requests(with pool.maxSockets)', function(t) {
80+
var r = request.defaults({
81+
forever: true,
82+
pool: {maxSockets: 1024}
83+
})
84+
var req1 = r('http://localhost:6767')
85+
var req2 = r('http://localhost:6767/somepath')
86+
req1.abort()
87+
req2.abort()
88+
if (typeof req1.agent.destroy === 'function') {
89+
req1.agent.destroy()
90+
}
91+
if (typeof req2.agent.destroy === 'function') {
92+
req2.agent.destroy()
93+
}
94+
t.equal(req1.agent.maxSockets, 1024)
95+
t.equal(req1.agent, req2.agent)
96+
t.end()
97+
})
98+
99+
tape('forever, should use same agent in request() and request.verb', function(t) {
100+
var r = request.defaults({
101+
forever: true,
102+
pool: {maxSockets: 1024}
103+
})
104+
var req1 = r('http://localhost:6767')
105+
var req2 = r.get('http://localhost:6767')
106+
req1.abort()
107+
req2.abort()
108+
if (typeof req1.agent.destroy === 'function') {
109+
req1.agent.destroy()
110+
}
111+
if (typeof req2.agent.destroy === 'function') {
112+
req2.agent.destroy()
113+
}
114+
t.equal(req1.agent.maxSockets, 1024)
115+
t.equal(req1.agent, req2.agent)
116+
t.end()
117+
})
118+
119+
tape('should use different agent if pool option specified', function(t) {
120+
var r = request.defaults({
121+
forever: true,
122+
pool: {maxSockets: 1024}
123+
})
124+
var req1 = r('http://localhost:6767')
125+
var req2 = r.get({
126+
url: 'http://localhost:6767',
127+
pool: {maxSockets: 20}
128+
})
129+
req1.abort()
130+
req2.abort()
131+
if (typeof req1.agent.destroy === 'function') {
132+
req1.agent.destroy()
133+
}
134+
if (typeof req2.agent.destroy === 'function') {
135+
req2.agent.destroy()
136+
}
137+
t.equal(req1.agent.maxSockets, 1024)
138+
t.equal(req2.agent.maxSockets, 20)
139+
t.notEqual(req1.agent, req2.agent)
140+
t.end()
141+
})
142+
61143
tape('cleanup', function(t) {
62144
s.close(function() {
63145
t.end()

0 commit comments

Comments
 (0)