-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,19 +8,42 @@ import { sleep } from "https://deno.land/x/[email protected]/mod.ts"; | |
// throw new Error("Function not implemented."); | ||
// } | ||
|
||
const kv = await Deno.openKv(); | ||
|
||
const server = new Server({ | ||
port: 8000, | ||
root: `${Deno.cwd()}/_site`, | ||
}); | ||
|
||
// Rate limiting | ||
server.use(async (request, next) => { | ||
// Rate limiting (max 10 requests per 10 seconds) | ||
const maxRequests = 10; | ||
const interval = 10000; | ||
const rateLimitter = { | ||
requestCount: 0, | ||
lastResetTime: Date.now(), | ||
}; | ||
|
||
server.use(async (request, next, _conn) => { | ||
const response = await next(request); | ||
|
||
await sleep(2); | ||
|
||
// const remoteAddr = conn.remoteAddr; | ||
|
||
const currentTime = Date.now(); | ||
|
||
// Reset rateLimitter after elapsing interval | ||
if (currentTime - rateLimitter.lastResetTime > interval) { | ||
rateLimitter.requestCount = 0; | ||
rateLimitter.lastResetTime = currentTime; | ||
} | ||
|
||
// Rate limiting | ||
if (rateLimitter.requestCount < maxRequests) { | ||
rateLimitter.requestCount += 1; | ||
} else { | ||
console.log("Rate limiting: Sleep 30 seconds."); | ||
await sleep(30); | ||
} | ||
|
||
await sleep(1); | ||
|
||
return response; | ||
}) | ||
|
||
|