Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(api): prefix rate limit keys with the app name

spacesdrive7f008526fccd
diff --git a/backend/lib/rateLimit/limiter.ts b/backend/lib/rateLimit/limiter.ts
index 656a65f..9d5336b 100644
--- a/backend/lib/rateLimit/limiter.ts
+++ b/backend/lib/rateLimit/limiter.ts
@@ -21,8 +21,9 @@ const deniedUntil = new Map<string, number>();
const REDIS_RETRY_AFTER_MS = 30_000;
let redisDownUntil = 0;
+// prefixed with the app name: one Upstash database is often shared by several apps
export function rateLimitKey(rule: RateLimitRule, identifier: string): string {
- return `ratelimit:${rule.name}:${identifier}`;
+ return `twiligent:ratelimit:${rule.name}:${identifier}`;
}
// A client that is already over the limit is answered from the isolate until its retry time, so a
diff --git a/backend/tests/lib/rateLimit/limiter.test.ts b/backend/tests/lib/rateLimit/limiter.test.ts
index 5431593..3191473 100644
--- a/backend/tests/lib/rateLimit/limiter.test.ts
+++ b/backend/tests/lib/rateLimit/limiter.test.ts
@@ -38,7 +38,7 @@ function scriptedRedis(answer: (call: ScriptCall) => [number, number, number, nu
beforeEach(() => resetRateLimitState());
test('keys are namespaced by rule and identifier', () => {
- assert.equal(rateLimitKey(rule, 'user-1'), 'ratelimit:test:user-1');
+ assert.equal(rateLimitKey(rule, 'user-1'), 'twiligent:ratelimit:test:user-1');
});
test('without Redis the limit is counted in memory', async () => {
@@ -61,7 +61,7 @@ test('with Redis the decision comes from the algorithm script for that rule', as
assert.deepEqual(decision, { allowed: true, limit: 2, remaining: 1, resetMs: 900, retryAfterMs: 0 });
assert.equal(calls.length, 1);
assert.equal(calls[0].script, LUA_SCRIPTS.fixedWindow);
- assert.deepEqual(calls[0].keys, ['ratelimit:test:user-1']);
+ assert.deepEqual(calls[0].keys, ['twiligent:ratelimit:test:user-1']);
assert.deepEqual(calls[0].args, ['100', '2', '1000']);
});
diff --git a/docs/architecture/backend/CACHING.md b/docs/architecture/backend/CACHING.md
index 425fb31..44a477c 100644
--- a/docs/architecture/backend/CACHING.md
+++ b/docs/architecture/backend/CACHING.md
@@ -40,7 +40,7 @@ export async function getRedditCache(redis: Redis | null, userId: string, accoun
| `ig:{userId}:{accountId}` | `{ media, fetchedAt, version }` - normalized Instagram media (see `normalizeInstagramMedia()`) | `GET /api/accounts/:id/ig-analytics` or `ig-media` succeeds, or the Overview warms it | `DELETE /api/accounts/:id`, or a `version` mismatch |
| `reddit:{userId}:{accountId}` | Object `{ profile, analytics, posts }` for Reddit account | `GET /api/accounts/:id/reddit-analytics` succeeds | `DELETE /api/accounts/:id` |
-Rate limit state also lives in Redis under `ratelimit:{rule}:{identifier}` keys (hashes or sorted sets, each with its own expiry). They are written only by the Lua scripts in `lib/rateLimit/scripts.ts` through a separate fast-fail client; see `RATE_LIMITING.md`.
+Rate limit state also lives in Redis under `twiligent:ratelimit:{rule}:{identifier}` keys (hashes or sorted sets, each with its own expiry). They are written only by the Lua scripts in `lib/rateLimit/scripts.ts` through a separate fast-fail client; see `RATE_LIMITING.md`.
**Note:** Instagram OAuth state is an HMAC-signed token and does not use Redis. The unused Redis-backed `oauth_ig:` helpers were removed.
diff --git a/docs/architecture/backend/RATE_LIMITING.md b/docs/architecture/backend/RATE_LIMITING.md
index 45d4f88..04b8acb 100644
--- a/docs/architecture/backend/RATE_LIMITING.md
+++ b/docs/architecture/backend/RATE_LIMITING.md
@@ -93,7 +93,7 @@ CORS exposes the four headers. The frontend needs no special handling: `request(
## Redis Keys
-`ratelimit:{rule}:{identifier}`, for example `ratelimit:user:<uuid>` or `ratelimit:ip:203.0.113.7`. Bucket, fixed window and sliding counter keys are hashes; sliding log keys are sorted sets. All keys expire on their own.
+`twiligent:ratelimit:{rule}:{identifier}`, for example `twiligent:ratelimit:user:<uuid>` or `twiligent:ratelimit:ip:203.0.113.7`. The `twiligent:` prefix keeps the keys apart from other apps that share the same Upstash database (a common setup on the free tier, where `ratelimit:` is a popular prefix). Bucket, fixed window and sliding counter keys are hashes; sliding log keys are sorted sets. All keys expire on their own.
## Cost