mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-23 15:42:12 +03:00
feat(redis): add configurable key namespace prefix (#11042)
Validated on the combined board over tip 80d931ae: quota-redis-store (incl. the KEY_PREFIX derivation test), local-redis-status and rate-limiter-redis-optional green, typecheck:core clean. One pre-merge fix pushed to the branch: docs/reference/ENVIRONMENT.md gained the REDIS_KEY_PREFIX row (env-doc-sync gate requires every .env.example var documented). Board note: the redis tests leave an ioredis retry handle open and hang the runner exit locally — assertions all pass; pre-existing pattern, not from this PR. Thank you @MeRezaRezaei!
This commit is contained in:
@@ -14,9 +14,12 @@ workloads:
|
||||
|
||||
| Workload | Driver | Client Factory | Key Pattern |
|
||||
|---|---|---|---|
|
||||
| Rate limiting | `rateLimiter.ts` | `getRedisClient()` — lazy `ioredis` singleton | Lua‑atomic rate limit windows |
|
||||
| Auth cache | `apiKeys.ts` | Reuses `rateLimiter`'s client | `auth:api_key:<sha256>` with TTL |
|
||||
| Quota store | `redisQuotaStore.ts` | Separate `getRedisClient(url)` singleton | Configurable per-instance |
|
||||
| Rate limiting | `rateLimiter.ts` | `getRedisClient()` — lazy `ioredis` singleton | `<prefix>rl:*` Lua‑atomic rate limit windows |
|
||||
| Auth cache | `apiKeys.ts` | Reuses `rateLimiter`'s client | `<prefix>auth:api_key:<sha256>` with TTL |
|
||||
| Quota store | `redisQuotaStore.ts` | Separate `getRedisClient(url)` singleton | `<prefix>quota:*` configurable per-instance |
|
||||
|
||||
All three workloads share one namespace prefix so OmniRoute can co-exist with other apps on a
|
||||
single Redis instance (e.g. `127.0.0.1:6379`). See [Key Namespacing](#key-namespacing).
|
||||
|
||||
---
|
||||
|
||||
@@ -25,6 +28,7 @@ workloads:
|
||||
| Setting | Value | Where |
|
||||
|---|---|---|
|
||||
| `REDIS_URL` env var | `redis://redis:6379` (compose), optional | `rateLimiter.ts:5`, `.env.example` |
|
||||
| `REDIS_KEY_PREFIX` env var | `omniroute:` (default) | `rateLimiter.ts`, `redisQuotaStore.ts`, `.env.example` |
|
||||
| `QUOTA_STORE_REDIS_URL` env var | separate, can differ from `REDIS_URL` | `quota/storeFactory.ts` |
|
||||
| `QUOTA_STORE_DRIVER` | `"sqlite"` (default), `"redis"` optional | `quota/storeFactory.ts` |
|
||||
| ioredis `maxRetriesPerRequest` | `3` | `rateLimiter.ts` client creation |
|
||||
@@ -36,6 +40,29 @@ workloads:
|
||||
|
||||
---
|
||||
|
||||
## Key Namespacing
|
||||
|
||||
OmniRoute shares a Redis instance with whatever else runs on the host. Without a namespace,
|
||||
keys like `auth:api_key:<sha256>` or `rl:*` could collide with keys from other applications
|
||||
using the same Redis (this instance runs Redis on `127.0.0.1:6379` alongside other services).
|
||||
|
||||
Set `REDIS_KEY_PREFIX` to a non-empty string to prefix **every** OmniRoute key:
|
||||
|
||||
```bash
|
||||
# .env — all OmniRoute keys become omniroute:rl:*, omniroute:auth:*, omniroute:quota:*
|
||||
REDIS_KEY_PREFIX=omniroute:
|
||||
```
|
||||
|
||||
- **Default:** `omniroute:` (applied when `REDIS_KEY_PREFIX` is unset or blank).
|
||||
- **Applied to:** rate limiter + auth cache (shared `ioredis` client via `keyPrefix`) and the
|
||||
quota store (`KEY_PREFIX = "${REDIS_KEY_PREFIX}quota"`).
|
||||
- **Changing the prefix** when keys already exist in Redis orphans the old keys (they expire
|
||||
via TTL / LRU). Safe to change; no migration needed.
|
||||
- **ioredis `keyPrefix`** automatically prepends the prefix on writes **and** strips it on reads,
|
||||
so application code never sees the prefix.
|
||||
|
||||
---
|
||||
|
||||
## Recommended Production Tuning
|
||||
|
||||
### 1. Connection Pool / Client Options (ioredis `Redis` constructor)
|
||||
|
||||
Reference in New Issue
Block a user