Skip to content

Feature / Async Local Storage Alternative #1054

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

Merged
merged 5 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@opentelemetry/resources": "1.25.1",
"@opentelemetry/sdk-node": "0.52.1",
"@opentelemetry/sdk-trace-base": "1.25.1",
"async-mutex": "0.5.0",
"class-validator": "0.14.1",
"dataloader": "2.2.2",
"graphql": "16.9.0",
Expand Down
35 changes: 31 additions & 4 deletions src/packages/core/src/request-context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { AsyncLocalStorage } from 'async_hooks';
import { BaseLoader } from './base-loader';
import { AsyncLocalStorage } from 'node:async_hooks';
import { logger } from '@exogee/logger';
import { Mutex } from 'async-mutex';

import { BaseLoader } from './base-loader';

type Context = {
BaseLoaders: BaseLoader;
Expand All @@ -10,6 +12,10 @@ export class RequestContext {
private static storage = new AsyncLocalStorage<RequestContext>();
private static counter = 1;

// Workaround for environments that don't support AsyncLocalStorage
private static workaroundStorage: RequestContext | undefined;
private static mutex = new Mutex();

readonly id = RequestContext.counter++;

constructor(readonly context: Context) {}
Expand All @@ -27,11 +33,32 @@ export class RequestContext {
static async create<T>(next: (...args: any[]) => T): Promise<T> {
const ctx = RequestContext.createContext();
logger.trace(`Creating RequestContext with ID: ${ctx.id}`);
return RequestContext.storage.run(ctx, next);

// WebContainers don't support AsyncLocalStorage:
// https://github.com/stackblitz/webcontainer-core/issues/1169
//
// To support use cases like this, if you want us to, we'll just use a singleton
// scratch pad that we mutex access to, which will enforce that only one request
// can happen at a time, but will not rely on the AsyncLocalStorage API.
//
// In standard Node this is not an issue, but if you need to turn off support for
// isolated async contexts, you can do so with this environment variable.
if (process.env.GRAPHWEAVER_DISABLE_ASYNC_LOCAL_STORAGE === 'true') {
logger.trace('AsyncLocalStorage is disabled, using workaround storage, creating context.');
RequestContext.workaroundStorage = ctx;
return await RequestContext.mutex.runExclusive(next);
} else {
logger.trace('Creating new AsyncLocalStorage context.');
return RequestContext.storage.run(ctx, next);
}
}

static currentRequestContext(): RequestContext | undefined {
return RequestContext.storage.getStore();
if (process.env.GRAPHWEAVER_DISABLE_ASYNC_LOCAL_STORAGE === 'true') {
return RequestContext.workaroundStorage;
} else {
return RequestContext.storage.getStore();
}
}

private static createContext(): RequestContext {
Expand Down
3 changes: 3 additions & 0 deletions src/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading