Skip to content

Commit

Permalink
Add Ratelimit cleanup interval
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7d8 committed Apr 19, 2024
1 parent ffeddd3 commit 42700c2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 9.1.7

- Add Ratelimit cleanup interval
- Fix typo in readme
- fix some tabs

## 9.1.6

- Do not include prefix in route files directly
Expand Down
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ You will need a runtime to actually use the server. (adapt command to your packa

```sh
npm install @rjweb/runtime-bun # for bun users
npm install @rjweb/runtime-node # for nodejs users (who couldve guseed, may also work with deno though)
npm install @rjweb/runtime-node # for nodejs users (who couldve guessed, may also work with deno though)
```

## Migrating
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rjweb-server",
"version": "9.1.6",
"version": "9.1.7",
"description": "Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
Expand Down
20 changes: 10 additions & 10 deletions src/classes/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ export default class Route<Type extends RequestType> {

if (isRegExp(path)) {
this.urlData = {
type: 'regexp',
value: path,
type: 'regexp',
value: path,
prefix: '/',
segments: [{
segments: [{
raw: '',
paramsRegExp: new RegExp(''),
params: []
paramsRegExp: new RegExp(''),
params: []
}, {
raw: '',
paramsRegExp: new RegExp(''),
params: []
paramsRegExp: new RegExp(''),
params: []
}]
}
}
} else if (typeof path === 'string') {
const segments = parseURL(path).path.split('/')

Expand All @@ -80,8 +80,8 @@ export default class Route<Type extends RequestType> {
for (const segment of segments) {
this.urlData.segments.push({
raw: segment,
paramsRegExp: new RegExp(segment.replace(/{([^}]+)}/g, '(.*\\S)')),
params: (segment.match(/{([^}]+)}/g) ?? []).map((m) => m.slice(1, -1))
paramsRegExp: new RegExp(segment.replace(/{([^}]+)}/g, '(.*\\S)')),
params: (segment.match(/{([^}]+)}/g) ?? []).map((m) => m.slice(1, -1))
})
}
} else {
Expand Down
15 changes: 14 additions & 1 deletion src/classes/Server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseImplementation, Implementation } from "@/types/implementation"
import { Method, ReverseProxyIps, ServerStatus } from "@/types/global"
import { FullServerOptions, ServerOptions } from "@/types/structures/ServerOptions"
import { as, object, size } from "@rjweb/utils"
import { as, object, size, time } from "@rjweb/utils"
import GlobalContext from "@/types/internal/classes/GlobalContext"
import ContentTypes from "@/classes/router/ContentTypes"
import { DataContext, EndFn, ErrorCallbacks, RatelimitCallbacks, RealAny } from "@/types/internal"
Expand Down Expand Up @@ -67,6 +67,7 @@ export default class Server<const Options extends ServerOptions, Middlewares ext
private _status: ServerStatus = 'stopped'
private global: GlobalContext
private promises: Promise<any>[] = []
private interval: NodeJS.Timeout | null = null
private openAPISchemas: Record<string, oas31.SchemaObject | oas31.ReferenceObject> = {}

/**
Expand Down Expand Up @@ -286,6 +287,17 @@ export default class Server<const Options extends ServerOptions, Middlewares ext
await Promise.all(this.middlewares.map((middleware) => middleware.callbacks.load?.(middleware.config, this as any, this.global)))
this.global.logger.debug(`Running Middleware Promises ... Done ${(performance.now() - middlewareStartTime).toFixed(2)}ms`)

this.interval = setInterval(() => {
this.global.logger.debug('Running 1min Interval ...')
const intervalStartTime = performance.now()

for (const [ key, { end } ] of this.global.rateLimits) {
if (end < Date.now()) this.global.rateLimits.delete(key)
}

this.global.logger.debug(`Running 1min Interval ... Done ${(performance.now() - intervalStartTime).toFixed(2)}ms`)
}, time(1).m())

resolve(this.implementation.port())
} catch (err) {
this.implementation.stop()
Expand Down Expand Up @@ -318,6 +330,7 @@ export default class Server<const Options extends ServerOptions, Middlewares ext
if (this._status === 'stopped') throw new Error('Server is already stopped')

this.implementation.stop()
if (this.interval) clearInterval(this.interval)
this._status = 'stopped'

return this
Expand Down

0 comments on commit 42700c2

Please sign in to comment.