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

Allow Async\Semaphore to wait for different types #166

Open
Atry opened this issue Nov 23, 2021 · 1 comment
Open

Allow Async\Semaphore to wait for different types #166

Atry opened this issue Nov 23, 2021 · 1 comment

Comments

@Atry
Copy link
Contributor

Atry commented Nov 23, 2021

I use Async\Semaphore to limit the concurrency in hhvm/hhast#399, which works good. But the current design of Async\Semaphore does not support the use cases where the waitForAsync callers expect different types. I wonder if we could support the following type signature, so that we can use the same Semaphore limit the concurrency of heterogeneous job types.

final class Semaphore {
  public function __construct(
    private int $concurrentLimit,
  );

  public async function waitForAsync<T>((function():Awaitable<T>) $value): Awaitable<T>;
}
@Atry
Copy link
Contributor Author

Atry commented Nov 25, 2021

Another idea is to use IAsyncDisposable to release the worker instead of a closure.

final class Semaphore {
  public function __construct(
    private int $concurrentLimit,
  );

  public function aquireWorkerAsync(): Awaitable<IAsyncDisposable>;
}
// Usage
$worker_pool = new Semaphore(2);

async function my_script(vec<string> $files_to_lint): Awaitable<void> {
  $lint_results = Vec\map(
    $files_to_lint,
    $file ==> {
      await using($_worker = await $worker_pool->aquireWorkerAsync()) {
        return call_hh_client_linter($file);
      }
    }
  );
}

This approach is better than the closure approach because it could keep the call stack clean. In the closure approach, when an exception is thrown from the closure, the call stack would be noisy and hard to debug.

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

No branches or pull requests

1 participant