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

bottleneck lost tracing data when using reservoir options #211

Open
lingjin666 opened this issue Feb 12, 2023 · 1 comment
Open

bottleneck lost tracing data when using reservoir options #211

lingjin666 opened this issue Feb 12, 2023 · 1 comment

Comments

@lingjin666
Copy link

lingjin666 commented Feb 12, 2023

Here is my code

import Bottleneck from 'bottleneck';
import { AsyncLocalStorage } from 'async_hooks';

const asyncLocalStorage = new AsyncLocalStorage();

const bottleneck = new Bottleneck({
    reservoir: 3,
    reservoirRefreshInterval: 5000,
    reservoirRefreshAmount: 3,

    maxConcurrent: 1,
    minTime: 1000,
});

const init = async () => {
    const num = 1;
    for (let i = 0; i < 100; i++) {
        await asyncLocalStorage.run(i, async () => {
            await bottleneck.schedule(() => {
                return asyncFun();
            });
        });
    }
};

async function asyncFun() {
    console.log(asyncLocalStorage.getStore());
}

init();

output:

0
1
2
undefined
4
5
undefined
7
8
undefined
10
11
.......

Normally, it will print from 0 to 99. But when meet 3/6/9...., it prints undefined. So why ?

$ node -v    
v18.12.1
@abou7mied
Copy link

I'm encountering the same issue, I ended up with a workaround by re-assigning the store data within the job function:

import Bottleneck from 'bottleneck'
import { AsyncLocalStorage } from 'async_hooks'

const asyncLocalStorage = new AsyncLocalStorage()

const bottleneck = new Bottleneck({
  reservoir: 3,
  reservoirRefreshInterval: 5000,
  reservoirRefreshAmount: 3,

  maxConcurrent: 1,
  minTime: 1000,
})

const init = async () => {
  const num = 1
  for (let i = 0; i < 100; i++) {
    await asyncLocalStorage.run(i, async () => {
      const store = asyncLocalStorage.getStore() // <---- read current store
      await bottleneck.schedule(() => {
        asyncLocalStorage.enterWith(store) // <---- enters with it
        return asyncFun()
      })
    })
  }
}

async function asyncFun() {
  console.log(asyncLocalStorage.getStore())
}

init()

output:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...

It's not the best, but it does the job.

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

2 participants