-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Redis rate limiter to ensure provider send rate is upheld (#131)
* Adds Redis rate limiter to ensure provider send rate is upheld * Tweaks to throttling methods * Fixes to rate limiting Improves how rate limiting works by adding a throttled state
- Loading branch information
Showing
26 changed files
with
285 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
apps/platform/db/migrations/20230418041529_add_provider_rate_limiter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
exports.up = async function(knex) { | ||
await knex.schema.table('providers', function(table) { | ||
table.integer('rate_limit').nullable().after('is_default') | ||
}) | ||
} | ||
|
||
exports.down = async function(knex) { | ||
await knex.schema.table('providers', function(table) { | ||
table.dropColumn('rate_limit') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { DefaultRedis, Redis, RedisConfig } from './redis' | ||
|
||
// eslint-disable-next-line quotes | ||
const incrTtlLuaScript = `redis.call('set', KEYS[1], 0, 'EX', ARGV[2], 'NX') \ | ||
local consumed = redis.call('incrby', KEYS[1], ARGV[1]) \ | ||
local ttl = redis.call('pttl', KEYS[1]) \ | ||
if ttl == -1 then \ | ||
redis.call('expire', KEYS[1], ARGV[2]) \ | ||
ttl = 1000 * ARGV[2] \ | ||
end \ | ||
return {consumed, ttl} \ | ||
` | ||
|
||
interface RateLimitRedis extends Redis { | ||
rlflxIncr(key: string, points: number, secDuration: number): Promise<[ consumed: number, ttl: number ]> | ||
} | ||
|
||
export interface RateLimitResponse { | ||
exceeded: boolean | ||
pointsRemaining: number | ||
pointsUsed: number | ||
msRemaining: number | ||
} | ||
|
||
export default (config: RedisConfig) => { | ||
return new RateLimiter(config) | ||
} | ||
|
||
export class RateLimiter { | ||
client: RateLimitRedis | ||
constructor(config: RedisConfig) { | ||
this.client = DefaultRedis(config) as RateLimitRedis | ||
this.client.defineCommand('rlflxIncr', { | ||
numberOfKeys: 1, | ||
lua: incrTtlLuaScript, | ||
}) | ||
} | ||
|
||
async consume(key: string, limit: number, msDuration = 1000): Promise<RateLimitResponse> { | ||
const secDuration = Math.floor(msDuration / 1000) | ||
const response = await this.client.rlflxIncr(key, 1, secDuration) | ||
return { | ||
exceeded: response[0] > limit, | ||
pointsRemaining: Math.max(0, limit - response[0]), | ||
pointsUsed: response[0], | ||
msRemaining: response[1], | ||
} | ||
} | ||
|
||
async get(key: string) { | ||
const response = await this.client | ||
.multi() | ||
.get(key) | ||
.pttl(key) | ||
.exec() | ||
if (!response) return undefined | ||
return { | ||
pointsUsed: parseInt(response[0][1] as string, 10), | ||
msRemaining: response[1][1], | ||
} | ||
} | ||
|
||
async close() { | ||
this.client.disconnect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import IORedis, { Redis } from 'ioredis' | ||
|
||
export interface RedisConfig { | ||
host: string | ||
port: number | ||
tls: boolean | ||
} | ||
|
||
export const DefaultRedis = (config: RedisConfig, extraOptions = {}): Redis => { | ||
return new IORedis({ | ||
port: config.port, | ||
host: config.host, | ||
tls: config.tls | ||
? { rejectUnauthorized: false } | ||
: undefined, | ||
...extraOptions, | ||
}) | ||
} | ||
|
||
export { Redis } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.