Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/unkeyed/unkey
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Oct 23, 2024
2 parents e519784 + 10d978b commit 83ed260
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions apps/api/src/pkg/ratelimit/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,27 @@ export class AgentRatelimiter implements RateLimiter {
): Promise<Result<RatelimitResponse, RatelimitError>> {
const start = performance.now();
try {
// Construct a binding key that could match a configured ratelimiter
const lookup = `RL_${req.limit}_${Math.round(req.interval / 1000)}s` as keyof typeof c.env;
const binding = c.env[lookup];
if (req.async) {
// Construct a binding key that could match a configured ratelimiter
const lookup = `RL_${req.limit}_${Math.round(req.interval / 1000)}s` as keyof typeof c.env;
const binding = c.env[lookup];

if (binding) {
const res = await cloudflareRatelimiter.parse(binding).limit({ key: req.identifier });
if (binding) {
const res = await cloudflareRatelimiter.parse(binding).limit({ key: req.identifier });

this.metrics.emit({
metric: "metric.ratelimit",
workspaceId: req.workspaceId,
namespaceId: req.namespaceId,
latency: performance.now() - start,
identifier: req.identifier,
mode: "cloudflare",
error: false,
success: res.success,
source: "cloudflare",
});
return Ok({ pass: res.success, reset: -1, current: -1, remaining: -1, triggered: null });
this.metrics.emit({
metric: "metric.ratelimit",
workspaceId: req.workspaceId,
namespaceId: req.namespaceId,
latency: performance.now() - start,
identifier: req.identifier,
mode: "async",
error: false,
success: res.success,
source: "cloudflare",
});
return Ok({ pass: res.success, reset: -1, current: -1, remaining: -1, triggered: null });
}
}
} catch (err) {
this.logger.error("cfrl failed, falling back to agent", {
Expand Down

0 comments on commit 83ed260

Please sign in to comment.