Skip to content

Commit

Permalink
Merge pull request #378 from Al-Kostryukov/master
Browse files Browse the repository at this point in the history
Docs: clarify connection keep alive behaviour
  • Loading branch information
tomas authored Oct 2, 2021
2 parents b4913a5 + 1eaa917 commit 9a78d09
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,16 @@ On older versions, however, this has the unwanted behaviour of preventing the ru

So if you're stuck on 0.10 or even lower and want full speed, you can simply set the Connection header to 'Keep-Alive' by using `{ connection: 'Keep-Alive' }`. Please note, though, that an event loop handler will prevent the runtime from exiting so you'll need to manually call `process.exit()` or the universe will collapse.

By default, Node uses [http.globalAgent](https://nodejs.org/api/http.html#http_http_globalagent) with `keepAlive` option set to `false` to send HTTP(s) requests. That's why, by default, "Connection: close" header is sent, and the Connection is destroyed after the request.

To keep the Connection alive, you should create `http(s).Agent` with `keepAlive: true` and pass it as request option:

```js
const keepAliveAgent = new require('https').Agent({ keepAlive: true, keepAliveMsecs: 10000 });
needle(method, url, data, { agent: keepAliveAgent })
.then(function(response) {})
```

Examples Galore
---------------

Expand Down

0 comments on commit 9a78d09

Please sign in to comment.