refactor(core): Make setting and expiration of leader key atomic (#14702)

This commit is contained in:
Iván Ovejero
2025-04-17 10:36:37 +02:00
committed by GitHub
parent 77e2ac6495
commit 048df28d5f
2 changed files with 8 additions and 7 deletions

View File

@@ -108,15 +108,17 @@ export class MultiMainSetup extends TypedEmitter<MultiMainEvents> {
const { hostId } = this.instanceSettings;
// this can only succeed if leadership is currently vacant
const keySetSuccessfully = await this.publisher.setIfNotExists(this.leaderKey, hostId);
const keySetSuccessfully = await this.publisher.setIfNotExists(
this.leaderKey,
hostId,
this.leaderKeyTtl,
);
if (keySetSuccessfully) {
this.logger.debug(`[Instance ID ${hostId}] Leader is now this instance`);
this.instanceSettings.markAsLeader();
await this.publisher.setExpiration(this.leaderKey, this.leaderKeyTtl);
this.emit('leader-takeover');
} else {
this.instanceSettings.markAsFollower();