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

Fix creation of cluster client again #2870

Merged
merged 5 commits into from
Nov 25, 2024
Merged

Fix creation of cluster client again #2870

merged 5 commits into from
Nov 25, 2024

Conversation

soccermax
Copy link
Contributor

@soccermax soccermax commented Nov 25, 2024

Description

Describe your pull request here

In the following PR #2783 we fixed the issue #2721. That fix got removed from master and therefore also in the current releases. Could you please include this fix again and release a new version? Thank you very much 👍


Checklist

  • Does npm test pass with this change (including linting)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

@soccermax
Copy link
Contributor Author

FYI @leibale

@leibale
Copy link
Contributor

leibale commented Nov 25, 2024

Sorry about it.. I'm not sure what happened, but I'll need to make sure that we did not lose more work..
BTW, the latest non "next" release still contains this fix

@leibale leibale merged commit 9a3e1c5 into redis:master Nov 25, 2024
@soccermax
Copy link
Contributor Author

Thanks for taking care 👍

@rlindner81
Copy link

@leibale I worked on this with @soccermax. We had an issue recently and we realized that the our reconnecting handler is not called even when the reconnectStrategy is used and reconnect attempts are happening.

From local debugging, it looks to me like the socket is emitting both "error" and "reconnecting"
packages/client/lib/client/socket.ts:164

                const retryIn = this.#shouldReconnect(retries++, err as Error);
                if (typeof retryIn !== 'number') {
                    throw retryIn;
                }

                this.emit('error', err);
                await promiseTimeout(retryIn);
                this.emit('reconnecting');

These events are forwarded to the client
packages/client/lib/client/index.ts:346

            .on('error', err => {
                this.emit('error', err);
                if (this.#socket.isOpen && !this.#options?.disableOfflineQueue) {
                    this.#queue.flushWaitingForReply(err);
                } else {
                    this.#queue.flushAll(err);
                }
            })
//...
            .on('reconnecting', () => this.emit('reconnecting'))

but only the error is forwarded to the cluster interface
packages/client/lib/cluster/cluster-slots.ts:331

    async #createClient(
        node: ShardNode<M, F, S>,
        readonly = node.readonly
    ) {
        const client = new this.#Client(
            this.#clientOptionsDefaults({
                socket: this.#getNodeAddress(node.address) ?? {
                    host: node.host,
                    port: node.port
                },
                readonly
            })
        );
        client.on('error', err => this.#emit('error', err));

        await client.connect();

        return client;
    }

Maybe I'm misunderstanding the client vs cluster usage intentions. I would have assumed that it makes sense to forward either event.

Our code is something like

    if (...) { //env has cluster credentials
       client = redis.createCluster(...)
    } else if () { // env has client credentials
       client = redis.createClient(...)
    } else { ... }
    client.on("error", (err) => { ... });
    client.on("reconnecting", () => { ... });

Hope you can help us understand this better. Sorry for the verbosity, but this is most easily explained directly with code :-)

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

Successfully merging this pull request may close these issues.

3 participants