Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 1x 1x 1x 25x 1x 1x 25x 25x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 25x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 25x 10x 10x 10x 10x 10x 10x 25x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 1x 1x | import {adapter, type Errors, type IErrorMap, type IMeta} from '@feasibleone/blong';
import {cfgKey, creditsKey, monthKey, rateKey} from '../meter/keys.ts';
import {meterScript, parseMeterResult} from '../meter/script.ts';
/**
* Metering adapter instance (extends the generic `adapter.redis` base from
* blong-gogo).
*
* Registers the `meter` namespace so the gateway handlers can reach the
* metering operations (`meter.limit.meter|hydrate|cfg|adjust|reset`) via the
* handler proxy. The generic redis vocabulary (`redis.key.*`, `redis.hash.*`,
* `redis.script.*`) is inherited from the base adapter and stays reachable
* through the same port. Connection is lazy + fail-closed (any Redis error
* surfaces as `redis.unavailable` → HTTP 503).
*/
const errorMap: IErrorMap = {
'redis.unavailable': {message: 'Redis unavailable', statusCode: 503},
};
let _errors: Errors<typeof errorMap>;
/** The subset of the base adapter's client used by the metering ops. */
interface IRedisClient {
status: string;
connect(): Promise<unknown>;
eval(script: string, numKeys: number, ...keysAndArgs: unknown[]): Promise<unknown>;
hgetall(key: string): Promise<Record<string, string>>;
hset(key: string, field: string, value: string | number): Promise<unknown>;
hincrby(key: string, field: string, increment: number): Promise<number>;
del(...keys: string[]): Promise<number>;
}
export default adapter<{
redis: {
host?: string;
port?: number;
cluster?: boolean;
nodes?: Array<{host: string; port: number}>;
password?: string;
db?: number;
};
}>(({utError}) => {
_errors ||= utError.register(errorMap);
return {
extends: 'adapter.redis',
activation: {
default: {
namespace: 'meter',
imports: [],
},
dev: {
redis: {
host: '127.0.0.1',
port: 6379,
},
},
integration: {
redis: {
host: '127.0.0.1',
port: 6379,
},
},
},
async exec(params: Record<string, unknown>, $meta: IMeta) {
// Dispatch on the last method segment so both the bare
// `meter.limit.meter` and the namespace-prefixed
// `meter.gateway.limit.meter` forms reach the same operation.
const parts = ($meta.method ?? '').split('.');
const operation = parts[parts.length - 1];
const redis = (this.config.context as {redis?: IRedisClient}).redis;
try {
if (!redis) throw new Error('Redis client not available');
const status = redis.status;
if (
status !== 'ready' &&
status !== 'connect' &&
status !== 'connecting' &&
status !== 'reconnecting'
) {
await redis.connect();
}
switch (operation) {
case 'meter': {
const {applicationId, bundleName, creditCost, rateWindowSec} = params as {
applicationId: string;
bundleName: string;
creditCost: number;
rateWindowSec: number;
now?: number;
};
const now = (params.now as number | undefined) ??
Math.floor(Date.now() / 1000);
const keys = [
cfgKey(applicationId, bundleName),
creditsKey(applicationId, monthKey(now)),
rateKey(applicationId, bundleName, rateWindowSec, now),
];
const raw = await redis.eval(
meterScript,
keys.length,
...keys,
creditCost,
now,
rateWindowSec,
);
return parseMeterResult(raw);
}
case 'hydrate': {
const {
applicationId,
bundleName,
baseMonthlyCredits,
rateLimit,
rateWindowSec,
} = params as {
applicationId: string;
bundleName: string;
baseMonthlyCredits: number;
rateLimit: number;
rateWindowSec: number;
};
const key = cfgKey(applicationId, bundleName);
await redis.hset(key, 'baseMonthlyCredits', baseMonthlyCredits);
await redis.hset(key, 'rateLimit', rateLimit);
await redis.hset(key, 'rateWindowSec', rateWindowSec);
return {success: true};
}
case 'cfg': {
const {applicationId, bundleName} = params as {
applicationId: string;
bundleName: string;
};
return redis.hgetall(cfgKey(applicationId, bundleName));
}
case 'adjust': {
const {applicationId, delta} = params as {
applicationId: string;
delta: number;
month?: string;
now?: number;
};
const now = (params.now as number | undefined) ??
Math.floor(Date.now() / 1000);
const month = (params.month as string | undefined) ?? monthKey(now);
const balance = await redis.hincrby(
creditsKey(applicationId, month),
'balance',
delta,
);
return {balance};
}
case 'reset': {
const {applicationId, bundleName, clearCredits} = params as {
applicationId: string;
bundleName?: string;
clearCredits?: boolean;
month?: string;
now?: number;
};
const now = (params.now as number | undefined) ??
Math.floor(Date.now() / 1000);
const month = (params.month as string | undefined) ?? monthKey(now);
const keys = [
...(bundleName
? [cfgKey(applicationId, bundleName), rateKey(applicationId, bundleName, 60, now)]
: []),
];
if (clearCredits) keys.push(creditsKey(applicationId, month));
if (keys.length > 0) await redis.del(...keys);
return {success: true};
}
default:
return super.exec(params, $meta);
}
} catch (error) {
throw this.error(_errors['redis.unavailable'](error), $meta);
}
},
};
});
|