From 0d2b259fd4c8fb7a0f69cb56b59a8b35c15290ba Mon Sep 17 00:00:00 2001 From: stanley2058 Date: Tue, 16 Sep 2025 13:36:21 +0800 Subject: [PATCH 1/2] fix: use current value instead of return value --- src/y-socket-io/y-socket-io.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/y-socket-io/y-socket-io.js b/src/y-socket-io/y-socket-io.js index 7337815..38645cb 100644 --- a/src/y-socket-io/y-socket-io.js +++ b/src/y-socket-io/y-socket-io.js @@ -859,10 +859,13 @@ export class YSocketIO { assert(this.client) const redis = this.client.redis const key = this.getLeaderKeyOf(namespace) - const ok = await redis.set(key, this.serverId, { + await redis.set(key, this.serverId, { NX: true, PX: PERSIST_LEADER_HEARTBEAT_INTERVAL }) + + const curLeader = await redis.get(key) + const ok = curLeader === this.serverId if (!ok) return false this.persistentLeaderOf.add(namespace) From cdc04ed566aea47764f00e634c19e07ae6612b4f Mon Sep 17 00:00:00 2001 From: stanley2058 Date: Tue, 16 Sep 2025 14:05:34 +0800 Subject: [PATCH 2/2] fix: use GET option to avoid additional call --- src/y-socket-io/y-socket-io.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/y-socket-io/y-socket-io.js b/src/y-socket-io/y-socket-io.js index 38645cb..3f5cd9d 100644 --- a/src/y-socket-io/y-socket-io.js +++ b/src/y-socket-io/y-socket-io.js @@ -859,13 +859,13 @@ export class YSocketIO { assert(this.client) const redis = this.client.redis const key = this.getLeaderKeyOf(namespace) - await redis.set(key, this.serverId, { + const prevVal = await redis.set(key, this.serverId, { NX: true, - PX: PERSIST_LEADER_HEARTBEAT_INTERVAL + PX: PERSIST_LEADER_HEARTBEAT_INTERVAL, + GET: true }) - const curLeader = await redis.get(key) - const ok = curLeader === this.serverId + const ok = prevVal === this.serverId || prevVal === null if (!ok) return false this.persistentLeaderOf.add(namespace)