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

Revise ResourceManager mutexing implementation with a decorator pattern #649

Open
jlrobins opened this issue Nov 20, 2024 · 0 comments
Open
Assignees
Labels
cleanup General refactoring and/or minor adjustments needed that shouldn't impact overall functionality good first issue Good for newcomers

Comments

@jlrobins
Copy link
Contributor

jlrobins commented Nov 20, 2024

Revise work done in #646 to use a decorator pattern instead. Decorator should take a parameter -- the key for which mutex to use.

And then write a new prove-race-proof test for at least one of the methods.

Sample method decorator:

function mutexWrapper(key: string) {
  // target is the class, propertyKey is the method name, descriptor is the method descriptor
  return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    const originalMethod = descriptor.value;

    descriptor.value = async function (...args: any[]) {
      console.log("mutexWrapper() start", { methodName: propertyKey, key});
      const result = await originalMethod(args);
      console.log("mutexWrapper() end", { methodName: propertyKey, key});
      return result;
    };

    return descriptor;
  };
}

class ResourceManager {
  constructor() {}
 
  @mutexWrapper("FOO")
  async readAndWrite() {
    console.log("readAndWrite()");
  }

  @mutexWrapper("BAR")
  async readAndDelete() {
    console.log("readAndDelete()");
  }
}

async function main() {
  const resourceManager = new ResourceManager();
  await resourceManager.readAndWrite();
  await resourceManager.readAndDelete();
}

main()

Output:

mutexWrapper() start { methodName: 'readAndWrite', key: 'FOO' }
readAndWrite()
mutexWrapper() end { methodName: 'readAndWrite', key: 'FOO' }
mutexWrapper() start { methodName: 'readAndDelete', key: 'BAR' }
readAndDelete()
mutexWrapper() end { methodName: 'readAndDelete', key: 'BAR' }

Note

This does require adding "experimentalDecorators": true into the tsconfig.json under compilerOptions, otherwise we'll see errors like:

Unable to resolve signature of method decorator when called as an expression.
  The runtime will invoke the decorator with 2 arguments, but the decorator expects 3.ts(1241)
@jlrobins jlrobins self-assigned this Nov 20, 2024
@jlrobins jlrobins added good first issue Good for newcomers cleanup General refactoring and/or minor adjustments needed that shouldn't impact overall functionality labels Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup General refactoring and/or minor adjustments needed that shouldn't impact overall functionality good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant