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

BRPOP doesn't actually work #1921

Open
alwhiting opened this issue Oct 9, 2024 · 1 comment
Open

BRPOP doesn't actually work #1921

alwhiting opened this issue Oct 9, 2024 · 1 comment

Comments

@alwhiting
Copy link

alwhiting commented Oct 9, 2024

Hi,

Sorry if I'm missing something obvious but BRPOP does not seem to actually resolve when a queued item becomes available. If something is present when BRPOP is initiated it immediately resolves otherwise it 'blocks' until timeout and resolves with null.

Example:

import { Redis } from 'ioredis';

(async () => {
    const redis = new Redis();

    setTimeout(() => {
        redis.lpush('some-queue', 'a queued thing');
    }, 1000);

    const start = Date.now();

    do {
        const queuedThing = await redis.brpop('some-queue', 10);
        if (!queuedThing) {
            console.log('Timeout waiting for queue, elapsed:', Date.now() - start);
            continue;
        }

        console.log('Got thing from queue:', queuedThing, 'elapsed:', Date.now() - start);
        process.exit(0);
    } while (true);

})();

Output:

ts-node example.ts
Timeout waiting for queue, elapsed: 10041
Got thing from queue: [ 'some-queue', 'a queued thing' ] elapsed: 10043

A timeout should not occur right? The loop should block for 1 second and then process the LPUSHed event and then exit.

redis-server 7.4.1
ioredis 5.4.1
node 20.15.1

edit-- I tested directly w/ redis-cli and it works as expected.

@RubensBR
Copy link

In this case you need a second connection:

import { Redis } from 'ioredis';

(async () => {
    const producer = new Redis();
    const consumer = new Redis();

    setTimeout(() => {
        producer.lpush('some-queue', 'a queued thing');
    }, 1000);

    const start = Date.now();

    do {
        const queuedThing = await consumer.brpop('some-queue', 10);
        if (!queuedThing) {
            console.log('Timeout waiting for queue, elapsed:', Date.now() - start);
            continue;
        }

        console.log('Got thing from queue:', queuedThing, 'elapsed:', Date.now() - start);
        process.exit(0);
    } while (true);

})();

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