-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IP based rate limit middleware, powered by upstash
The minimum rate limitting limits in AWS WAF are too high for our purposes of trying to avoid cost spikes caused by to many openai API requests.
- Loading branch information
1 parent
78ba515
commit f94c2ce
Showing
7 changed files
with
108 additions
and
7 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
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 {z} from 'zod'; | ||
|
||
declare global { | ||
namespace NodeJS { | ||
interface ProcessEnv extends z.infer<typeof envVariables> {} | ||
} | ||
} | ||
|
||
const envVariables = z.object({ | ||
AWS_ACCESS_KEY_ID: z.string(), | ||
AWS_HANDLER_VERIFY_HEADER: z.string(), | ||
AWS_REGION: z.string(), | ||
AWS_SECRET_ACCESS_KEY: z.string(), | ||
GOOGLE_SEARCH_API_KEY: z.string(), | ||
GOOGLE_SEARCH_SEARCH_ENGINE_ID: z.string(), | ||
UPSTASH_REDIS_REST_TOKEN: z.string(), | ||
UPSTASH_REDIS_REST_URL: z.string(), | ||
}); | ||
|
||
envVariables.parse(process.env); |
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,38 @@ | ||
import {Ratelimit} from '@upstash/ratelimit'; | ||
import {Redis} from '@upstash/redis'; | ||
import type {MiddlewareHandler} from 'hono'; | ||
|
||
let ratelimit: Ratelimit | undefined; | ||
|
||
try { | ||
ratelimit = new Ratelimit({ | ||
redis: Redis.fromEnv(), | ||
limiter: Ratelimit.slidingWindow(10, `10 m`), | ||
analytics: true, | ||
ephemeralCache: new Map(), | ||
}); | ||
} catch { | ||
console.warn(`Unable to create Redis instance, no rate limits are applied.`); | ||
} | ||
|
||
export const ratelimitMiddleware: MiddlewareHandler = async (context, next) => { | ||
if (ratelimit && context.req.method === `POST`) { | ||
const ip = context.req.header(`cloudfront-viewer-address`); | ||
|
||
if (ip) { | ||
const {success, reset} = await ratelimit.limit(ip); | ||
|
||
if (!success) { | ||
console.debug(`Rate limit reached for ${ip}`); | ||
|
||
return context.text(`Too Many Requests`, 429, { | ||
'Retry-After': ((reset - Date.now()) / 1000).toFixed(), | ||
}); | ||
} | ||
} else { | ||
console.warn(`cloudfront-viewer-address not provided`); | ||
} | ||
} | ||
|
||
return next(); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.