-
Notifications
You must be signed in to change notification settings - Fork 4
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 undefined current context and memory leak with non HTTP events #25
base: master
Are you sure you want to change the base?
Conversation
seiwan
commented
Mar 23, 2023
- Fix the issue "unable to get on undefined..." due to concurrent requests
- Fix the memory leak issue caused by non HTTP events by managing them with LRU Cache
@@ -246,6 +246,38 @@ export class MyProvider { | |||
} | |||
``` | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to talk here about ClsModule since now is mandatory for Context. Also would be nice to have an small description on why we use ClsModule
|
||
@Injectable() | ||
export class ContextContainer { | ||
private contexts: Record<string, Context> = {}; | ||
private contextStack: string[] = []; | ||
private cache: LRUCache<string, Context>; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The id
recover process for the context should return an exception if we are not able to get it from the ClsService or the request. This exceptionb should be logged to prevent the user that the lib has been missused
|
||
remove(): void { | ||
const id = this.cls.getId(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add remove condition to take in account protocol here.
return this; | ||
} | ||
|
||
getCachedValue(key: string | symbol) { | ||
getCachedValue(key: string | symbol): any { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe getCachedValue(key: string | symbol): T?
private call(name: string, ...args: (string | boolean | symbol)[]): any { | ||
const currentContext = this.contexts.current(); | ||
|
||
if (currentContext instanceof Promise) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check on this
@@ -1,29 +1,45 @@ | |||
/* eslint-disable @typescript-eslint/no-unused-vars */ | |||
import { ContextContainer } from './context-container'; | |||
import { Injectable } from '@nestjs/common'; | |||
import { Context } from './context'; | |||
|
|||
@Injectable() | |||
export class CurrentContext { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check on typings
|
||
@Module({}) | ||
export class TestModule { | ||
static forRoot(max: number): DynamicModule { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename max
to something more like lruCacheMaxValue or something more readable
private readonly contextContainer: ContextContainer, | ||
private readonly asyncRequestService: AsyncRequestServiceExposer, | ||
) { | ||
this.cache = new LRUCache({ max: 50 }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleanup this