Skip to content

Commit

Permalink
feat: http keepalives (#17582)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Sep 2, 2022
1 parent 22d0d34 commit 9bc8b05
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,10 @@ Example:
}
```

### keepalive

If enabled, this allows a single TCP connection to remain open for multiple HTTP(S) requests/responses.

### matchHost

This can be a base URL (e.g. `https://api.github.com`) or a hostname like `github.com` or `api.github.com`.
Expand Down
11 changes: 11 additions & 0 deletions lib/config/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,17 @@ const options: RenovateOptions[] = [
env: false,
experimental: true,
},
{
name: 'keepalive',
description: 'Enable http keepalives for hosts',
type: 'boolean',
stage: 'repository',
parent: 'hostRules',
default: false,
cli: false,
env: false,
experimental: true,
},
{
name: 'prBodyDefinitions',
description: 'Table column definitions for use in PR tables.',
Expand Down
1 change: 1 addition & 0 deletions lib/types/host-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface HostRuleSearchResult {
concurrentRequestLimit?: number;

dnsCache?: boolean;
keepalive?: boolean;
}

export interface HostRule extends HostRuleSearchResult {
Expand Down
7 changes: 7 additions & 0 deletions lib/util/http/host-rules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ describe('util/http/host-rules', () => {
});
});

it('uses http keepalives', () => {
hostRules.add({ keepalive: true });
expect(
applyHostRules(url, { ...options, token: 'xxx' }).agent
).toBeDefined();
});

it('disables http2', () => {
process.env.HTTP_PROXY = 'http://proxy';
bootstrap();
Expand Down
5 changes: 5 additions & 0 deletions lib/util/http/host-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { hasProxy } from '../../proxy';
import type { HostRule } from '../../types';
import * as hostRules from '../host-rules';
import { dnsLookup } from './dns';
import { keepaliveAgents } from './keepalive';
import type { GotOptions } from './types';

export function findMatchingRules(options: GotOptions, url: string): HostRule {
Expand Down Expand Up @@ -109,6 +110,10 @@ export function applyHostRules(url: string, inOptions: GotOptions): GotOptions {
options.lookup = dnsLookup;
}

if (foundRules.keepalive) {
options.agent = keepaliveAgents;
}

if (!hasProxy() && foundRules.enableHttp2 === true) {
options.http2 = true;
}
Expand Down
12 changes: 12 additions & 0 deletions lib/util/http/keepalive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Agent, { HttpsAgent } from 'agentkeepalive';
import type { Agents } from 'got';

const http = new Agent();
const https = new HttpsAgent();

const keepaliveAgents: Agents = {
http,
https,
};

export { keepaliveAgents };
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
"@types/tmp": "0.2.3",
"@yarnpkg/core": "3.2.4",
"@yarnpkg/parsers": "2.5.1",
"agentkeepalive": "4.2.1",
"auth-header": "1.0.0",
"azure-devops-node-api": "11.2.0",
"bunyan": "1.8.15",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3172,7 +3172,7 @@ agent-base@6, agent-base@^6.0.2:
dependencies:
debug "4"

agentkeepalive@^4.2.1:
agentkeepalive@4.2.1, agentkeepalive@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.2.1.tgz#a7975cbb9f83b367f06c90cc51ff28fe7d499717"
integrity sha512-Zn4cw2NEqd+9fiSVWMscnjyQ1a8Yfoc5oBajLeo5w+YBHgDUcEBY2hS4YpTz6iN5f/2zQiktcuM6tS8x1p9dpA==
Expand Down

0 comments on commit 9bc8b05

Please sign in to comment.