| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b9615b3 commit ab7587e
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -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 | |
@@ -104,7 +114,7 @@ added: v0.3.4 | |||
| 104 | 114 | outstanding requests, so they can be used for future requests without | |
| 105 | 115 | having to reestablish a TCP connection. Default = `false` | |
| 106 | 116 | * `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) | ||
| 108 | 118 | for TCP Keep-Alive packets. Ignored when the | |
| 109 | 119 | `keepAlive` option is `false` or `undefined`. Default = `1000`. | |
| 110 | 120 | * `maxSockets` {Number} Maximum number of sockets to allow per | |
@@ -116,7 +126,7 @@ added: v0.3.4 | |||
| 116 | 126 | The default [`http.globalAgent`][] that is used by [`http.request()`][] has all | |
| 117 | 127 | of these values set to their respective defaults. | |
| 118 | 128 | ||
| 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. | ||
| 120 | 130 | ||
| 121 | 131 | ```js | |
| 122 | 132 | const http = require('http'); | |
@@ -138,7 +148,7 @@ added: v0.11.4 | |||
| 138 | 148 | Produces a socket/stream to be used for HTTP requests. | |
| 139 | 149 | ||
| 140 | 150 | 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. | ||
| 142 | 152 | ||
| 143 | 153 | A socket/stream can be supplied in one of two ways: by returning the | |
| 144 | 154 | socket/stream from this function, or by passing the socket/stream to `callback`. | |
@@ -153,7 +163,7 @@ added: v0.11.4 | |||
| 153 | 163 | Destroy any sockets that are currently in use by the agent. | |
| 154 | 164 | ||
| 155 | 165 | 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 | ||
| 157 | 167 | the agent when you know that it will no longer be used. Otherwise, | |
| 158 | 168 | sockets may hang open for quite a long time before the server | |
| 159 | 169 | terminates them. | |
@@ -166,7 +176,7 @@ added: v0.11.4 | |||
| 166 | 176 | * {Object} | |
| 167 | 177 | ||
| 168 | 178 | 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. | ||
| 170 | 180 | ||
| 171 | 181 | ### agent.getName(options) | |
| 172 | 182 | <!-- YAML | |
@@ -181,8 +191,8 @@ added: v0.11.4 | |||
| 181 | 191 | * Returns: {String} | |
| 182 | 192 | ||
| 183 | 193 | 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 | ||
| 186 | 196 | CA, cert, ciphers, and other HTTPS/TLS-specific options that determine | |
| 187 | 197 | socket reusability. | |
| 188 | 198 | ||
@@ -193,7 +203,7 @@ added: v0.11.7 | |||
| 193 | 203 | ||
| 194 | 204 | * {Number} | |
| 195 | 205 | ||
| 196 | - By default set to 256. For Agents supporting HTTP KeepAlive, this | ||
| 206 | + By default set to 256. For agents with `keepAlive` enabled, this | ||
| 197 | 207 | sets the maximum number of sockets that will be left open in the free | |
| 198 | 208 | state. | |
| 199 | 209 | ||
@@ -226,7 +236,7 @@ added: v0.3.6 | |||
| 226 | 236 | * {Object} | |
| 227 | 237 | ||
| 228 | 238 | An object which contains arrays of sockets currently in use by the | |
| 229 | - Agent. Do not modify. | ||
| 239 | + agent. Do not modify. | ||
| 230 | 240 | ||
| 231 | 241 | ## Class: http.ClientRequest | |
| 232 | 242 | <!-- YAML | |
@@ -662,7 +672,7 @@ added: v0.1.0 | |||
| 662 | 672 | * `response` {http.ServerResponse} | |
| 663 | 673 | ||
| 664 | 674 | 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). | ||
| 666 | 676 | ||
| 667 | 677 | ### Event: 'upgrade' | |
| 668 | 678 | <!-- YAML | |
@@ -1510,7 +1520,7 @@ added: v0.5.9 | |||
| 1510 | 1520 | ||
| 1511 | 1521 | * {http.Agent} | |
| 1512 | 1522 | ||
| 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 | ||
| 1514 | 1524 | requests. | |
| 1515 | 1525 | ||
| 1516 | 1526 | ## http.request(options[, callback]) | |
@@ -1540,15 +1550,13 @@ added: v0.3.6 | |||
| 1540 | 1550 | * `headers` {Object} An object containing request headers. | |
| 1541 | 1551 | * `auth` {String} Basic authentication i.e. `'user:password'` to compute an | |
| 1542 | 1552 | 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: | ||
| 1545 | 1554 | * `undefined` (default): use [`http.globalAgent`][] for this host and port. | |
| 1546 | 1555 | * `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. | ||
| 1549 | 1557 | * `createConnection` {Function} A function that produces a socket/stream to | |
| 1550 | 1558 | 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 | ||
| 1552 | 1560 | `createConnection` function. See [`agent.createConnection()`][] for more | |
| 1553 | 1561 | details. | |
| 1554 | 1562 | * `timeout` {Integer}: A number specifying the socket timeout in milliseconds. | |
@@ -1669,3 +1677,4 @@ There are a few special headers that should be noted. | |||
| 1669 | 1677 | [constructor options]: #http_new_agent_options | |
| 1670 | 1678 | [Readable Stream]: stream.html#stream_class_stream_readable | |
| 1671 | 1679 | [Writable Stream]: stream.html#stream_class_stream_writable | |
| 1680 | + [socket.unref()]: net.html#net_socket_unref | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments