@tramvai/module-dns-cache 2.119.2 → 2.119.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/server.es.js +64 -3
- package/lib/server.js +65 -3
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ First main optimization of HTTP connections is already used in `tramvai` [HTTP c
|
|
|
13
13
|
|
|
14
14
|
Because of that, DNS cache it's a nice and cheap optimization, but don't expect some huge performance boost.
|
|
15
15
|
|
|
16
|
-
Potential disadvantages - external services can change their IP addresses, and HTTP requests will use outdated DNS cache, you can detect this by search `
|
|
16
|
+
Potential disadvantages - external services can change their IP addresses, and HTTP requests will use outdated DNS cache, you can detect some of this errors by search `ECONNRESET` errors is server logs, so long cache TTL is not recommended.
|
|
17
17
|
|
|
18
18
|
## Installation
|
|
19
19
|
|
package/lib/server.es.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import noop from '@tinkoff/utils/function/noop';
|
|
2
|
+
import monkeypatch from '@tinkoff/monkeypatch';
|
|
2
3
|
import http from 'http';
|
|
3
4
|
import https from 'https';
|
|
4
5
|
import dns from 'dns';
|
|
5
6
|
import CacheableLookup from 'cacheable-lookup';
|
|
6
7
|
import { createToken, declareModule, provide, commandLineListTokens, Scope } from '@tramvai/core';
|
|
7
8
|
import { ENV_MANAGER_TOKEN, CREATE_CACHE_TOKEN, ENV_USED_TOKEN } from '@tramvai/tokens-common';
|
|
9
|
+
import { getUrlAndOptions } from '@tramvai/module-metrics';
|
|
8
10
|
|
|
9
11
|
const DNS_LOOKUP_CACHE_TOKEN = createToken('dnsLookupCache');
|
|
10
12
|
const TramvaiDnsCacheModule = declareModule({
|
|
@@ -18,7 +20,7 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
18
20
|
if (envManager.get('DNS_LOOKUP_CACHE_ENABLE') !== 'true') {
|
|
19
21
|
return noop;
|
|
20
22
|
}
|
|
21
|
-
return ()
|
|
23
|
+
return function addDnsLookupCache() {
|
|
22
24
|
const maxTtl = Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
23
25
|
const cacheable = new CacheableLookup({
|
|
24
26
|
cache,
|
|
@@ -34,8 +36,67 @@ const TramvaiDnsCacheModule = declareModule({
|
|
|
34
36
|
}
|
|
35
37
|
originalLookup.call(cacheable, hostname, options, callback);
|
|
36
38
|
};
|
|
37
|
-
cacheable.install
|
|
38
|
-
|
|
39
|
+
// cacheable.install method is not working for http.Agent.prototype and https.Agent.prototype,
|
|
40
|
+
// and is used on globalAgent - cover only requests with default agent, and not cover tramvai http clients
|
|
41
|
+
// @ts-expect-error
|
|
42
|
+
const originalHttpCreateConnection = http.Agent.prototype.createConnection;
|
|
43
|
+
// @ts-expect-error
|
|
44
|
+
http.Agent.prototype.createConnection = function createDnsCachedConnection(options, callback) {
|
|
45
|
+
if (!('lookup' in options)) {
|
|
46
|
+
// eslint-disable-next-line no-param-reassign
|
|
47
|
+
options.lookup = cacheable.lookup;
|
|
48
|
+
}
|
|
49
|
+
return originalHttpCreateConnection.call(this, options, callback);
|
|
50
|
+
};
|
|
51
|
+
// @ts-expect-error
|
|
52
|
+
const originalHttpsCreateConnection = https.Agent.prototype.createConnection;
|
|
53
|
+
// @ts-expect-error
|
|
54
|
+
https.Agent.prototype.createConnection = function createDnsCachedConnection(options, callback) {
|
|
55
|
+
if (!('lookup' in options)) {
|
|
56
|
+
// eslint-disable-next-line no-param-reassign
|
|
57
|
+
options.lookup = cacheable.lookup;
|
|
58
|
+
}
|
|
59
|
+
return originalHttpsCreateConnection.call(this, options, callback);
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
deps: {
|
|
64
|
+
envManager: ENV_MANAGER_TOKEN,
|
|
65
|
+
cache: DNS_LOOKUP_CACHE_TOKEN,
|
|
66
|
+
},
|
|
67
|
+
}),
|
|
68
|
+
provide({
|
|
69
|
+
provide: commandLineListTokens.init,
|
|
70
|
+
multi: true,
|
|
71
|
+
useFactory: ({ envManager, cache }) => {
|
|
72
|
+
if (envManager.get('DNS_LOOKUP_CACHE_ENABLE') !== 'true') {
|
|
73
|
+
return noop;
|
|
74
|
+
}
|
|
75
|
+
// if dns cache is stale, we can get any error, so it's safe to always clear dns cache on request error,
|
|
76
|
+
// except aborted requests and timeouts - this errors shoud be expected
|
|
77
|
+
return function addPossibleStaleDnsErrorsHandler() {
|
|
78
|
+
function handlePossibleStaleDnsErrors(originalReq, ...args) {
|
|
79
|
+
// @ts-expect-error
|
|
80
|
+
const req = originalReq.apply(this, args);
|
|
81
|
+
req.on('error', (e) => {
|
|
82
|
+
if ((e === null || e === void 0 ? void 0 : e.type) === 'aborted' || (e === null || e === void 0 ? void 0 : e.code) === 'ETIMEDOUT') {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const [_, __, url] = getUrlAndOptions(args);
|
|
86
|
+
cache.delete(url === null || url === void 0 ? void 0 : url.hostname);
|
|
87
|
+
});
|
|
88
|
+
return req;
|
|
89
|
+
}
|
|
90
|
+
monkeypatch({
|
|
91
|
+
obj: http,
|
|
92
|
+
method: 'request',
|
|
93
|
+
handler: handlePossibleStaleDnsErrors,
|
|
94
|
+
});
|
|
95
|
+
monkeypatch({
|
|
96
|
+
obj: https,
|
|
97
|
+
method: 'request',
|
|
98
|
+
handler: handlePossibleStaleDnsErrors,
|
|
99
|
+
});
|
|
39
100
|
};
|
|
40
101
|
},
|
|
41
102
|
deps: {
|
package/lib/server.js
CHANGED
|
@@ -3,16 +3,19 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var noop = require('@tinkoff/utils/function/noop');
|
|
6
|
+
var monkeypatch = require('@tinkoff/monkeypatch');
|
|
6
7
|
var http = require('http');
|
|
7
8
|
var https = require('https');
|
|
8
9
|
var dns = require('dns');
|
|
9
10
|
var CacheableLookup = require('cacheable-lookup');
|
|
10
11
|
var core = require('@tramvai/core');
|
|
11
12
|
var tokensCommon = require('@tramvai/tokens-common');
|
|
13
|
+
var moduleMetrics = require('@tramvai/module-metrics');
|
|
12
14
|
|
|
13
15
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
14
16
|
|
|
15
17
|
var noop__default = /*#__PURE__*/_interopDefaultLegacy(noop);
|
|
18
|
+
var monkeypatch__default = /*#__PURE__*/_interopDefaultLegacy(monkeypatch);
|
|
16
19
|
var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
|
|
17
20
|
var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
|
|
18
21
|
var dns__default = /*#__PURE__*/_interopDefaultLegacy(dns);
|
|
@@ -30,7 +33,7 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
30
33
|
if (envManager.get('DNS_LOOKUP_CACHE_ENABLE') !== 'true') {
|
|
31
34
|
return noop__default["default"];
|
|
32
35
|
}
|
|
33
|
-
return ()
|
|
36
|
+
return function addDnsLookupCache() {
|
|
34
37
|
const maxTtl = Number(envManager.get('DNS_LOOKUP_CACHE_TTL'));
|
|
35
38
|
const cacheable = new CacheableLookup__default["default"]({
|
|
36
39
|
cache,
|
|
@@ -46,8 +49,67 @@ const TramvaiDnsCacheModule = core.declareModule({
|
|
|
46
49
|
}
|
|
47
50
|
originalLookup.call(cacheable, hostname, options, callback);
|
|
48
51
|
};
|
|
49
|
-
cacheable.install
|
|
50
|
-
|
|
52
|
+
// cacheable.install method is not working for http.Agent.prototype and https.Agent.prototype,
|
|
53
|
+
// and is used on globalAgent - cover only requests with default agent, and not cover tramvai http clients
|
|
54
|
+
// @ts-expect-error
|
|
55
|
+
const originalHttpCreateConnection = http__default["default"].Agent.prototype.createConnection;
|
|
56
|
+
// @ts-expect-error
|
|
57
|
+
http__default["default"].Agent.prototype.createConnection = function createDnsCachedConnection(options, callback) {
|
|
58
|
+
if (!('lookup' in options)) {
|
|
59
|
+
// eslint-disable-next-line no-param-reassign
|
|
60
|
+
options.lookup = cacheable.lookup;
|
|
61
|
+
}
|
|
62
|
+
return originalHttpCreateConnection.call(this, options, callback);
|
|
63
|
+
};
|
|
64
|
+
// @ts-expect-error
|
|
65
|
+
const originalHttpsCreateConnection = https__default["default"].Agent.prototype.createConnection;
|
|
66
|
+
// @ts-expect-error
|
|
67
|
+
https__default["default"].Agent.prototype.createConnection = function createDnsCachedConnection(options, callback) {
|
|
68
|
+
if (!('lookup' in options)) {
|
|
69
|
+
// eslint-disable-next-line no-param-reassign
|
|
70
|
+
options.lookup = cacheable.lookup;
|
|
71
|
+
}
|
|
72
|
+
return originalHttpsCreateConnection.call(this, options, callback);
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
deps: {
|
|
77
|
+
envManager: tokensCommon.ENV_MANAGER_TOKEN,
|
|
78
|
+
cache: DNS_LOOKUP_CACHE_TOKEN,
|
|
79
|
+
},
|
|
80
|
+
}),
|
|
81
|
+
core.provide({
|
|
82
|
+
provide: core.commandLineListTokens.init,
|
|
83
|
+
multi: true,
|
|
84
|
+
useFactory: ({ envManager, cache }) => {
|
|
85
|
+
if (envManager.get('DNS_LOOKUP_CACHE_ENABLE') !== 'true') {
|
|
86
|
+
return noop__default["default"];
|
|
87
|
+
}
|
|
88
|
+
// if dns cache is stale, we can get any error, so it's safe to always clear dns cache on request error,
|
|
89
|
+
// except aborted requests and timeouts - this errors shoud be expected
|
|
90
|
+
return function addPossibleStaleDnsErrorsHandler() {
|
|
91
|
+
function handlePossibleStaleDnsErrors(originalReq, ...args) {
|
|
92
|
+
// @ts-expect-error
|
|
93
|
+
const req = originalReq.apply(this, args);
|
|
94
|
+
req.on('error', (e) => {
|
|
95
|
+
if ((e === null || e === void 0 ? void 0 : e.type) === 'aborted' || (e === null || e === void 0 ? void 0 : e.code) === 'ETIMEDOUT') {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const [_, __, url] = moduleMetrics.getUrlAndOptions(args);
|
|
99
|
+
cache.delete(url === null || url === void 0 ? void 0 : url.hostname);
|
|
100
|
+
});
|
|
101
|
+
return req;
|
|
102
|
+
}
|
|
103
|
+
monkeypatch__default["default"]({
|
|
104
|
+
obj: http__default["default"],
|
|
105
|
+
method: 'request',
|
|
106
|
+
handler: handlePossibleStaleDnsErrors,
|
|
107
|
+
});
|
|
108
|
+
monkeypatch__default["default"]({
|
|
109
|
+
obj: https__default["default"],
|
|
110
|
+
method: 'request',
|
|
111
|
+
handler: handlePossibleStaleDnsErrors,
|
|
112
|
+
});
|
|
51
113
|
};
|
|
52
114
|
},
|
|
53
115
|
deps: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-dns-cache",
|
|
3
|
-
"version": "2.119.
|
|
3
|
+
"version": "2.119.4",
|
|
4
4
|
"description": "DNS lookup cache",
|
|
5
5
|
"browser": "lib/browser.js",
|
|
6
6
|
"main": "lib/server.js",
|
|
@@ -24,8 +24,10 @@
|
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@tinkoff/utils": "^2.1.2",
|
|
27
|
-
"@tramvai/core": "2.119.
|
|
28
|
-
"@tramvai/
|
|
27
|
+
"@tramvai/core": "2.119.4",
|
|
28
|
+
"@tramvai/module-metrics": "2.119.4",
|
|
29
|
+
"@tramvai/tokens-common": "2.119.4",
|
|
30
|
+
"@tinkoff/monkeypatch": "2.0.5",
|
|
29
31
|
"cacheable-lookup": "^7.0.0"
|
|
30
32
|
},
|
|
31
33
|
"devDependencies": {},
|