Use fibers to store context

pull/24094/head
Diego Sampaio 4 years ago
parent 0179f3fd9c
commit 8f6d4c8128
No known key found for this signature in database
GPG Key ID: E060152B30502562
  1. 14
      ee/server/services/package-lock.json
  2. 2
      ee/server/services/package.json
  3. 7
      server/sdk/index.ts
  4. 49
      server/sdk/lib/ContextStore.ts

@ -343,6 +343,12 @@
"@types/range-parser": "*"
}
},
"@types/fibers": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/@types/fibers/-/fibers-3.1.1.tgz",
"integrity": "sha512-yHoUi46uika0snoTpNcVqUSvgbRndaIps4TUCotrXjtc0DHDoPQckmyXEZ2bX3e4mpJmyEW3hRhCwQa/ISCPaA==",
"dev": true
},
"@types/mime": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz",
@ -1273,6 +1279,14 @@
"integrity": "sha1-EOhdo4v+p/xZk0HClu4ddyZu5kA=",
"dev": true
},
"fibers": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/fibers/-/fibers-5.0.0.tgz",
"integrity": "sha512-UpGv/YAZp7mhKHxDvC1tColrroGRX90sSvh8RMZV9leo+e5+EkRVgCEZPlmXeo3BUNQTZxUaVdLskq1Q2FyCPg==",
"requires": {
"detect-libc": "^1.0.3"
}
},
"file-uri-to-path": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-2.0.0.tgz",

@ -33,6 +33,7 @@
"ejson": "^2.2.2",
"eventemitter3": "^4.0.7",
"express": "^4.17.1",
"fibers": "^5.0.0",
"jaeger-client": "^3.18.1",
"mem": "^8.1.1",
"moleculer": "^0.14.14",
@ -50,6 +51,7 @@
"@types/cookie-parser": "^1.4.2",
"@types/ejson": "^2.1.3",
"@types/express": "^4.17.13",
"@types/fibers": "^3.1.1",
"@types/mongodb": "^3.6.19",
"@types/node": "^14.17.4",
"@types/ws": "^7.4.7",

@ -1,8 +1,6 @@
import { AsyncLocalStorage } from 'async_hooks';
import { IServiceContext } from './types/ServiceClass';
import { proxify, proxifyWithWait } from './lib/proxify';
import { IAuthorization } from './types/IAuthorization';
import { IServiceContext } from './types/ServiceClass';
import { IPresence } from './types/IPresence';
import { IAccount } from './types/IAccount';
import { ILicense } from './types/ILicense';
@ -16,6 +14,7 @@ import { IRoomService } from './types/IRoomService';
import { IMediaService } from './types/IMediaService';
import { IAnalyticsService } from './types/IAnalyticsService';
import { ILDAPService } from './types/ILDAPService';
import { ContextStore } from './lib/ContextStore';
// TODO think in a way to not have to pass the service name to proxify here as well
export const Authorization = proxifyWithWait<IAuthorization>('authorization');
@ -36,4 +35,4 @@ export const LDAP = proxifyWithWait<ILDAPService>('ldap');
// of service/method not available
export const EnterpriseSettings = proxify<IEnterpriseSettings>('ee-settings');
export const asyncLocalStorage = new AsyncLocalStorage<IServiceContext>();
export const asyncLocalStorage = new ContextStore<IServiceContext>();

@ -0,0 +1,49 @@
import Fiber from 'fibers';
// import { AsyncLocalStorage } from 'async_hooks';
// export class ContextStore<T> {
// store: AsyncLocalStorage<T>;
// constructor() {
// this.store = new AsyncLocalStorage<T>();
// }
// getStore(): T | undefined {
// return this.store.getStore();
// }
// run(store: T, callback: () => void, ...args: any): void {
// return this.store.run(store, callback, ...args);
// }
// }
export class ContextStore<T extends object> {
// store: AsyncLocalStorage<T>;
// constructor() {
// this.store = new AsyncLocalStorage<T>();
// }
getStore(): T | undefined {
return Fiber.current as unknown as T;
}
run(store: T, callback: (...args: any) => void, ...args: any): void {
// eslint-disable-next-line new-cap
return Fiber((...rest: any) => {
const fiber = Fiber.current as Record<any, any>;
// console.log('store ->', store);
for (const key in store) {
if (store.hasOwnProperty(key)) {
// console.log('store ->', key, store[key]);
fiber[key] = store[key];
}
}
// Object.keys(store).forEach((key) => {
// fiber[key] = store[key];
// });
Fiber.yield(callback(...rest));
}).run(...args);
}
}
Loading…
Cancel
Save