# electrode-keepalive

> electrode-keepalive

Latest version **0.3.11** (published 2021-09-01) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install electrode-keepalive
pnpm add electrode-keepalive
yarn add electrode-keepalive
bun add electrode-keepalive
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.11 |
| Published | 2021-09-01 |
| First published | 2016-12-16 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 4.2.2 |
| Dependencies | 2 |
| Unpacked size | 44 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Maintainers | jchip |
| Keywords | electrode, hapi, node, webserver |

## Links

- npm: https://www.npmjs.com/package/electrode-keepalive
- Repository: https://github.com/electrode-io/electrode-keepalive
- Homepage: https://github.com/electrode-io/electrode-keepalive#readme
- Issues: https://github.com/electrode-io/electrode-keepalive/issues
- npm.io page: https://npm.io/package/electrode-keepalive

## Dependencies (2)

- [humanize-ms](https://npm.io/package/humanize-ms.md) ^1.2.1
- [agentkeepalive](https://npm.io/package/agentkeepalive.md) 4.1.4

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 0.3.11 (latest) — 2021-09-01
- 0.3.10 — 2021-08-31
- 0.3.9 — 2021-08-25
- 0.3.8 — 2021-08-21
- 0.3.7 — 2021-08-17
- 0.3.5 — 2017-09-30
- 0.3.4 — 2017-09-30
- 0.3.3 — 2017-09-29
- 0.3.1 — 2017-09-29
- 0.3.0 — 2017-09-27
- 0.2.1 — 2017-05-27
- 0.2.0 — 2016-12-16
- 0.1.0 — 2016-12-16

## README

# electrode-keepalive

`electrode-keepalive` provides an keep-alive HttpAgent capable of maintaining and tracking available sockets using IP address and hostname as key. This allows for graceful handling of DNS changes or failover when routing configuration is updated. It resolves part of the problems in this [issue](https://github.com/nodejs/node/issues/6713)

It also uses the patched [agentkeepalive] to support destroying kept alive sockets within a timeout.

The big changes from [agentkeepalive] are:

1.  Do DNS lookup and use host and IP address as key to track sockets.
2.  Use LIFO instead of FIFO when reusing free sockets. This solves issue of free sockets stay around too long, because after a burst of traffic, node creates a long list of free sockets, and continuous traffic would cycle through and prevent them from expiring.

### Usage

```js
const ElectrodeKeepAlive = require("electrode-keepalive");
const keepAlive = new ElectrodeKeepAlive();
request({ url: "http://www.google.com", agent: keepAlive.agent });
```

HTTPS:

```js
const ElectrodeKeepAlive = require("electrode-keepalive");
const keepAlive = new ElectrodeKeepAlive({ https: true });
request({ url: "https://www.google.com", agent: keepAlive.agent });
```

See [contructor](#constructor) for details on options you can passed.

### Install

```bash
npm install electrode-keepalive --save
```

# APIs

## Properties

### [agent](#agent)

`instance.agent` - Returns the http/https agent. (readonly)

### [https](#https)

`instance.https` - If `true`, then the agent is an https agent. (readonly)

### [static DNS_CACHE](#static-dns_cache)

`ElectrodeKeepAlive.DNS_CACHE` - Internal DNS cache mapping object.

### [static checkExpiredDnsInterval](#static-checkexpireddnsinterval)

`ElectrodeKeepAlive.checkExpiredDnsInterval` - Interval in milliseconds to check and delete expired DNS cache entries. **Default**: `5000ms`

This is not a regular timer interval. The check only occurs when [preLookup](#prelookup) is called.

## Static Methods

### [static clearDnsCache](#static-cleardnscache)

`ElectrodeKeepAlive.clearDnsCache()` - Wipes out current DNS mapping.

### [static deleteExpiredDnsCache](#static-deleteexpireddnscache)

`ElectrodeKeepAlive.deleteExpiredDnsCache()` - delete any DNS cache entry that's expired.

## Methods

### [constructor](#constructor)

`constructor(options)`

Creates an instance of `ElectrodeKeepAlive`.

- `options` should be the default http agent [settings] that are passed through to the underlying implementation. Additionally the following options are supported:

  - `https` - If true, then creates an https Agent.
  - `expiry` - The duration (in milliseconds) that ip entries in the DNS mapping will be refreshed. **Default**: `5000ms`

- Since this uses [agentkeepalive], you can also pass in any options it supports.

  - The defaults for the following two options are set if you didn't specify them:
    - `freeSocketKeepAliveTimeout` - `30000ms`
    - `timeout` - `60000ms`

- `keepAlive` - This is always default to `true` by `ElectrodeKeepAlive`.

### [preLookup](#prelookup)

`instance.preLookup(host, options, callback)`

This method is used to do DNS lookup to translate host name into IP for tracking free sockets.
It will also populate `ElectrodeKeepAlive` DNS cache.

The arguments matches Node dns module lookup.

### [getName](#getname)

`instance.getName(options)`

The internal method to override http agent's default `getName`. Not intended for public use.

[settings]: https://nodejs.org/api/http.html#http_new_agent_options
[agentkeepalive]: https://github.com/node-modules/agentkeepalive

### [getNameAsync](#getnameasync)

`instance.getNameAsync(options, callback)`

Async version of [getName](#getname).

---
_Source: https://npm.io/package/electrode-keepalive · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
