Skip to content

Commit

Permalink
update to ioredis for rate limit db
Browse files Browse the repository at this point in the history
  • Loading branch information
sjahl committed Oct 31, 2023
1 parent a341e8a commit 907c97a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion deploy/manifests/browser/base/api.deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ spec:
- name: WRITE_CACHE_REDIS_URL
value: redis
- name: RATE_LIMITER_REDIS_URL
value: redis://redis:6379/2
value: redis
- name: TRUST_PROXY
valueFrom:
configMapKeyRef:
Expand Down
17 changes: 6 additions & 11 deletions graphql-api/src/graphql/rate-limiting.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import redis from 'redis'
import { Redis } from 'ioredis'
import config from '../config'
import { UserVisibleError } from '../errors'
import logger from '../logger'

const rateLimitDb = redis.createClient({
url: config.RATE_LIMITER_REDIS_URL,
retry_strategy:
process.env.NODE_ENV === 'development'
? ({ attempt }: any) => {
logger.info('Retrying connection to rate limit database')
return Math.min(attempt * 100, 3000)
}
: ({ attempt }: any) => Math.min(attempt * 100, 3000),
const rateLimitDb = new Redis({
sentinels: [{ host: config.RATE_LIMITER_REDIS_URL, port: 26379 }],
name: 'mymaster',
db: 2,
})

const increaseRateLimitCounter = (key: any, value: any) => {
return Promise.race([
new Promise((resolve, reject) => {
rateLimitDb
.multi()
.set([key, 0, 'EX', 59, 'NX'])
.set(key, 0, 'EX', 59, 'NX')
.incrby(key, value)
.exec((err: any, replies: any) => {
if (err) {
Expand Down

0 comments on commit 907c97a

Please sign in to comment.