@chainlink/external-adapter-framework 0.0.14 → 0.0.15
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/.c8rc.json +3 -0
- package/.eslintignore +10 -0
- package/.eslintrc.js +96 -0
- package/.github/README.MD +42 -0
- package/.github/actions/setup/action.yaml +13 -0
- package/.github/workflows/label.yaml +39 -0
- package/.github/workflows/main.yaml +39 -0
- package/.github/workflows/publish.yaml +17 -0
- package/.prettierignore +13 -0
- package/.yarnrc +0 -0
- package/README.md +103 -0
- package/dist/examples/coingecko/test/e2e/adapter.test.ts.js +82953 -0
- package/dist/examples/coingecko/test/integration/adapter.test.ts.js +91672 -0
- package/dist/main.js +72703 -0
- package/docker-compose.yaml +35 -0
- package/env.sh +54 -0
- package/env2.sh +55 -0
- package/jest.config.js +5 -0
- package/package.json +14 -3
- package/publish.sh +0 -0
- package/src/adapter.ts +263 -0
- package/src/background-executor.ts +52 -0
- package/src/cache/factory.ts +26 -0
- package/src/cache/index.ts +258 -0
- package/src/cache/local.ts +73 -0
- package/src/cache/metrics.ts +112 -0
- package/src/cache/redis.ts +93 -0
- package/src/config/index.ts +517 -0
- package/src/config/provider-limits.ts +127 -0
- package/src/examples/bank-frick/README.MD +10 -0
- package/src/examples/bank-frick/accounts.ts +246 -0
- package/src/examples/bank-frick/config/index.ts +53 -0
- package/src/examples/bank-frick/index.ts +13 -0
- package/src/examples/bank-frick/types.d.ts +38 -0
- package/src/examples/bank-frick/util.ts +55 -0
- package/src/examples/coingecko/src/config/index.ts +12 -0
- package/src/examples/coingecko/src/config/overrides.json +10826 -0
- package/src/examples/coingecko/src/cryptoUtils.ts +88 -0
- package/src/examples/coingecko/src/endpoint/coins.ts +54 -0
- package/src/examples/coingecko/src/endpoint/crypto-marketcap.ts +66 -0
- package/src/examples/coingecko/src/endpoint/crypto-volume.ts +66 -0
- package/src/examples/coingecko/src/endpoint/crypto.ts +63 -0
- package/src/examples/coingecko/src/endpoint/dominance.ts +40 -0
- package/src/examples/coingecko/src/endpoint/global-marketcap.ts +40 -0
- package/src/examples/coingecko/src/endpoint/index.ts +6 -0
- package/src/examples/coingecko/src/globalUtils.ts +78 -0
- package/src/examples/coingecko/src/index.ts +17 -0
- package/src/examples/coingecko/test/e2e/adapter.test.ts +278 -0
- package/src/examples/coingecko/test/integration/__snapshots__/adapter.test.ts.snap +15 -0
- package/src/examples/coingecko/test/integration/adapter.test.ts +281 -0
- package/src/examples/coingecko/test/integration/capturedRequests.json +1 -0
- package/src/examples/coingecko/test/integration/fixtures.ts +42 -0
- package/src/examples/coingecko-old/batch-warming.ts +79 -0
- package/src/examples/coingecko-old/index.ts +9 -0
- package/src/examples/coingecko-old/rest.ts +77 -0
- package/src/examples/ncfx/config/index.ts +12 -0
- package/src/examples/ncfx/index.ts +9 -0
- package/src/examples/ncfx/websocket.ts +99 -0
- package/src/index.ts +149 -0
- package/src/metrics/constants.ts +23 -0
- package/src/metrics/index.ts +115 -0
- package/src/metrics/util.ts +18 -0
- package/src/rate-limiting/background/fixed-frequency.ts +45 -0
- package/src/rate-limiting/index.ts +100 -0
- package/src/rate-limiting/metrics.ts +18 -0
- package/src/rate-limiting/request/simple-counting.ts +76 -0
- package/src/transports/batch-warming.ts +127 -0
- package/src/transports/index.ts +152 -0
- package/src/transports/metrics.ts +95 -0
- package/src/transports/rest.ts +168 -0
- package/src/transports/util.ts +63 -0
- package/src/transports/websocket.ts +245 -0
- package/src/util/index.ts +23 -0
- package/src/util/logger.ts +69 -0
- package/src/util/recordRequests.ts +47 -0
- package/src/util/request.ts +117 -0
- package/src/util/subscription-set/expiring-sorted-set.ts +54 -0
- package/src/util/subscription-set/subscription-set.ts +35 -0
- package/src/util/test-payload-loader.ts +87 -0
- package/src/validation/error.ts +116 -0
- package/src/validation/index.ts +110 -0
- package/src/validation/input-params.ts +45 -0
- package/src/validation/override-functions.ts +44 -0
- package/src/validation/overrideFunctions.ts +44 -0
- package/src/validation/preset-tokens.json +23 -0
- package/src/validation/validator.ts +384 -0
- package/test/adapter.test.ts +27 -0
- package/test/background-executor.test.ts +108 -0
- package/test/cache/cache-key.test.ts +114 -0
- package/test/cache/helper.ts +100 -0
- package/test/cache/local.test.ts +54 -0
- package/test/cache/redis.test.ts +89 -0
- package/test/correlation.test.ts +114 -0
- package/test/index.test.ts +37 -0
- package/test/metrics/feed-id.test.ts +38 -0
- package/test/metrics/helper.ts +14 -0
- package/test/metrics/labels.test.ts +36 -0
- package/test/metrics/metrics.test.ts +267 -0
- package/test/metrics/redis-metrics.test.ts +113 -0
- package/test/metrics/warmer-metrics.test.ts +193 -0
- package/test/metrics/ws-metrics.test.ts +225 -0
- package/test/rate-limit-config.test.ts +242 -0
- package/test/smoke/smoke.test.ts +166 -0
- package/test/smoke/test-payload-fail.json +3 -0
- package/test/smoke/test-payload.js +22 -0
- package/test/smoke/test-payload.json +7 -0
- package/test/transports/batch.test.ts +466 -0
- package/test/transports/rest.test.ts +242 -0
- package/test/transports/websocket.test.ts +183 -0
- package/test/tsconfig.json +5 -0
- package/test/util.ts +77 -0
- package/test/validation.test.ts +178 -0
- package/test.sh +20 -0
- package/test2.sh +2 -0
- package/tsconfig.json +28 -0
- package/typedoc.json +6 -0
- package/webpack.config.js +57 -0
- package/yarn-error.log +3778 -0
- package/adapter.d.ts +0 -107
- package/adapter.js +0 -115
- package/background-executor.d.ts +0 -11
- package/background-executor.js +0 -45
- package/cache/factory.d.ts +0 -6
- package/cache/factory.js +0 -55
- package/cache/index.d.ts +0 -94
- package/cache/index.js +0 -173
- package/cache/local.d.ts +0 -23
- package/cache/local.js +0 -83
- package/cache/metrics.d.ts +0 -27
- package/cache/metrics.js +0 -120
- package/cache/redis.d.ts +0 -16
- package/cache/redis.js +0 -100
- package/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
- package/config/index.d.ts +0 -209
- package/config/index.js +0 -380
- package/config/provider-limits.d.ts +0 -31
- package/config/provider-limits.js +0 -79
- package/examples/bank-frick/accounts.d.ts +0 -39
- package/examples/bank-frick/accounts.js +0 -191
- package/examples/bank-frick/config/index.d.ts +0 -4
- package/examples/bank-frick/config/index.js +0 -54
- package/examples/bank-frick/index.d.ts +0 -2
- package/examples/bank-frick/index.js +0 -14
- package/examples/bank-frick/util.d.ts +0 -4
- package/examples/bank-frick/util.js +0 -39
- package/examples/coingecko/batch-warming.d.ts +0 -2
- package/examples/coingecko/batch-warming.js +0 -52
- package/examples/coingecko/index.d.ts +0 -2
- package/examples/coingecko/index.js +0 -10
- package/examples/coingecko/rest.d.ts +0 -2
- package/examples/coingecko/rest.js +0 -50
- package/examples/ncfx/config/index.d.ts +0 -12
- package/examples/ncfx/config/index.js +0 -15
- package/examples/ncfx/index.d.ts +0 -2
- package/examples/ncfx/index.js +0 -10
- package/examples/ncfx/websocket.d.ts +0 -36
- package/examples/ncfx/websocket.js +0 -72
- package/index.d.ts +0 -11
- package/index.js +0 -133
- package/metrics/constants.d.ts +0 -16
- package/metrics/constants.js +0 -25
- package/metrics/index.d.ts +0 -15
- package/metrics/index.js +0 -122
- package/metrics/util.d.ts +0 -7
- package/metrics/util.js +0 -9
- package/package/adapter.d.ts +0 -88
- package/package/adapter.js +0 -112
- package/package/background-executor.d.ts +0 -11
- package/package/background-executor.js +0 -45
- package/package/cache/factory.d.ts +0 -6
- package/package/cache/factory.js +0 -57
- package/package/cache/index.d.ts +0 -90
- package/package/cache/index.js +0 -169
- package/package/cache/local.d.ts +0 -23
- package/package/cache/local.js +0 -83
- package/package/cache/metrics.d.ts +0 -27
- package/package/cache/metrics.js +0 -120
- package/package/cache/redis.d.ts +0 -16
- package/package/cache/redis.js +0 -100
- package/package/config/index.d.ts +0 -195
- package/package/config/index.js +0 -365
- package/package/config/provider-limits.d.ts +0 -31
- package/package/config/provider-limits.js +0 -76
- package/package/examples/coingecko/batch-warming.d.ts +0 -2
- package/package/examples/coingecko/batch-warming.js +0 -52
- package/package/examples/coingecko/index.d.ts +0 -2
- package/package/examples/coingecko/index.js +0 -10
- package/package/examples/coingecko/rest.d.ts +0 -2
- package/package/examples/coingecko/rest.js +0 -50
- package/package/examples/ncfx/config/index.d.ts +0 -12
- package/package/examples/ncfx/config/index.js +0 -15
- package/package/examples/ncfx/index.d.ts +0 -2
- package/package/examples/ncfx/index.js +0 -10
- package/package/examples/ncfx/websocket.d.ts +0 -36
- package/package/examples/ncfx/websocket.js +0 -72
- package/package/index.d.ts +0 -12
- package/package/index.js +0 -92
- package/package/metrics/constants.d.ts +0 -16
- package/package/metrics/constants.js +0 -25
- package/package/metrics/index.d.ts +0 -15
- package/package/metrics/index.js +0 -123
- package/package/metrics/util.d.ts +0 -3
- package/package/metrics/util.js +0 -9
- package/package/package.json +0 -72
- package/package/rate-limiting/background/fixed-frequency.d.ts +0 -10
- package/package/rate-limiting/background/fixed-frequency.js +0 -37
- package/package/rate-limiting/index.d.ts +0 -54
- package/package/rate-limiting/index.js +0 -63
- package/package/rate-limiting/metrics.d.ts +0 -3
- package/package/rate-limiting/metrics.js +0 -44
- package/package/rate-limiting/request/simple-counting.d.ts +0 -20
- package/package/rate-limiting/request/simple-counting.js +0 -62
- package/package/test.d.ts +0 -1
- package/package/test.js +0 -6
- package/package/transports/batch-warming.d.ts +0 -34
- package/package/transports/batch-warming.js +0 -101
- package/package/transports/index.d.ts +0 -87
- package/package/transports/index.js +0 -87
- package/package/transports/metrics.d.ts +0 -21
- package/package/transports/metrics.js +0 -105
- package/package/transports/rest.d.ts +0 -43
- package/package/transports/rest.js +0 -129
- package/package/transports/util.d.ts +0 -8
- package/package/transports/util.js +0 -85
- package/package/transports/websocket.d.ts +0 -80
- package/package/transports/websocket.js +0 -169
- package/package/util/expiring-sorted-set.d.ts +0 -21
- package/package/util/expiring-sorted-set.js +0 -47
- package/package/util/index.d.ts +0 -11
- package/package/util/index.js +0 -35
- package/package/util/logger.d.ts +0 -42
- package/package/util/logger.js +0 -62
- package/package/util/request.d.ts +0 -55
- package/package/util/request.js +0 -2
- package/package/validation/error.d.ts +0 -50
- package/package/validation/error.js +0 -79
- package/package/validation/index.d.ts +0 -5
- package/package/validation/index.js +0 -86
- package/package/validation/input-params.d.ts +0 -15
- package/package/validation/input-params.js +0 -30
- package/package/validation/override-functions.d.ts +0 -3
- package/package/validation/override-functions.js +0 -40
- package/package/validation/preset-tokens.json +0 -23
- package/package/validation/validator.d.ts +0 -47
- package/package/validation/validator.js +0 -303
- package/rate-limiting/background/fixed-frequency.d.ts +0 -10
- package/rate-limiting/background/fixed-frequency.js +0 -35
- package/rate-limiting/index.d.ts +0 -54
- package/rate-limiting/index.js +0 -63
- package/rate-limiting/metrics.d.ts +0 -3
- package/rate-limiting/metrics.js +0 -44
- package/rate-limiting/request/simple-counting.d.ts +0 -20
- package/rate-limiting/request/simple-counting.js +0 -62
- package/test.d.ts +0 -1
- package/test.js +0 -6
- package/transports/batch-warming.d.ts +0 -35
- package/transports/batch-warming.js +0 -101
- package/transports/index.d.ts +0 -70
- package/transports/index.js +0 -87
- package/transports/metrics.d.ts +0 -21
- package/transports/metrics.js +0 -105
- package/transports/rest.d.ts +0 -44
- package/transports/rest.js +0 -131
- package/transports/util.d.ts +0 -8
- package/transports/util.js +0 -85
- package/transports/websocket.d.ts +0 -81
- package/transports/websocket.js +0 -168
- package/util/expiring-sorted-set.d.ts +0 -21
- package/util/expiring-sorted-set.js +0 -47
- package/util/index.d.ts +0 -12
- package/util/index.js +0 -35
- package/util/logger.d.ts +0 -42
- package/util/logger.js +0 -62
- package/util/request.d.ts +0 -57
- package/util/request.js +0 -2
- package/util/subscription-set/expiring-sorted-set.d.ts +0 -22
- package/util/subscription-set/expiring-sorted-set.js +0 -47
- package/util/subscription-set/subscription-set.d.ts +0 -18
- package/util/subscription-set/subscription-set.js +0 -19
- package/util/test-payload-loader.d.ts +0 -25
- package/util/test-payload-loader.js +0 -83
- package/validation/error.d.ts +0 -50
- package/validation/error.js +0 -79
- package/validation/index.d.ts +0 -5
- package/validation/index.js +0 -91
- package/validation/input-params.d.ts +0 -15
- package/validation/input-params.js +0 -30
- package/validation/override-functions.d.ts +0 -3
- package/validation/override-functions.js +0 -40
- package/validation/preset-tokens.json +0 -23
- package/validation/validator.d.ts +0 -47
- package/validation/validator.js +0 -303
package/package/cache/index.d.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { AdapterEndpoint } from '../adapter';
|
|
2
|
-
import { AdapterConfig } from '../config';
|
|
3
|
-
import { AdapterMiddlewareBuilder, AdapterResponse, sleep } from '../util';
|
|
4
|
-
export * from './local';
|
|
5
|
-
export * from './redis';
|
|
6
|
-
/**
|
|
7
|
-
* An object describing an entry in the cache.
|
|
8
|
-
* @typeParam T - the type of the entry's value
|
|
9
|
-
*/
|
|
10
|
-
export interface CacheEntry<T> {
|
|
11
|
-
key: string;
|
|
12
|
-
value: T;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Generic interface for a local or remote Cache.
|
|
16
|
-
* @typeParam T - the type of the cache entries' values
|
|
17
|
-
*/
|
|
18
|
-
export interface Cache<T = unknown> {
|
|
19
|
-
/**
|
|
20
|
-
* Gets an item from the Cache.
|
|
21
|
-
*
|
|
22
|
-
* @param key - the key of the desired entry for which to fetch its value
|
|
23
|
-
* @returns a Promise of the entry's value, or undefined if not found / expired.
|
|
24
|
-
*/
|
|
25
|
-
get: (key: string) => Promise<T | undefined>;
|
|
26
|
-
/**
|
|
27
|
-
* Sets an item in the Cache.
|
|
28
|
-
*
|
|
29
|
-
* @param key - the key of the new entry
|
|
30
|
-
* @param value - the value of the new entry
|
|
31
|
-
* @param ttl - the time in milliseconds until the entry expires
|
|
32
|
-
* @returns an empty Promise that resolves when the entry has been set
|
|
33
|
-
*/
|
|
34
|
-
set: (key: string, value: T, ttl: number) => Promise<void>;
|
|
35
|
-
/**
|
|
36
|
-
* Sets a list of items in the Cache.
|
|
37
|
-
*
|
|
38
|
-
* @param entries - a list of cache entries
|
|
39
|
-
* @param ttl - the time in milliseconds until the entries expire
|
|
40
|
-
* @returns an empty Promise that resolves when all entries have been set
|
|
41
|
-
*/
|
|
42
|
-
setMany: (entries: CacheEntry<T>[], ttl: number) => Promise<void>;
|
|
43
|
-
/**
|
|
44
|
-
* Deletes the specified item from the Cache
|
|
45
|
-
*
|
|
46
|
-
* @param key - the key of the entry to be deleted
|
|
47
|
-
* @returns an empty Promise that resolves when the entry has been deleted
|
|
48
|
-
*/
|
|
49
|
-
delete: (key: string) => Promise<void>;
|
|
50
|
-
}
|
|
51
|
-
export declare const calculateCacheKey: ({ adapterEndpoint, adapterConfig, }: {
|
|
52
|
-
adapterEndpoint: AdapterEndpoint;
|
|
53
|
-
adapterConfig: AdapterConfig;
|
|
54
|
-
}, data: unknown) => string;
|
|
55
|
-
export declare const calculateFeedId: (adapterEndpoint: AdapterEndpoint, data: unknown) => string;
|
|
56
|
-
/**
|
|
57
|
-
* Calculates a unique key from the provided data.
|
|
58
|
-
*
|
|
59
|
-
* @param data - the request data/body, i.e. the adapter input params
|
|
60
|
-
* @param paramNames - the keys from adapter endpoint input parameters
|
|
61
|
-
* @returns the calculated unique key
|
|
62
|
-
*
|
|
63
|
-
* @example
|
|
64
|
-
* ```
|
|
65
|
-
* calculateKey({ base: 'ETH', quote: 'BTC' }, ['base','quote'])
|
|
66
|
-
* // equals `|base:eth|quote:btc`
|
|
67
|
-
* ```
|
|
68
|
-
*/
|
|
69
|
-
export declare const calculateKey: (data: unknown, paramNames: string[]) => string;
|
|
70
|
-
/**
|
|
71
|
-
* Polls the provided Cache for an AdapterResponse set in the provided key. If the maximum
|
|
72
|
-
* amount of retries is exceeded, it returns undefined instead.
|
|
73
|
-
*
|
|
74
|
-
* @param cache - a Cache instance
|
|
75
|
-
* @param key - the key generated from an AdapterRequest that corresponds to the desired AdapterResponse
|
|
76
|
-
* @param retry - current retry, only for internal use
|
|
77
|
-
* @returns the AdapterResponse if found, else undefined
|
|
78
|
-
*/
|
|
79
|
-
export declare const pollResponseFromCache: (cache: Cache<AdapterResponse>, key: string, options: {
|
|
80
|
-
maxRetries: number;
|
|
81
|
-
sleep: number;
|
|
82
|
-
}, retry?: number) => Promise<AdapterResponse | undefined>;
|
|
83
|
-
/**
|
|
84
|
-
* Given a Cache instance in the adapter dependencies, builds a middleware function that will perform
|
|
85
|
-
* a get from said Cache and return that if found; otherwise it'll continue the middleware chain.
|
|
86
|
-
*
|
|
87
|
-
* @param adapter - an initialized adapter
|
|
88
|
-
* @returns the cache middleware function
|
|
89
|
-
*/
|
|
90
|
-
export declare const buildCacheMiddleware: AdapterMiddlewareBuilder;
|
package/package/cache/index.js
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.buildCacheMiddleware = exports.pollResponseFromCache = exports.calculateKey = exports.calculateFeedId = exports.calculateCacheKey = void 0;
|
|
30
|
-
const util_1 = require("../util");
|
|
31
|
-
const cacheMetrics = __importStar(require("./metrics"));
|
|
32
|
-
__exportStar(require("./local"), exports);
|
|
33
|
-
__exportStar(require("./redis"), exports);
|
|
34
|
-
const logger = (0, util_1.makeLogger)('Cache');
|
|
35
|
-
// Uses calculateKey to generate a unique key from the endpoint name, data, and input parameters
|
|
36
|
-
const calculateCacheKey = ({ adapterEndpoint, adapterConfig, }, data) => {
|
|
37
|
-
const paramNames = Object.keys(adapterEndpoint.inputParameters);
|
|
38
|
-
if (paramNames.length === 0) {
|
|
39
|
-
logger.trace(`Using default cache key ${adapterConfig.DEFAULT_CACHE_KEY}`);
|
|
40
|
-
return adapterConfig.DEFAULT_CACHE_KEY;
|
|
41
|
-
}
|
|
42
|
-
return `${adapterEndpoint.name}-${(0, exports.calculateKey)(data, paramNames)}`;
|
|
43
|
-
};
|
|
44
|
-
exports.calculateCacheKey = calculateCacheKey;
|
|
45
|
-
const calculateFeedId = (adapterEndpoint, data) => {
|
|
46
|
-
const paramNames = Object.keys(adapterEndpoint.inputParameters);
|
|
47
|
-
if (paramNames.length === 0) {
|
|
48
|
-
logger.trace(`Cannot generate Feed ID without input parameters`);
|
|
49
|
-
return 'N/A';
|
|
50
|
-
}
|
|
51
|
-
return (0, exports.calculateKey)(data, paramNames);
|
|
52
|
-
};
|
|
53
|
-
exports.calculateFeedId = calculateFeedId;
|
|
54
|
-
/**
|
|
55
|
-
* Calculates a unique key from the provided data.
|
|
56
|
-
*
|
|
57
|
-
* @param data - the request data/body, i.e. the adapter input params
|
|
58
|
-
* @param paramNames - the keys from adapter endpoint input parameters
|
|
59
|
-
* @returns the calculated unique key
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* ```
|
|
63
|
-
* calculateKey({ base: 'ETH', quote: 'BTC' }, ['base','quote'])
|
|
64
|
-
* // equals `|base:eth|quote:btc`
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
const calculateKey = (data, paramNames) => {
|
|
68
|
-
if (data && typeof data !== 'object') {
|
|
69
|
-
throw new Error('Data to calculate cache key should be an object');
|
|
70
|
-
}
|
|
71
|
-
const params = data;
|
|
72
|
-
let cacheKey = ''; // TODO: Maybe there's a faster String Builder
|
|
73
|
-
for (const paramName of paramNames) {
|
|
74
|
-
// Ignore overrides param when generating cache keys
|
|
75
|
-
// TODO: expand support for ignoring `includes` and `tokenOverrides` params when generating keys
|
|
76
|
-
if (paramName === 'overrides') {
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
const param = params[paramName];
|
|
80
|
-
if (param === undefined) {
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
83
|
-
cacheKey += `|${paramName}:`;
|
|
84
|
-
switch (typeof param) {
|
|
85
|
-
case 'string':
|
|
86
|
-
cacheKey += param.toLowerCase();
|
|
87
|
-
break;
|
|
88
|
-
case 'number':
|
|
89
|
-
case 'boolean':
|
|
90
|
-
cacheKey += param.toString();
|
|
91
|
-
break;
|
|
92
|
-
case 'object':
|
|
93
|
-
// TODO: Implement object sorting for deterministic cache key generation
|
|
94
|
-
cacheKey += JSON.stringify(param);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
// TODO: Check max size, potential issue
|
|
98
|
-
logger.trace(`Generated cache key for request: "${cacheKey}"`);
|
|
99
|
-
return cacheKey;
|
|
100
|
-
};
|
|
101
|
-
exports.calculateKey = calculateKey;
|
|
102
|
-
// Calculate the amount of time the non-expired entry has been in the cache
|
|
103
|
-
const calculateStaleness = (expirationTimestamp, ttl) => {
|
|
104
|
-
if (expirationTimestamp) {
|
|
105
|
-
const createTimestamp = expirationTimestamp - ttl;
|
|
106
|
-
return (Date.now() - createTimestamp) / 1000;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
// If expirationTimestamp is not available, staleness cannot be calculated
|
|
110
|
-
// Defaults to ttl for metrics purposes
|
|
111
|
-
return ttl;
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
/**
|
|
115
|
-
* Polls the provided Cache for an AdapterResponse set in the provided key. If the maximum
|
|
116
|
-
* amount of retries is exceeded, it returns undefined instead.
|
|
117
|
-
*
|
|
118
|
-
* @param cache - a Cache instance
|
|
119
|
-
* @param key - the key generated from an AdapterRequest that corresponds to the desired AdapterResponse
|
|
120
|
-
* @param retry - current retry, only for internal use
|
|
121
|
-
* @returns the AdapterResponse if found, else undefined
|
|
122
|
-
*/
|
|
123
|
-
const pollResponseFromCache = async (cache, key, options, retry = 0) => {
|
|
124
|
-
if (retry > options.maxRetries) {
|
|
125
|
-
// Ideally this shouldn't happen often (p99 of reqs should be found in the cache)
|
|
126
|
-
logger.info('Exceeded max cache polling retries');
|
|
127
|
-
return undefined;
|
|
128
|
-
}
|
|
129
|
-
logger.trace('Getting response from cache...');
|
|
130
|
-
const response = await cache.get(key);
|
|
131
|
-
if (response) {
|
|
132
|
-
logger.trace('Got response from cache');
|
|
133
|
-
return response;
|
|
134
|
-
}
|
|
135
|
-
if (options.maxRetries === 0) {
|
|
136
|
-
logger.debug(`Response not found, retries disabled`);
|
|
137
|
-
return undefined;
|
|
138
|
-
}
|
|
139
|
-
logger.debug(`Response not found, sleeping ${options.sleep} milliseconds...`);
|
|
140
|
-
await (0, util_1.sleep)(options.sleep);
|
|
141
|
-
return (0, exports.pollResponseFromCache)(cache, key, options, retry + 1);
|
|
142
|
-
};
|
|
143
|
-
exports.pollResponseFromCache = pollResponseFromCache;
|
|
144
|
-
/**
|
|
145
|
-
* Given a Cache instance in the adapter dependencies, builds a middleware function that will perform
|
|
146
|
-
* a get from said Cache and return that if found; otherwise it'll continue the middleware chain.
|
|
147
|
-
*
|
|
148
|
-
* @param adapter - an initialized adapter
|
|
149
|
-
* @returns the cache middleware function
|
|
150
|
-
*/
|
|
151
|
-
const buildCacheMiddleware = (adapter) => async (req, res) => {
|
|
152
|
-
const response = await adapter.dependencies.cache.get(req.requestContext.cacheKey);
|
|
153
|
-
if (response) {
|
|
154
|
-
logger.debug('Found response from cache, sending that');
|
|
155
|
-
if (adapter.config.METRICS_ENABLED && adapter.config.EXPERIMENTAL_METRICS_ENABLED) {
|
|
156
|
-
const label = cacheMetrics.cacheMetricsLabel(req.requestContext.cacheKey, req.requestContext.meta?.metrics?.feedId || 'N/A', adapter.config.CACHE_TYPE);
|
|
157
|
-
// Record cache staleness and cache get count and value
|
|
158
|
-
const staleness = calculateStaleness(response.maxAge, adapter.config.CACHE_MAX_AGE);
|
|
159
|
-
cacheMetrics.cacheGet(label, response.result, staleness);
|
|
160
|
-
req.requestContext.meta = {
|
|
161
|
-
...req.requestContext.meta,
|
|
162
|
-
metrics: { ...req.requestContext.meta?.metrics, cacheHit: true },
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
return res.send(response);
|
|
166
|
-
}
|
|
167
|
-
logger.debug('Did not find response in cache, moving to next middleware');
|
|
168
|
-
};
|
|
169
|
-
exports.buildCacheMiddleware = buildCacheMiddleware;
|
package/package/cache/local.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Cache, CacheEntry } from './index';
|
|
2
|
-
/**
|
|
3
|
-
* Type for a value stored in a LocalCache entry.
|
|
4
|
-
*
|
|
5
|
-
* @typeParam T - the type for the entry's value
|
|
6
|
-
*/
|
|
7
|
-
export interface LocalCacheEntry<T> {
|
|
8
|
-
expirationTimestamp: number;
|
|
9
|
-
value: T;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Local implementation of a Cache. It uses a simple js Object, storing entries with both
|
|
13
|
-
* a value and an expiration timestamp. Expired entries are deleted on reads (i.e. no background gc/upkeep).
|
|
14
|
-
*
|
|
15
|
-
* @typeParam T - the type for the entries' values
|
|
16
|
-
*/
|
|
17
|
-
export declare class LocalCache<T = unknown> implements Cache<T> {
|
|
18
|
-
store: Record<string, LocalCacheEntry<T>>;
|
|
19
|
-
get(key: string): Promise<T | undefined>;
|
|
20
|
-
delete(key: string): Promise<void>;
|
|
21
|
-
set(key: string, value: T, ttl: number): Promise<void>;
|
|
22
|
-
setMany(entries: CacheEntry<T>[], ttl: number): Promise<void>;
|
|
23
|
-
}
|
package/package/cache/local.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.LocalCache = void 0;
|
|
27
|
-
const util_1 = require("../util");
|
|
28
|
-
const cacheMetrics = __importStar(require("./metrics"));
|
|
29
|
-
const logger = (0, util_1.makeLogger)('LocalCache');
|
|
30
|
-
/**
|
|
31
|
-
* Local implementation of a Cache. It uses a simple js Object, storing entries with both
|
|
32
|
-
* a value and an expiration timestamp. Expired entries are deleted on reads (i.e. no background gc/upkeep).
|
|
33
|
-
*
|
|
34
|
-
* @typeParam T - the type for the entries' values
|
|
35
|
-
*/
|
|
36
|
-
class LocalCache {
|
|
37
|
-
constructor() {
|
|
38
|
-
this.store = {};
|
|
39
|
-
}
|
|
40
|
-
async get(key) {
|
|
41
|
-
logger.trace(`Getting key ${key}`);
|
|
42
|
-
const entry = this.store[key];
|
|
43
|
-
if (!entry) {
|
|
44
|
-
logger.debug(`No entry in local cache for key "${key}", returning undefined`);
|
|
45
|
-
return undefined;
|
|
46
|
-
}
|
|
47
|
-
const expired = entry.expirationTimestamp <= Date.now();
|
|
48
|
-
if (expired) {
|
|
49
|
-
logger.debug('Entry in local cache expired, deleting and returning undefined');
|
|
50
|
-
this.delete(key);
|
|
51
|
-
return undefined;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
logger.debug('Found valid entry in local cache, returning value');
|
|
55
|
-
return entry.value;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
async delete(key) {
|
|
59
|
-
logger.trace(`Deleting key ${key}`);
|
|
60
|
-
delete this.store[key]; // Deletes are slower than ignoring or setting null, fyi
|
|
61
|
-
}
|
|
62
|
-
async set(key, value, ttl) {
|
|
63
|
-
logger.trace(`Setting key ${key} with ttl ${ttl}`);
|
|
64
|
-
this.store[key] = {
|
|
65
|
-
value,
|
|
66
|
-
expirationTimestamp: Date.now() + ttl,
|
|
67
|
-
};
|
|
68
|
-
// Only record metrics if feed Id is present, otherwise assuming value is not adapter response to record
|
|
69
|
-
const feedId = value.meta?.metrics?.feedId;
|
|
70
|
-
if (feedId) {
|
|
71
|
-
// Record cache set count, max age, and staleness (set to 0 for cache set)
|
|
72
|
-
const label = cacheMetrics.cacheMetricsLabel(key, feedId, cacheMetrics.CacheTypes.Local);
|
|
73
|
-
cacheMetrics.cacheSet(label, ttl);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
async setMany(entries, ttl) {
|
|
77
|
-
logger.trace(`Setting a bunch of keys with ttl ${ttl}`);
|
|
78
|
-
for (const { key, value } of entries) {
|
|
79
|
-
this.set(key, value, ttl);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
exports.LocalCache = LocalCache;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import * as client from 'prom-client';
|
|
2
|
-
interface CacheMetricsLabels {
|
|
3
|
-
participant_id: string;
|
|
4
|
-
feed_id: string;
|
|
5
|
-
cache_type: string;
|
|
6
|
-
}
|
|
7
|
-
export declare const cacheGet: (label: CacheMetricsLabels, value: unknown, staleness: number) => void;
|
|
8
|
-
export declare const cacheSet: (label: CacheMetricsLabels, maxAge: number) => void;
|
|
9
|
-
export declare const cacheMetricsLabel: (cacheKey: string, feedId: string, cacheType: string) => {
|
|
10
|
-
participant_id: string;
|
|
11
|
-
feed_id: string;
|
|
12
|
-
cache_type: string;
|
|
13
|
-
};
|
|
14
|
-
export declare enum CacheTypes {
|
|
15
|
-
Redis = "redis",
|
|
16
|
-
Local = "local"
|
|
17
|
-
}
|
|
18
|
-
export declare enum CMD_SENT_STATUS {
|
|
19
|
-
TIMEOUT = "TIMEOUT",
|
|
20
|
-
FAIL = "FAIL",
|
|
21
|
-
SUCCESS = "SUCCESS"
|
|
22
|
-
}
|
|
23
|
-
export declare const redisConnectionsOpen: client.Counter<string>;
|
|
24
|
-
export declare const redisRetriesCount: client.Counter<string>;
|
|
25
|
-
export declare const redisCommandsSentCount: client.Counter<"status" | "function_name">;
|
|
26
|
-
export declare const cacheWarmerCount: client.Gauge<"isBatched">;
|
|
27
|
-
export {};
|
package/package/cache/metrics.js
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.cacheWarmerCount = exports.redisCommandsSentCount = exports.redisRetriesCount = exports.redisConnectionsOpen = exports.CMD_SENT_STATUS = exports.CacheTypes = exports.cacheMetricsLabel = exports.cacheSet = exports.cacheGet = void 0;
|
|
27
|
-
const client = __importStar(require("prom-client"));
|
|
28
|
-
const cacheGet = (label, value, staleness) => {
|
|
29
|
-
if (typeof value === 'number' || typeof value === 'string') {
|
|
30
|
-
const parsedValue = Number(value);
|
|
31
|
-
if (!Number.isNaN(parsedValue) && Number.isFinite(parsedValue)) {
|
|
32
|
-
cacheDataGetValues.labels(label).set(parsedValue);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
cacheDataGetCount.labels(label).inc();
|
|
36
|
-
cacheDataStalenessSeconds.labels(label).set(staleness);
|
|
37
|
-
};
|
|
38
|
-
exports.cacheGet = cacheGet;
|
|
39
|
-
const cacheSet = (label, maxAge) => {
|
|
40
|
-
cacheDataSetCount.labels(label).inc();
|
|
41
|
-
cacheDataMaxAge.labels(label).set(maxAge);
|
|
42
|
-
cacheDataStalenessSeconds.labels(label).set(0);
|
|
43
|
-
};
|
|
44
|
-
exports.cacheSet = cacheSet;
|
|
45
|
-
const cacheMetricsLabel = (cacheKey, feedId, cacheType) => ({
|
|
46
|
-
participant_id: cacheKey,
|
|
47
|
-
feed_id: feedId,
|
|
48
|
-
cache_type: cacheType,
|
|
49
|
-
});
|
|
50
|
-
exports.cacheMetricsLabel = cacheMetricsLabel;
|
|
51
|
-
var CacheTypes;
|
|
52
|
-
(function (CacheTypes) {
|
|
53
|
-
CacheTypes["Redis"] = "redis";
|
|
54
|
-
CacheTypes["Local"] = "local";
|
|
55
|
-
})(CacheTypes = exports.CacheTypes || (exports.CacheTypes = {}));
|
|
56
|
-
var CMD_SENT_STATUS;
|
|
57
|
-
(function (CMD_SENT_STATUS) {
|
|
58
|
-
CMD_SENT_STATUS["TIMEOUT"] = "TIMEOUT";
|
|
59
|
-
CMD_SENT_STATUS["FAIL"] = "FAIL";
|
|
60
|
-
CMD_SENT_STATUS["SUCCESS"] = "SUCCESS";
|
|
61
|
-
})(CMD_SENT_STATUS = exports.CMD_SENT_STATUS || (exports.CMD_SENT_STATUS = {}));
|
|
62
|
-
const baseLabels = [
|
|
63
|
-
'feed_id',
|
|
64
|
-
'participant_id',
|
|
65
|
-
'cache_type',
|
|
66
|
-
'is_from_ws',
|
|
67
|
-
'experimental',
|
|
68
|
-
];
|
|
69
|
-
// Skipping this metrics for v3
|
|
70
|
-
// const cache_execution_duration_seconds = new client.Histogram({
|
|
71
|
-
// name: 'cache_execution_duration_seconds',
|
|
72
|
-
// help: 'A histogram bucket of the distribution of cache execution durations',
|
|
73
|
-
// labelNames: [...baseLabels, 'cache_hit'] as const,
|
|
74
|
-
// buckets: [0.01, 0.1, 1, 10],
|
|
75
|
-
// })
|
|
76
|
-
const cacheDataGetCount = new client.Counter({
|
|
77
|
-
name: 'cache_data_get_count',
|
|
78
|
-
help: 'A counter that increments every time a value is fetched from the cache',
|
|
79
|
-
labelNames: baseLabels,
|
|
80
|
-
});
|
|
81
|
-
const cacheDataGetValues = new client.Gauge({
|
|
82
|
-
name: 'cache_data_get_values',
|
|
83
|
-
help: 'A gauge keeping track of values being fetched from cache',
|
|
84
|
-
labelNames: baseLabels,
|
|
85
|
-
});
|
|
86
|
-
const cacheDataMaxAge = new client.Gauge({
|
|
87
|
-
name: 'cache_data_max_age',
|
|
88
|
-
help: 'A gauge tracking the max age of stored values in the cache',
|
|
89
|
-
labelNames: baseLabels,
|
|
90
|
-
});
|
|
91
|
-
const cacheDataSetCount = new client.Counter({
|
|
92
|
-
name: 'cache_data_set_count',
|
|
93
|
-
help: 'A counter that increments every time a value is set to the cache',
|
|
94
|
-
labelNames: [...baseLabels, 'status_code'],
|
|
95
|
-
});
|
|
96
|
-
const cacheDataStalenessSeconds = new client.Gauge({
|
|
97
|
-
name: 'cache_data_staleness_seconds',
|
|
98
|
-
help: 'Observes the staleness of the data returned',
|
|
99
|
-
labelNames: baseLabels,
|
|
100
|
-
});
|
|
101
|
-
// Redis Metrics
|
|
102
|
-
exports.redisConnectionsOpen = new client.Counter({
|
|
103
|
-
name: 'redis_connections_open',
|
|
104
|
-
help: 'The number of redis connections that are open',
|
|
105
|
-
});
|
|
106
|
-
exports.redisRetriesCount = new client.Counter({
|
|
107
|
-
name: 'redis_retries_count',
|
|
108
|
-
help: 'The number of retries that have been made to establish a redis connection',
|
|
109
|
-
});
|
|
110
|
-
exports.redisCommandsSentCount = new client.Counter({
|
|
111
|
-
name: 'redis_commands_sent_count',
|
|
112
|
-
help: 'The number of redis commands sent',
|
|
113
|
-
labelNames: ['status', 'function_name'],
|
|
114
|
-
});
|
|
115
|
-
// Cache Warmer Metrics
|
|
116
|
-
exports.cacheWarmerCount = new client.Gauge({
|
|
117
|
-
name: 'cache_warmer_get_count',
|
|
118
|
-
help: 'The number of cache warmers running',
|
|
119
|
-
labelNames: ['isBatched'],
|
|
120
|
-
});
|
package/package/cache/redis.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import Redis from 'ioredis';
|
|
2
|
-
import { Cache, CacheEntry } from './index';
|
|
3
|
-
/**
|
|
4
|
-
* Redis implementation of a Cache. It uses a simple js Object, storing entries with both
|
|
5
|
-
* a value and an expiration timestamp. Expired entries are deleted on reads (i.e. no background gc/upkeep).
|
|
6
|
-
*
|
|
7
|
-
* @typeParam T - the type for the entries' values
|
|
8
|
-
*/
|
|
9
|
-
export declare class RedisCache<T = unknown> implements Cache<T> {
|
|
10
|
-
private client;
|
|
11
|
-
constructor(client: Redis);
|
|
12
|
-
get(key: string): Promise<T | undefined>;
|
|
13
|
-
delete(key: string): Promise<void>;
|
|
14
|
-
set(key: string, value: T, ttl: number): Promise<void>;
|
|
15
|
-
setMany(entries: CacheEntry<T>[], ttl: number): Promise<void>;
|
|
16
|
-
}
|
package/package/cache/redis.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.RedisCache = void 0;
|
|
27
|
-
const util_1 = require("../util");
|
|
28
|
-
const cacheMetrics = __importStar(require("./metrics"));
|
|
29
|
-
const logger = (0, util_1.makeLogger)('RedisCache');
|
|
30
|
-
/**
|
|
31
|
-
* Redis implementation of a Cache. It uses a simple js Object, storing entries with both
|
|
32
|
-
* a value and an expiration timestamp. Expired entries are deleted on reads (i.e. no background gc/upkeep).
|
|
33
|
-
*
|
|
34
|
-
* @typeParam T - the type for the entries' values
|
|
35
|
-
*/
|
|
36
|
-
// TODO: Add error handling for redis command failures
|
|
37
|
-
class RedisCache {
|
|
38
|
-
constructor(client) {
|
|
39
|
-
this.client = client;
|
|
40
|
-
}
|
|
41
|
-
async get(key) {
|
|
42
|
-
logger.trace(`Getting key ${key}`);
|
|
43
|
-
const value = await this.client.get(key);
|
|
44
|
-
// Record get command sent to Redis
|
|
45
|
-
cacheMetrics.redisCommandsSentCount
|
|
46
|
-
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'get' })
|
|
47
|
-
.inc();
|
|
48
|
-
if (!value) {
|
|
49
|
-
logger.debug(`No entry in redis cache for key "${key}", returning undefined`);
|
|
50
|
-
return undefined;
|
|
51
|
-
}
|
|
52
|
-
return JSON.parse(value); // TODO: Check for invalid parsing
|
|
53
|
-
}
|
|
54
|
-
async delete(key) {
|
|
55
|
-
logger.trace(`Deleting key ${key}`);
|
|
56
|
-
await this.client.del(key);
|
|
57
|
-
// Record delete command sent to Redis
|
|
58
|
-
cacheMetrics.redisCommandsSentCount
|
|
59
|
-
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'delete' })
|
|
60
|
-
.inc();
|
|
61
|
-
}
|
|
62
|
-
async set(key, value, ttl) {
|
|
63
|
-
logger.trace(`Setting key ${key}`);
|
|
64
|
-
// TODO: Check for invalid stringify
|
|
65
|
-
await this.client.set(key, JSON.stringify(value), 'PX', ttl);
|
|
66
|
-
// Only record metrics if feed Id is present, otherwise assuming value is not adapter response to record
|
|
67
|
-
const feedId = value.meta?.metrics?.feedId;
|
|
68
|
-
if (feedId) {
|
|
69
|
-
// Record cache set count, max age, and staleness (set to 0 for cache set)
|
|
70
|
-
const label = cacheMetrics.cacheMetricsLabel(key, feedId, cacheMetrics.CacheTypes.Redis);
|
|
71
|
-
cacheMetrics.cacheSet(label, ttl);
|
|
72
|
-
}
|
|
73
|
-
// Record set command sent to Redis
|
|
74
|
-
cacheMetrics.redisCommandsSentCount
|
|
75
|
-
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'set' })
|
|
76
|
-
.inc();
|
|
77
|
-
}
|
|
78
|
-
async setMany(entries, ttl) {
|
|
79
|
-
logger.trace(`Setting a bunch of keys`);
|
|
80
|
-
// Unfortunately, there's no ttl for mset
|
|
81
|
-
let chain = this.client.multi();
|
|
82
|
-
for (const entry of entries) {
|
|
83
|
-
chain = chain.set(entry.key, JSON.stringify(entry.value), 'PX', ttl);
|
|
84
|
-
// TODO: Move to after error handling once implemented to avoid recording cache sets that failed
|
|
85
|
-
// Only record metrics if feed Id is present, otherwise assuming value is not adapter response to record
|
|
86
|
-
const feedId = entry.value.meta?.metrics?.feedId;
|
|
87
|
-
if (feedId) {
|
|
88
|
-
// Record cache set count, max age, and staleness (set to 0 for cache set)
|
|
89
|
-
const label = cacheMetrics.cacheMetricsLabel(entry.key, feedId, cacheMetrics.CacheTypes.Redis);
|
|
90
|
-
cacheMetrics.cacheSet(label, ttl);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
await chain.exec();
|
|
94
|
-
// Record setMany command sent to Redis
|
|
95
|
-
cacheMetrics.redisCommandsSentCount
|
|
96
|
-
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'exec' })
|
|
97
|
-
.inc();
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
exports.RedisCache = RedisCache;
|