Skip to content

Commit cdbecc4

Browse files
mikealkoichik
authored andcommitted
docs: Improved http2 agent docs
Fixes #1517.
1 parent 19ff87a commit cdbecc4

1 file changed

Lines changed: 51 additions & 4 deletions

File tree

doc/api/http.markdown

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ Options:
399399
- `path`: Request path. Should include query string and fragments if any.
400400
E.G. `'/index.html?page=12'`
401401
- `headers`: An object containing request headers.
402-
- `agent`: Controls `Agent` behavior. When an Agent is used request will default to Connection:keep-alive. Possible values:
402+
- `agent`: Controls `Agent` behavior. When an Agent is used request will default to
403+
Connection:keep-alive. Possible values:
403404
- `undefined` (default): use default `Agent` for this host and port.
404405
- `Agent` object: explicitly use the passed in `Agent`.
405406
- `false`: opts out of connection pooling with an Agent, defaults request to Connection:close.
@@ -478,21 +479,53 @@ Example:
478479

479480
## http.Agent
480481

482+
In node 0.5.3+ there is a new implementation of the HTTP Agent which is used
483+
for pooling sockets used in HTTP client requests.
484+
485+
Previously, a single agent instance help the pool for single host+port. The
486+
current implementation now holds sockets for any number of hosts.
487+
488+
The current HTTP Agent also defaults client requests to using
489+
Connection:keep-alive. If no pending HTTP requests are waiting on a socket
490+
to become free the socket is closed. This means that node's pool has the
491+
benefit of keep-alive when under load but still does not require developers
492+
to manually close the HTTP clients using keep-alive.
493+
494+
Sockets are removed from the agent's pool when the socket emits either a
495+
"close" event or a special "agentRemove" event. This means that if you intend
496+
to keep one HTTP request open for a long time and don't want it to stay in the
497+
pool you can do something along the lines of:
498+
499+
http.get(options, function(res) {
500+
// Do stuff
501+
}).on("socket", function (socket) {
502+
socket.emit("agentRemove");
503+
});
504+
505+
Alternatively, you could just opt out of pooling entirely using `agent:false`:
506+
507+
http.get({host:'localhost', port:80, path:'/', agent:false}, function (res) {
508+
// Do stuff
509+
})
510+
481511
## http.globalAgent
482512

483513
Global instance of Agent which is used as the default for all http client requests.
484514

485515
### agent.maxSockets
486516

487-
By default set to 5. Determines how many concurrent sockets the agent can have open per host.
517+
By default set to 5. Determines how many concurrent sockets the agent can have
518+
open per host.
488519

489520
### agent.sockets
490521

491-
An object which contains arrays of sockets currently in use by the Agent. Do not modify.
522+
An object which contains arrays of sockets currently in use by the Agent. Do not
523+
modify.
492524

493525
### agent.requests
494526

495-
An object which contains queues of requests that have not yet been assigned to sockets. Do not modify.
527+
An object which contains queues of requests that have not yet been assigned to
528+
sockets. Do not modify.
496529

497530

498531
## http.ClientRequest
@@ -645,6 +678,20 @@ followed by `request.end()`.
645678

646679
Aborts a request. (New since v0.3.8.)
647680

681+
### request.setTimeout(timeout, [callback])
682+
683+
Once a socket is assigned to this request and is connected
684+
socket.setTimeout(timeout, [callback]) will be called.
685+
686+
### request.setNoDelay(noDelay=true)
687+
688+
Once a socket is assigned to this request and is connected
689+
socket.setNoDelay(noDelay) will be called.
690+
691+
### request.setSocketKeepAlive(enable=false, [initialDelay])
692+
693+
Once a socket is assigned to this request and is connected
694+
socket.setKeepAlive(enable, [initialDelay]) will be called.
648695

649696
## http.ClientResponse
650697

0 commit comments

Comments
 (0)