async-pending-counter

This class uses very simple logic that simply waits,


Keywords
async, await, typescript, javascript, pending
License
AGPL-3.0-only
Install
npm install async-pending-counter@2.0.1

Documentation

LICENSE

Async Pending Counter

  • This class uses very simple logic that simply waits,
    rather than acquiring a lock for synchronization

Motivation

I want to execute multiple OAuth2 refresh token requests using the Web Fetch API (for example, 11 requests or more) at the same time, but all modern browsers such as chrome and firefox fail.

Trial and error has shown that a single or a small number of requests (about 5 requests) can succeed.

Therefore, I thought that a simple limit mechanism was necessary, I have implemented a module that meets that requirement.

  • Note: maybe the above issue is specific to my PC environment. In other words, we have not tested all possibilities.

Usage

// import this module
import { PendingCounter } from "async-pending-counter";
// creat instance
const pc = new PendingCounter(/* max pending */5, /* polling cycle */100);

// ...

// use it
async function refreshToken(refresh_token: string) {
    const fetchOption = {
        method: "post",
        mode: "cors",
        headers: {
            "content-type": "application/x-www-form-urlencoded",
            host: "login.eveonline.com"
        },
        body: `grant_type=refresh_token&refresh_token=${encodeURIComponent(refresh_token)}&...`
    } as RequestInit;

    let result: any;
    await pc.acquire(); // If the pending count exceeds the limit, wait for ...
    // Requires a "try catch" block to surely execute the "release" method
    try {
        result = await fetch("https://example.com/oauth/token", fetchOption).then(res => res.json());
    } catch (e) {
        throw e;
    } finally {
        pc.release();   // release the pending count
    }
 
    // ...
}

Optimization feature (experimental

Optimize the waiting time when pending reaches the upper limit. (2020-01-31

import {
    createOptimizableLock,
    OptimizablePendingCounter,
} from "async-pending-counter/optimizable";

Authors

License

This project is licensed under the AGPL-3.0 License - see the LICENSE file for details