Skip to content

Commit 7dff6aa

Browse files
lanceitaloacasas
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 <vieuxtech@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 1a39bfb commit 7dff6aa

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

doc/api/http.md

+52-41
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
@@ -100,11 +110,13 @@ added: v0.3.4
100110

101111
* `options` {Object} Set of configurable options to set on the agent.
102112
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`.
108120
* `maxSockets` {Number} Maximum number of sockets to allow per
109121
host. Default = `Infinity`.
110122
* `maxFreeSockets` {Number} Maximum number of sockets to leave open
@@ -114,7 +126,7 @@ added: v0.3.4
114126
The default [`http.globalAgent`][] that is used by [`http.request()`][] has all
115127
of these values set to their respective defaults.
116128

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.
118130

119131
```js
120132
const http = require('http');
@@ -136,7 +148,7 @@ added: v0.11.4
136148
Produces a socket/stream to be used for HTTP requests.
137149

138150
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.
140152

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

153165
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
155167
the agent when you know that it will no longer be used. Otherwise,
156168
sockets may hang open for quite a long time before the server
157169
terminates them.
@@ -164,7 +176,7 @@ added: v0.11.4
164176
* {Object}
165177

166178
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.
168180

169181
### agent.getName(options)
170182
<!-- YAML
@@ -179,8 +191,8 @@ added: v0.11.4
179191
* Returns: {String}
180192

181193
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
184196
CA, cert, ciphers, and other HTTPS/TLS-specific options that determine
185197
socket reusability.
186198

@@ -191,7 +203,7 @@ added: v0.11.7
191203

192204
* {Number}
193205

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

@@ -224,7 +236,7 @@ added: v0.3.6
224236
* {Object}
225237

226238
An object which contains arrays of sockets currently in use by the
227-
Agent. Do not modify.
239+
agent. Do not modify.
228240

229241
## Class: http.ClientRequest
230242
<!-- YAML
@@ -652,7 +664,7 @@ added: v0.1.0
652664
* `response` {http.ServerResponse}
653665

654666
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).
656668

657669
### Event: 'upgrade'
658670
<!-- YAML
@@ -1489,7 +1501,7 @@ added: v0.5.9
14891501

14901502
* {http.Agent}
14911503

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
14931505
requests.
14941506

14951507
## http.request(options[, callback])
@@ -1519,15 +1531,13 @@ added: v0.3.6
15191531
* `headers` {Object} An object containing request headers.
15201532
* `auth` {String} Basic authentication i.e. `'user:password'` to compute an
15211533
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:
15241535
* `undefined` (default): use [`http.globalAgent`][] for this host and port.
15251536
* `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.
15281538
* `createConnection` {Function} A function that produces a socket/stream to
15291539
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
15311541
`createConnection` function. See [`agent.createConnection()`][] for more
15321542
details.
15331543
* `timeout` {Integer}: A number specifying the socket timeout in milliseconds.
@@ -1648,3 +1658,4 @@ There are a few special headers that should be noted.
16481658
[constructor options]: #http_new_agent_options
16491659
[Readable Stream]: stream.html#stream_class_stream_readable
16501660
[Writable Stream]: stream.html#stream_class_stream_writable
1661+
[socket.unref()]: net.html#net_socket_unref

0 commit comments

Comments
 (0)