Skip to content

Commit ab7587e

Browse files
lanceMylesBorins
authored andcommitted
doc: update http.md for consistency and clarity
The HTTP Keep-Alive text was inconsistent. These changes follow the following rules * When referring to flag used in code, it should always be written using backticks and in camel case. E.g. `keepAlive`. * When referring to the mechanism Keep-Alive functionality as described in the HTTP 1.1 RFC, it is written as 'HTTP Keep-Alive', without the use of backticks. * When referring to the request header, it should always use backticks and be written as `Connection: keep-alive`. This commit also includes some changes to how `http.Agent` is referenced. When `Agent` is used as a reference to an object or instance, backticks should always be used. And lastly, the documentation about `Agent` behavior around HTTP Keep-Alive has been clarified and improved. Fixes: #10567 PR-URL: #10715 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent b9615b3 commit ab7587e

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

doc/api/http.md

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,32 @@ list like the following:
4848
added: v0.3.4
4949
-->
5050

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
7177
you intend to keep one HTTP request open for a long time and don't
7278
want it to stay in the pool you can do something along the lines of:
7379

@@ -79,7 +85,11 @@ http.get(options, (res) => {
7985
});
8086
```
8187

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+
8393
`agent:false`:
8494

8595
```js
@@ -104,7 +114,7 @@ added: v0.3.4
104114
outstanding requests, so they can be used for future requests without
105115
having to reestablish a TCP connection. Default = `false`
106116
* `keepAliveMsecs` {Integer} When using the `keepAlive` option, specifies
107-
the [initial delay](net.html#net_socket_setkeepalive_enable_initialdelay)
117+
the [initial delay](#net_socket_setkeepalive_enable_initialdelay)
108118
for TCP Keep-Alive packets. Ignored when the
109119
`keepAlive` option is `false` or `undefined`. Default = `1000`.
110120
* `maxSockets` {Number} Maximum number of sockets to allow per
@@ -116,7 +126,7 @@ added: v0.3.4
116126
The default [`http.globalAgent`][] that is used by [`http.request()`][] has all
117127
of these values set to their respective defaults.
118128

119-
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.
120130

121131
```js
122132
const http = require('http');
@@ -138,7 +148,7 @@ added: v0.11.4
138148
Produces a socket/stream to be used for HTTP requests.
139149

140150
By default, this function is the same as [`net.createConnection()`][]. However,
141-
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.
142152

143153
A socket/stream can be supplied in one of two ways: by returning the
144154
socket/stream from this function, or by passing the socket/stream to `callback`.
@@ -153,7 +163,7 @@ added: v0.11.4
153163
Destroy any sockets that are currently in use by the agent.
154164

155165
It is usually not necessary to do this. However, if you are using an
156-
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
157167
the agent when you know that it will no longer be used. Otherwise,
158168
sockets may hang open for quite a long time before the server
159169
terminates them.
@@ -166,7 +176,7 @@ added: v0.11.4
166176
* {Object}
167177

168178
An object which contains arrays of sockets currently awaiting use by
169-
the Agent when HTTP KeepAlive is used. Do not modify.
179+
the agent when `keepAlive` is enabled. Do not modify.
170180

171181
### agent.getName(options)
172182
<!-- YAML
@@ -181,8 +191,8 @@ added: v0.11.4
181191
* Returns: {String}
182192

183193
Get a unique name for a set of request options, to determine whether a
184-
connection can be reused. In the http agent, this returns
185-
`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
186196
CA, cert, ciphers, and other HTTPS/TLS-specific options that determine
187197
socket reusability.
188198

@@ -193,7 +203,7 @@ added: v0.11.7
193203

194204
* {Number}
195205

196-
By default set to 256. For Agents supporting HTTP KeepAlive, this
206+
By default set to 256. For agents with `keepAlive` enabled, this
197207
sets the maximum number of sockets that will be left open in the free
198208
state.
199209

@@ -226,7 +236,7 @@ added: v0.3.6
226236
* {Object}
227237

228238
An object which contains arrays of sockets currently in use by the
229-
Agent. Do not modify.
239+
agent. Do not modify.
230240

231241
## Class: http.ClientRequest
232242
<!-- YAML
@@ -662,7 +672,7 @@ added: v0.1.0
662672
* `response` {http.ServerResponse}
663673

664674
Emitted each time there is a request. Note that there may be multiple requests
665-
per connection (in the case of keep-alive connections).
675+
per connection (in the case of HTTP Keep-Alive connections).
666676

667677
### Event: 'upgrade'
668678
<!-- YAML
@@ -1510,7 +1520,7 @@ added: v0.5.9
15101520

15111521
* {http.Agent}
15121522

1513-
Global instance of Agent which is used as the default for all HTTP client
1523+
Global instance of `Agent` which is used as the default for all HTTP client
15141524
requests.
15151525

15161526
## http.request(options[, callback])
@@ -1540,15 +1550,13 @@ added: v0.3.6
15401550
* `headers` {Object} An object containing request headers.
15411551
* `auth` {String} Basic authentication i.e. `'user:password'` to compute an
15421552
Authorization header.
1543-
* `agent` {http.Agent|Boolean} Controls [`Agent`][] behavior. When an Agent
1544-
is used request will default to `Connection: keep-alive`. Possible values:
1553+
* `agent` {http.Agent|Boolean} Controls [`Agent`][] behavior. Possible values:
15451554
* `undefined` (default): use [`http.globalAgent`][] for this host and port.
15461555
* `Agent` object: explicitly use the passed in `Agent`.
1547-
* `false`: opts out of connection pooling with an Agent, defaults request to
1548-
`Connection: close`.
1556+
* `false`: causes a new `Agent` with default values to be used.
15491557
* `createConnection` {Function} A function that produces a socket/stream to
15501558
use for the request when the `agent` option is not used. This can be used to
1551-
avoid creating a custom Agent class just to override the default
1559+
avoid creating a custom `Agent` class just to override the default
15521560
`createConnection` function. See [`agent.createConnection()`][] for more
15531561
details.
15541562
* `timeout` {Integer}: A number specifying the socket timeout in milliseconds.
@@ -1669,3 +1677,4 @@ There are a few special headers that should be noted.
16691677
[constructor options]: #http_new_agent_options
16701678
[Readable Stream]: stream.html#stream_class_stream_readable
16711679
[Writable Stream]: stream.html#stream_class_stream_writable
1680+
[socket.unref()]: net.html#net_socket_unref

0 commit comments

Comments
 (0)