kubemq-io / kubemq-js

Apache License 2.0
9 stars 7 forks source link

I cannot make reconnection for Queries work correctly. #1

Closed Neo1975 closed 3 years ago

Neo1975 commented 3 years ago

Hi, I'm not sure this is a bug but I have some problem trying to use reconnection feature.

I'm testing two different situation: 1- start a receiver without an instance of kubemq 2- start a receiver with an instance of kubemq and shoutdown it after connection of receiver.

Looking in the code

                while (!unsubscribe) {
                    onStateChange.emit('connecting');
                    yield this.subscribeFn(request, cb).then((value) => {
                        value.onClose.on(() => {
                            isClosed = true;
                            onStateChange.emit('disconnected');
                        });
                        currentStream = value.stream;
                    });
                    let isClosed = false;
                    onStateChange.emit('connected');
                    while (!isClosed && !unsubscribe) {
                        yield new Promise((r) => setTimeout(r, 1000));
                    }
                    const reconnectionInterval = this.clientOptions.reconnectInterval;
                    if (reconnectionInterval === 0) {
                        unsubscribe = true;
                    }
                    else {
                        yield new Promise((r) => setTimeout(r, reconnectionInterval));
                    }
                }

isClosed can become true only if event value.onClose is triggered that happens only if:

            stream.on('close', () => {
                onClose.emit();
            });

In my previous scenario instead only

            stream.on('error', (e) => {
                cb(e, null);
            });

is triggered.

I also tried to call client.close() in my callback cb, without success.

Looking better deeply in the code I think that stream never emit a close.

Can you help me?

kubemq commented 3 years ago

Thank you, Will merge your PR

ukaoskid commented 8 months ago

@Neo1975 @kubemq I am still facing this problem, but it's probably because I haven't understood how to manage the client close on error. (same scenarios like starting receivers when server is down, or when server goes down)

Do you have any example?