Skip to content

Commit

Permalink
fix: default ratelimits
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Sep 18, 2024
1 parent 4ae69e9 commit 3cebea8
Show file tree
Hide file tree
Showing 4 changed files with 864 additions and 799 deletions.
2 changes: 1 addition & 1 deletion apps/api/src/pkg/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function initCache(c: Context<HonoEnv>, metrics: Metrics): C<CacheNamespa
cloudflareApiKey: c.env.CLOUDFLARE_API_KEY,
zoneId: c.env.CLOUDFLARE_ZONE_ID,
domain: "cache.unkey.dev",
cacheBuster: "v5",
cacheBuster: "v6",
})
: undefined;

Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/pkg/cache/namespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type CacheNamespaces = {
api: Api;
permissions: string[];
roles: string[];
ratelimits: { [name: string]: Ratelimit };
ratelimits: { [name: string]: Pick<Ratelimit, "name" | "limit" | "duration"> };
identity: CachedIdentity | null;
} | null;
apiById: (Api & { keyAuth: KeyAuth | null }) | null;
Expand Down
33 changes: 18 additions & 15 deletions apps/api/src/pkg/keys/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,19 @@ export class KeyService {
* Merge ratelimits from the identity and the key
* Key limits take pecedence
*/
const ratelimits: { [name: string]: Ratelimit } = {};
const ratelimits: { [name: string]: Pick<Ratelimit, "name" | "limit" | "duration"> } = {};

if (
dbRes.ratelimitAsync !== null &&
dbRes.ratelimitDuration !== null &&
dbRes.ratelimitLimit !== null
) {
ratelimits.default = {
name: "default",
limit: dbRes.ratelimitLimit,
duration: dbRes.ratelimitDuration,
};
}
for (const rl of dbRes.identity?.ratelimits ?? []) {
ratelimits[rl.name] = rl;
}
Expand Down Expand Up @@ -457,26 +469,17 @@ export class KeyService {
const ratelimits: {
[name: string | "default"]: Required<RatelimitRequest>;
} = {};
if (
data.key.ratelimitAsync !== null &&
data.key.ratelimitDuration !== null &&
data.key.ratelimitLimit !== null
) {
if ("default" in data.ratelimits) {
ratelimits.default = {
identity: data.identity?.id ?? data.key.id,
name: "default",
identity: data.key.id,
name: data.ratelimits.default.name,
cost: req.ratelimit?.cost ?? 1,
limit: data.key.ratelimitLimit,
duration: data.key.ratelimitDuration,
limit: data.ratelimits.default.limit,
duration: data.ratelimits.default.duration,
};
}

for (const r of req.ratelimits ?? []) {
if (r.name === "default" && "default" in ratelimits) {
// it's already added above
continue;
}

if (typeof r.limit !== "undefined" && typeof r.duration !== "undefined") {
ratelimits[r.name] = {
identity: data.identity?.id ?? data.key.id,
Expand Down
Loading

1 comment on commit 3cebea8

@vercel
Copy link

@vercel vercel bot commented on 3cebea8 Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

planetfall – ./apps/planetfall

planetfall.unkey.dev
planetfall-unkey.vercel.app
planetfall-two.vercel.app
planetfall-git-main-unkey.vercel.app

Please sign in to comment.