Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): excessive REST router refreshes #90

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions packages/router/src/controllers/Rest/Rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export class RestController extends ConduitRouter {
private _swagger: SwaggerGenerator;
private _scheduledTimeout: any = null;

get registeredRoutes() {
kkopanidis marked this conversation as resolved.
Show resolved Hide resolved
return this._registeredRoutes;
}

constructor(readonly app: Application, swaggerRouterMetadata: SwaggerRouterMetadata) {
super(app);
this._registeredLocalRoutes = new Map();
Expand All @@ -46,7 +42,7 @@ export class RestController extends ConduitRouter {
const registered = this._registeredLocalRoutes.has(key);
this._registeredLocalRoutes.set(key, router);
if (registered) {
this._scheduleTimeout();
this.scheduleRouterRefresh();
} else {
this.addRoute(path, router);
}
Expand All @@ -58,7 +54,7 @@ export class RestController extends ConduitRouter {
this._registeredRoutes.set(key, route);
// If the route has been registered, then we need to reinitialize the router so the actual routes change
if (registered) {
this._scheduleTimeout();
this.scheduleRouterRefresh();
} else {
this.addConduitRoute(route);
}
Expand Down Expand Up @@ -234,23 +230,27 @@ export class RestController extends ConduitRouter {
this._swagger.addRouteSwaggerDocumentation(route);
}

private _scheduleTimeout() {
protected refreshRouter() {
this.scheduleRouterRefresh();
}

private scheduleRouterRefresh() {
if (this._scheduledTimeout) {
clearTimeout(this._scheduledTimeout);
this._scheduledTimeout = null;
}

this._scheduledTimeout = setTimeout(() => {
try {
this.refreshRouter();
this._refreshRouter();
} catch (err) {
console.error(err);
}
this._scheduledTimeout = null;
}, 3000);
}

protected refreshRouter() {
protected _refreshRouter() {
this.initializeRouter();
this._registeredLocalRoutes.forEach((route, key) => {
const [method, path] = key.split('-');
Expand Down