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 undefined current context and memory leak with non HTTP events #25

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

seiwan
Copy link

@seiwan 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

@seiwan seiwan requested a review from Rzial March 23, 2023 15:18
@seiwan seiwan self-assigned this Mar 23, 2023
@@ -246,6 +246,38 @@ export class MyProvider {
}
```

Copy link

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>;

Copy link

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();

Copy link

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 {
Copy link

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) {
Copy link

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 {
Copy link

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 {
Copy link

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 });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants