0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
Commit Graph

41 Commits

Author SHA1 Message Date
Fedor Indutny
d6260a8f4b http: overridable keep-alive behavior of Agent
Introduce two overridable `Agent` methods:

* `keepSocketAlive(socket)`
* `reuseSocket(socket, req)`

These methods can be overridden by particular `Agent` class child to
make keep-alive behavior customizable.

Motivation: destroy persisted sockets after some configurable timeout.
It is very non-trivial to do it with available primitives. Such program
will most likely need to poke with undocumented events and methods of
`Agent`. With introduced API such behavior is easy to implement.

PR-URL: https://github.com/nodejs/node/pull/13005
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-06-07 14:41:22 -04:00
Anna Henningsen
3e02636bcb
async_hooks,http: fix socket reuse with Agent
Under very specific circumstances the `http` implementation
could be brought to crash, because the Agent did not re-assign
the async id field properly after setting up a socket for reuse.

Fixes: https://github.com/nodejs/node/issues/13325
PR-URL: https://github.com/nodejs/node/pull/13348
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
2017-06-03 22:36:51 +02:00
Trevor Norris
4a7233c178
lib: implement async_hooks API in core
Implement async_hooks support in the following:

* fatalException handler
* process.nextTick
* Timers
* net/dgram/http

PR-URL: https://github.com/nodejs/node/pull/12892
Ref: https://github.com/nodejs/node/pull/11883
Ref: https://github.com/nodejs/node/pull/8531
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
2017-05-10 22:22:29 +02:00
Brian White
e283319969
http: fix permanent deoptimizations
PR-URL: https://github.com/nodejs/node/pull/12456
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-04-30 03:17:29 -04:00
James M Snell
5425e0dcbe http: use more efficient module.exports pattern
PR-URL: https://github.com/nodejs/node/pull/11594
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2017-03-20 16:01:31 -07:00
James M Snell
98e54b0bd4 meta: restore original copyright header
A prior io.js era commit inappropriately removed the
original copyright statements from the source. This
restores those in any files still remaining from that
edit.

Ref: https://github.com/nodejs/TSC/issues/174
Ref: https://github.com/nodejs/node/pull/10599
PR-URL: https://github.com/nodejs/node/pull/10155

Note: This PR was required, reviewed-by and approved
by the Node.js Foundation Legal Committee and the TSC.
There is no `Approved-By:` meta data.
2017-03-10 11:23:48 -08:00
Sakthipriyan Vairamani (thefourtheye)
dce37dc35b
lib: remove unnecessary assignments with _extend
The first parameter to `util._extend` is the target object. Assigning
the target object to the result of `util._extend` is not necessary.

PR-URL: https://github.com/nodejs/node/pull/11364

Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
2017-02-22 10:35:25 +05:30
Evan Torrie
c04d4df08c http: eliminate capture of ClientRequest in Agent
Keepalive sockets that are returned to the agent's freesocket pool were
previously capturing a reference to the ClientRequest that initiated the
request.

This commit eliminates that by moving the installation of the socket
listeners to a different function.

PR-URL: https://github.com/nodejs/node/pull/10134
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-01-06 10:07:32 -08:00
maasencioh
cb45374c31 http: name anonymous functions
Ref: https://github.com/nodejs/node/issues/8913
PR-URL: https://github.com/nodejs/node/pull/9054
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-10-18 15:29:38 -07:00
Ilkka Myller
db5a8791fa https: fix memory leak with https.request()
If calling `https.request()` with `options.headers.host` defined
and `options.servername` undefined, `https.Agent.createSocket` mutates
connection `options` after `https.Agent.addRequest` has created empty
socket pool array with mismatching connection name. This results in two
socket pool arrays being created and only the last one gets eventually
deleted by `removeSocket` - causing a memory leak.

This commit fixes the leak by making sure that `addRequest` does the
same modifications to `options` object as the `createSocket`.

`createSocket` is intentionally left unmodified to prevent userland
regressions.

Test case included.

PR-URL: https://github.com/nodejs/node/pull/8647
Fixes: https://github.com/nodejs/node/issues/6687
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jackson Tian <shvyo1987@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-09-21 03:22:15 +03:00
Ben Noordhuis
fb19b800b8 tls,https: respect address family when connecting
Respect the `{ family: 6 }` address family property when connecting to
a remote peer over TLS.

Fixes: https://github.com/nodejs/node/issues/4139
Fixes: https://github.com/nodejs/node/issues/6440
PR-URL: https://github.com/nodejs/node/pull/6654
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2016-05-28 22:49:36 +02:00
Dirceu Pereira Tiegs
fe77de196c http: use localAddress instead of path
Fix `options` usage on `lib/_http_agent.js` for the Legacy API.

Fixes: https://github.com/nodejs/node/issues/5051
PR-URL: https://github.com/nodejs/node/pull/5190
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-05-19 12:01:57 +02:00
Alexander Penev
2e1ae3ead1 https: fix ssl socket leak when keepalive is used
SSL sockets leak whenever keep alive is enabled, ca option is set in
the global agent, and requests are sent without the ca property.
In the following case at Agent.prototype.createSocket a socket will
be created with a hashtag name that includes data from the global
agents’ ca property.

On subsequent requests at Agent.prototype.addRequest we do not find
the free socket, because the hashtag name generated there does not
take into account the global agents’ ca property, thus creating a new
socket and leaving the first socket to timeout. closes: #5699

PR-URL: https://github.com/nodejs/node/pull/5713
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-03-17 14:32:49 +02:00
Brian White
9bee03aaf2 http: allow async createConnection()
This commit adds support for async createConnection()
implementations and is still backwards compatible with
synchronous createConnection() implementations.

This commit also makes the http client more friendly with
generic stream objects produced by createConnection() by
checking stream.writable instead of stream.destroyed as the
latter is currently a net.Socket-ism and not set by the core
stream implementations.

PR-URL: https://github.com/nodejs/node/pull/4638
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-02-11 12:52:46 -05:00
Damian Schenkelman
8ac1ff73fb http: use self.keepAlive instead of self.options.keepAlive
In http.agent, all other options are directly accessed through
`self.` not `self.options`.

PR-URL: https://github.com/nodejs/node/pull/4407
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2015-12-30 11:08:16 -08:00
Dave
6e11e22081 http: remove excess calls to removeSocket
socket.destroy() triggers a 'close' event from the socket which triggers
the onClose handler of HTTPAgent which calls self.removeSocket(). So by
calling self.removeSocket() prior to socket.destroy() we end up with two
calls to self.removeSocket().

If there are pending requests, removeSocket ends up creating a new socket.
So if there are pending requests, each time a request completes, we tear
down one socket and create two more. So the total number of sockets grows
exponentially and without regard for any maxSockets settings. This was
noticed in https://github.com/nodejs/node/issues/4050. Let's get rid of
the extra calls to removeSocket so we only call it once per completed
request.

PR-URL: https://github.com/nodejs/node/pull/4172
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
2015-12-21 16:11:57 -05:00
Sakthipriyan Vairamani
f32a606e37 lib,src: remove usage of events.EventEmitter
The `events` module already exports `EventEmitter` constructor function
So, we don't have to use `events.EventEmitter` to access it.

Refer: https://github.com/nodejs/node/pull/2896

PR-URL: https://github.com/nodejs/node/pull/2921
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
2015-09-23 00:23:08 +05:30
Malcolm Ahoy
7d7941235a http: default Agent.getName to 'localhost'
Refactor out the if/else statement checking for option.host.
Add whitespace to make concatenation chunks more readable and
consistent with the https version of Agent.getName().

PR-URL: https://github.com/nodejs/node/pull/2825
Reviewed-By: Julian Duque <julianduquej@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2015-09-15 15:05:33 -07:00
Brendan Ashworth
2965442308 http: fix agent.getName() and add tests
This commit fixes agent.getName(), which returned an extra colon
according to the docs, and adds tests (it was previously not unit
tested).

PR-URL: https://github.com/nodejs/io.js/pull/1617
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2015-08-04 11:56:17 -07:00
Fedor Indutny
2ca5a3db47 https: reuse TLS sessions in Agent
Fix: #1499
PR-URL: https://github.com/nodejs/io.js/pull/2228
Reviewed-By: Shigeki Ohtsu <ohtsu@iij.ad.jp>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2015-07-27 11:48:36 -07:00
fengmk2
7956a13dad http: logically respect maxSockets
Allows the number of pooled free sockets to equal maxSockets.
Previously it would only allow maxSockets - 1.

PR-URL: https://github.com/iojs/io.js/pull/1242
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Christian Tellnes <christian@tellnes.no>
2015-04-17 12:20:44 -04:00
skenqbx
8453fbc879 https: don't overwrite servername option
PR-URL: https://github.com/iojs/io.js/pull/1110
Reviewed-By: Fedor Indutny <fedor@indutny.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
2015-03-10 23:28:04 -07:00
cjihrig
804e7aa9ab lib: use const to define constants
This commit replaces a number of var statements throughout
the lib code with const statements.

PR-URL: https://github.com/iojs/io.js/pull/541
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2015-01-21 16:21:31 -05:00
isaacs
3e1b1dd4a9 Remove excessive copyright/license boilerplate
The copyright and license notice is already in the LICENSE file.  There
is no justifiable reason to also require that it be included in every
file, since the individual files are not individually distributed except
as part of the entire package.
2015-01-12 15:30:28 -08:00
Ben Noordhuis
21130c7d6f lib: turn on strict mode
Turn on strict mode for the files in the lib/ directory.  It helps
catch bugs and can have a positive effect on performance.

PR-URL: https://github.com/node-forward/node/pull/64
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
2014-11-22 17:23:30 +01:00
Majid Arif Siddiqui
176f0bd3df lib: improved forEach object performance
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
2014-09-05 09:19:32 -07:00
Nathan Rajlich
f3189ace6b http: remove the circular dependency
Between `ClientRequest` and `Agent`. The circular require was doing
weird things at load time, like making the `globalAgent` property
be `undefined` from within the context of the "_http_client"
module.

Removing the circular dependency completely fixes this.

This commit effectively removes the undocumented `Agent#request()`
and `Agent#get()` functions.
2014-02-26 13:19:36 -08:00
isaacs
ea8feced87 http: force socket encoding to be null
Otherwise the string triggers an assertion error in node_http_parser.c,
line 370:

    assert(Buffer::HasInstance(args[0]) == true);

because the first argument is not a Buffer object.
2013-11-07 08:24:50 -08:00
fengmk2
1ce5db4d95 http: cleanup freeSockets when socket destroyed
If the socket was destroyed, we need to remove it from the agent's
`freeSockets` list, otherwise dead socket could be reused by new
request.
2013-11-07 11:42:41 +04:00
Nathan Rajlich
f31037ddfe http: don't pass the request options to Agent
The `options` that were being passed in before here are specific to a
single request, which kinda defeats the purpose of using an Agent in the
first place.

On a worse note, these `options` have not yet been "processed" by the
`http.ClientRequest` class, so if `port: null` is set (like it is as the
result of a `url.parse()` call), then they take preference over the
processed values since the agent's "options" get mixed in last in the
`createSocket()` function.

Fixes #6197.
Fixes #6199.
Closes #6231.
2013-09-14 12:29:48 -07:00
Brian White
6d842897c5 lib: remove unused variables and functions 2013-08-15 17:19:17 -07:00
isaacs
65f6f06a61 http: add agent.maxFreeSockets option 2013-08-15 13:54:28 -07:00
isaacs
12cd13312b http: Support legacy agent.addRequest API
There are some agent subclasses using this today.

Despite the addRequest function being undocumented internal API, it's
easy enough to just support the old signature for backwards
compatibility.
2013-08-07 10:29:15 -07:00
isaacs
32fdae2ca3 http: Fix overlooked agent.globalAgent export
Noticed by @bnoordhuis in https://github.com/joyent/node/pull/5991#discussion_r5575946
2013-08-05 12:33:19 -07:00
isaacs
22c68fdc1d src: Replace macros with util functions 2013-08-01 15:08:01 -07:00
Ben Noordhuis
0330bdf519 lib: macro-ify type checks
Increases the grep factor. Makes it easier to harmonize type checks
across the code base.
2013-07-24 21:49:35 +02:00
isaacs
49519f1217 http: Reuse more http/https Agent code 2013-07-09 22:31:11 -07:00
isaacs
40e92650bb http: Add agent.get/request methods 2013-07-09 22:31:11 -07:00
isaacs
9fc9b87472 http: Proper KeepAlive behavior
Instead of destroying sockets when there are no pending requests, put
them in a freeSockets list, and unref() them so that they do not keep
the event loop open.

Also, set the default max sockets to Infinity, to prevent the awful
surprising deadlocks that happen when more connections are made.
2013-07-09 22:31:11 -07:00
isaacs
6176e49181 http: style 2013-07-09 22:31:11 -07:00
Timothy J Fontaine
62e4f89765 http: split Agent into its own file 2013-04-17 00:08:28 +02:00