Skip to content

Commit

Permalink
Use DO fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Jul 21, 2024
1 parent f60e940 commit 9787d6e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion glot_cloudflare/functions/api/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const onRequestPost: PagesFunction<Env & StringRecord> = async (context)

const id = context.env.RATE_LIMITER.idFromName(ip);
const stub = context.env.RATE_LIMITER.get(id);
const stats = stub.increment({ maxRequests: 10, periodDuration: 60 * 1000 });
const response = await stub.fetch(context.request);
const stats = await response.text();

console.log(stats)

Expand Down
5 changes: 5 additions & 0 deletions glot_cloudflare_rate_limiter/src/rate_limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export class RateLimiter extends DurableObject {
private startTimestamp: number = 0;
private requestCount: number = 0;

async fetch(request: Request): Promise<Response> {
const stats = await this.increment({ maxRequests: 10, periodDuration: 60 * 1000 });
return new Response(JSON.stringify(stats), { status: 200 });
}

async increment(config: RateLimitConfig): Promise<RateLimitStats> {
const now = Date.now();
const elapsedTime = now - this.startTimestamp;
Expand Down

0 comments on commit 9787d6e

Please sign in to comment.