@@ -48,26 +48,32 @@ list like the following:
48
48
added: v0.3.4
49
49
-->
50
50
51
- The HTTP Agent is used for pooling sockets used in HTTP client
52
- requests.
53
-
54
- The HTTP Agent also defaults client requests to using
55
- ` Connection: keep-alive ` . If no pending HTTP requests are waiting on a
56
- socket to become free the socket is closed. This means that Node.js's
57
- pool has the benefit of keep-alive when under load but still does not
58
- require developers to manually close the HTTP clients using
59
- KeepAlive.
60
-
61
- If you opt into using HTTP KeepAlive, you can create an Agent object
62
- with that flag set to ` true ` . (See the [ constructor options] [ ] .)
63
- Then, the Agent will keep unused sockets in a pool for later use. They
64
- will be explicitly marked so as to not keep the Node.js process running.
65
- However, it is still a good idea to explicitly [ ` destroy() ` ] [ ] KeepAlive
66
- agents when they are no longer in use, so that the Sockets will be shut
67
- down.
68
-
69
- Sockets are removed from the agent's pool when the socket emits either
70
- a ` 'close' ` event or a special ` 'agentRemove' ` event. This means that if
51
+ An ` Agent ` is responsible for managing connection persistence
52
+ and reuse for HTTP clients. It maintains a queue of pending requests
53
+ for a given host and port, reusing a single socket connection for each
54
+ until the queue is empty, at which time the socket is either destroyed
55
+ or put into a pool where it is kept to be used again for requests to the
56
+ same host and port. Whether it is destroyed or pooled depends on the
57
+ ` keepAlive ` [ option] ( #http_new_agent_options ) .
58
+
59
+ Pooled connections have TCP Keep-Alive enabled for them, but servers may
60
+ still close idle connections, in which case they will be removed from the
61
+ pool and a new connection will be made when a new HTTP request is made for
62
+ that host and port. Servers may also refuse to allow multiple requests
63
+ over the same connection, in which case the connection will have to be
64
+ remade for every request and cannot be pooled. The ` Agent ` will still make
65
+ the requests to that server, but each one will occur over a new connection.
66
+
67
+ When a connection is closed by the client or the server, it is removed
68
+ from the pool. Any unused sockets in the pool will be unrefed so as not
69
+ to keep the Node.js process running when there are no outstanding requests.
70
+ (see [ socket.unref()] ).
71
+
72
+ It is good practice, to [ ` destroy() ` ] [ ] an ` Agent ` instance when it is no
73
+ longer in use, because unused sockets consume OS resources.
74
+
75
+ Sockets are removed from an agent's pool when the socket emits either
76
+ a ` 'close' ` event or an ` 'agentRemove' ` event. This means that if
71
77
you intend to keep one HTTP request open for a long time and don't
72
78
want it to stay in the pool you can do something along the lines of:
73
79
@@ -79,7 +85,11 @@ http.get(options, (res) => {
79
85
});
80
86
```
81
87
82
- Alternatively, you could just opt out of pooling entirely using
88
+ You may also use an agent for an individual request. By providing
89
+ ` {agent: false} ` as an option to the ` http.get() ` or ` http.request() `
90
+ functions, a one-time use ` Agent ` with default options will be used
91
+ for the client connection.
92
+
83
93
` agent:false ` :
84
94
85
95
``` js
@@ -100,11 +110,13 @@ added: v0.3.4
100
110
101
111
* ` options ` {Object} Set of configurable options to set on the agent.
102
112
Can have the following fields:
103
- * ` keepAlive ` {Boolean} Keep sockets around in a pool to be used by
104
- other requests in the future. Default = ` false `
105
- * ` keepAliveMsecs ` {Integer} When using HTTP KeepAlive, how often
106
- to send TCP KeepAlive packets over sockets being kept alive.
107
- Default = ` 1000 ` . Only relevant if ` keepAlive ` is set to ` true ` .
113
+ * ` keepAlive ` {Boolean} Keep sockets around even when there are no
114
+ outstanding requests, so they can be used for future requests without
115
+ having to reestablish a TCP connection. Default = ` false `
116
+ * ` keepAliveMsecs ` {Integer} When using the ` keepAlive ` option, specifies
117
+ the [ initial delay] ( #net_socket_setkeepalive_enable_initialdelay )
118
+ for TCP Keep-Alive packets. Ignored when the
119
+ ` keepAlive ` option is ` false ` or ` undefined ` . Default = ` 1000 ` .
108
120
* ` maxSockets ` {Number} Maximum number of sockets to allow per
109
121
host. Default = ` Infinity ` .
110
122
* ` maxFreeSockets ` {Number} Maximum number of sockets to leave open
@@ -114,7 +126,7 @@ added: v0.3.4
114
126
The default [ ` http.globalAgent ` ] [ ] that is used by [ ` http.request() ` ] [ ] has all
115
127
of these values set to their respective defaults.
116
128
117
- To configure any of them, you must create your own [ ` http.Agent ` ] [ ] object .
129
+ To configure any of them, you must create your own [ ` http.Agent ` ] [ ] instance .
118
130
119
131
``` js
120
132
const http = require (' http' );
@@ -136,7 +148,7 @@ added: v0.11.4
136
148
Produces a socket/stream to be used for HTTP requests.
137
149
138
150
By default, this function is the same as [ ` net.createConnection() ` ] [ ] . However,
139
- custom Agents may override this method in case greater flexibility is desired.
151
+ custom agents may override this method in case greater flexibility is desired.
140
152
141
153
A socket/stream can be supplied in one of two ways: by returning the
142
154
socket/stream from this function, or by passing the socket/stream to ` callback ` .
@@ -151,7 +163,7 @@ added: v0.11.4
151
163
Destroy any sockets that are currently in use by the agent.
152
164
153
165
It is usually not necessary to do this. However, if you are using an
154
- agent with KeepAlive enabled, then it is best to explicitly shut down
166
+ agent with ` keepAlive ` enabled, then it is best to explicitly shut down
155
167
the agent when you know that it will no longer be used. Otherwise,
156
168
sockets may hang open for quite a long time before the server
157
169
terminates them.
@@ -164,7 +176,7 @@ added: v0.11.4
164
176
* {Object}
165
177
166
178
An object which contains arrays of sockets currently awaiting use by
167
- the Agent when HTTP KeepAlive is used . Do not modify.
179
+ the agent when ` keepAlive ` is enabled . Do not modify.
168
180
169
181
### agent.getName(options)
170
182
<!-- YAML
@@ -179,8 +191,8 @@ added: v0.11.4
179
191
* Returns: {String}
180
192
181
193
Get a unique name for a set of request options, to determine whether a
182
- connection can be reused. In the http agent, this returns
183
- ` host:port:localAddress ` . In the https agent, the name includes the
194
+ connection can be reused. For an HTTP agent, this returns
195
+ ` host:port:localAddress ` . For an HTTPS agent, the name includes the
184
196
CA, cert, ciphers, and other HTTPS/TLS-specific options that determine
185
197
socket reusability.
186
198
@@ -191,7 +203,7 @@ added: v0.11.7
191
203
192
204
* {Number}
193
205
194
- By default set to 256. For Agents supporting HTTP KeepAlive , this
206
+ By default set to 256. For agents with ` keepAlive ` enabled , this
195
207
sets the maximum number of sockets that will be left open in the free
196
208
state.
197
209
@@ -224,7 +236,7 @@ added: v0.3.6
224
236
* {Object}
225
237
226
238
An object which contains arrays of sockets currently in use by the
227
- Agent . Do not modify.
239
+ agent . Do not modify.
228
240
229
241
## Class: http.ClientRequest
230
242
<!-- YAML
@@ -652,7 +664,7 @@ added: v0.1.0
652
664
* ` response ` {http.ServerResponse}
653
665
654
666
Emitted each time there is a request. Note that there may be multiple requests
655
- per connection (in the case of keep-alive connections).
667
+ per connection (in the case of HTTP Keep-Alive connections).
656
668
657
669
### Event: 'upgrade'
658
670
<!-- YAML
@@ -1489,7 +1501,7 @@ added: v0.5.9
1489
1501
1490
1502
* {http.Agent}
1491
1503
1492
- Global instance of Agent which is used as the default for all HTTP client
1504
+ Global instance of ` Agent ` which is used as the default for all HTTP client
1493
1505
requests.
1494
1506
1495
1507
## http.request(options[ , callback] )
@@ -1519,15 +1531,13 @@ added: v0.3.6
1519
1531
* ` headers ` {Object} An object containing request headers.
1520
1532
* ` auth ` {String} Basic authentication i.e. ` 'user:password' ` to compute an
1521
1533
Authorization header.
1522
- * ` agent ` {http.Agent|Boolean} Controls [ ` Agent ` ] [ ] behavior. When an Agent
1523
- is used request will default to ` Connection: keep-alive ` . Possible values:
1534
+ * ` agent ` {http.Agent|Boolean} Controls [ ` Agent ` ] [ ] behavior. Possible values:
1524
1535
* ` undefined ` (default): use [ ` http.globalAgent ` ] [ ] for this host and port.
1525
1536
* ` Agent ` object: explicitly use the passed in ` Agent ` .
1526
- * ` false ` : opts out of connection pooling with an Agent, defaults request to
1527
- ` Connection: close ` .
1537
+ * ` false ` : causes a new ` Agent ` with default values to be used.
1528
1538
* ` createConnection ` {Function} A function that produces a socket/stream to
1529
1539
use for the request when the ` agent ` option is not used. This can be used to
1530
- avoid creating a custom Agent class just to override the default
1540
+ avoid creating a custom ` Agent ` class just to override the default
1531
1541
` createConnection ` function. See [ ` agent.createConnection() ` ] [ ] for more
1532
1542
details.
1533
1543
* ` timeout ` {Integer}: A number specifying the socket timeout in milliseconds.
@@ -1648,3 +1658,4 @@ There are a few special headers that should be noted.
1648
1658
[ constructor options ] : #http_new_agent_options
1649
1659
[ Readable Stream ] : stream.html#stream_class_stream_readable
1650
1660
[ Writable Stream ] : stream.html#stream_class_stream_writable
1661
+ [ socket.unref() ] : net.html#net_socket_unref
0 commit comments