|
| 1 | +import { |
| 2 | + AdapterEndpoint, |
| 3 | + priceEndpointInputParametersDefinition, |
| 4 | +} from '@chainlink/external-adapter-framework/adapter' |
| 5 | +import { TransportRoutes } from '@chainlink/external-adapter-framework/transports' |
| 6 | +import { makeLogger, SingleNumberResultResponse } from '@chainlink/external-adapter-framework/util' |
| 7 | +import { InputParameters } from '@chainlink/external-adapter-framework/validation' |
| 8 | +import { AdapterError } from '@chainlink/external-adapter-framework/validation/error' |
| 9 | +import { config } from '../config' |
| 10 | +import { buildNobiWsTransport } from '../transport/price' |
| 11 | + |
| 12 | +const logger = makeLogger('NobiWsEndpoint') |
| 13 | + |
| 14 | +// Hardcoded on purpose, documented max connections per key from the data provider |
| 15 | +const MAX_TRANSPORTS = 10 |
| 16 | + |
| 17 | +// Framework requires transport names to contain only lowercase letters |
| 18 | +const indexToTransportName = (i: number): string => `ws${String.fromCharCode(97 + i)}` |
| 19 | + |
| 20 | +export const inputParameters = new InputParameters(priceEndpointInputParametersDefinition, [ |
| 21 | + { |
| 22 | + base: 'BTC', |
| 23 | + quote: 'USD', |
| 24 | + }, |
| 25 | +]) |
| 26 | + |
| 27 | +export type BaseEndpointTypes = { |
| 28 | + Parameters: typeof inputParameters.definition |
| 29 | + Response: SingleNumberResultResponse |
| 30 | + Settings: typeof config.settings |
| 31 | +} |
| 32 | + |
| 33 | +// We register MAX_TRANSPORTS transports on the endpoint |
| 34 | +// Requests will be routed to transports in order up to |
| 35 | +// config.MAX_SUBSCRIPTIONS_PER_TRANSPORT subscriptions then rollover to the next transport |
| 36 | +const transportRoutes = new TransportRoutes<BaseEndpointTypes>() |
| 37 | +for (let i = 0; i < MAX_TRANSPORTS; i++) { |
| 38 | + transportRoutes.register(indexToTransportName(i), buildNobiWsTransport()) |
| 39 | +} |
| 40 | + |
| 41 | +// requestMapping is intentionally add-only (no decrement on unsubscribe) to keep routing stable |
| 42 | +// for long-lived pairs. The underlying transport handles subscribe/unsubscribe independently. |
| 43 | +export const requestMapping: Map<string, string> = new Map() |
| 44 | + |
| 45 | +export const routeRequest = ( |
| 46 | + base: string, |
| 47 | + quote: string, |
| 48 | + maxTransports: number, |
| 49 | + maxSubscriptionsPerTransport: number, |
| 50 | +): string => { |
| 51 | + // find if key is in the mapping already |
| 52 | + const mappingKey = `${base}/${quote}` |
| 53 | + const transportName = requestMapping.get(mappingKey) |
| 54 | + if (transportName) { |
| 55 | + logger.debug(`Routing mapping found for ${mappingKey}: transportName = ${transportName}`) |
| 56 | + return transportName |
| 57 | + } |
| 58 | + |
| 59 | + // if not found, find the first transport with available capacity and assign it in the mapping |
| 60 | + const transportIndex = Math.floor(requestMapping.size / maxSubscriptionsPerTransport) |
| 61 | + |
| 62 | + // return 429 if we've reached max capacity on all transports (MAX_TRANSPORTS * MAX_SUBSCRIPTIONS_PER_TRANSPORT) |
| 63 | + if (transportIndex >= maxTransports) { |
| 64 | + throw new AdapterError({ |
| 65 | + statusCode: 429, |
| 66 | + message: `All transports are at full capacity, the EA has reached the maximum number of active transports (${maxTransports}) and subscriptions per transport (${maxSubscriptionsPerTransport})`, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + // assign the new transport in the mapping and return it |
| 71 | + const newTransportName = indexToTransportName(transportIndex) |
| 72 | + requestMapping.set(mappingKey, newTransportName) |
| 73 | + logger.debug( |
| 74 | + `Routing new ${mappingKey} to ${newTransportName}, current requestMapping size = ${requestMapping.size}`, |
| 75 | + ) |
| 76 | + |
| 77 | + return newTransportName |
| 78 | +} |
| 79 | + |
| 80 | +export const endpoint = new AdapterEndpoint({ |
| 81 | + name: 'price', |
| 82 | + aliases: ['state'], |
| 83 | + inputParameters, |
| 84 | + transportRoutes, |
| 85 | + defaultTransport: indexToTransportName(0), |
| 86 | + customRouter: (req, settings) => |
| 87 | + routeRequest( |
| 88 | + req.requestContext.data.base, |
| 89 | + req.requestContext.data.quote, |
| 90 | + MAX_TRANSPORTS, |
| 91 | + settings.MAX_SUBSCRIPTIONS_PER_TRANSPORT, |
| 92 | + ), |
| 93 | +}) |
0 commit comments