Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion packages/client/lib/client/linked-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { equal, deepEqual } from "assert/strict";

describe("DoublyLinkedList", () => {
const list = new DoublyLinkedList();
const list = new DoublyLinkedList<number>();

it("should start empty", () => {
equal(list.length, 0);
Expand Down Expand Up @@ -96,6 +96,38 @@ describe("DoublyLinkedList", () => {
}
equal(count, 6);
});

it("should handle remove on empty list", () => {
list.reset();
const node = list.push(1);
list.remove(node);
equal(list.length, 0);
deepEqual(Array.from(list), []);
list.remove(node);
equal(list.length, 0);
deepEqual(Array.from(list), []);
});


it("should safely remove nodes while iterating", () => {
list.reset();
list.push(1);
list.push(2);
list.push(3);
list.push(4);
list.push(5);

const visited: number[] = [];
for (const node of list.nodes()) {
visited.push(node.value);
if (node.value % 2 === 0) {
list.remove(node);
}
}
deepEqual(visited, [1, 2, 3, 4, 5]);
equal(list.length, 3);
deepEqual(Array.from(list), [1, 3, 5]);
});
});

describe("SinglyLinkedList", () => {
Expand Down
21 changes: 13 additions & 8 deletions packages/client/lib/client/linked-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class DoublyLinkedList<T> {
++this.#length;

if (this.#tail === undefined) {
return this.#tail = this.#head = {
return this.#head = this.#tail = {
previous: this.#head,
next: undefined,
value
Expand Down Expand Up @@ -73,7 +73,7 @@ export class DoublyLinkedList<T> {
--this.#length;
const node = this.#head;
if (node.next) {
node.next.previous = node.previous;
node.next.previous = undefined;
this.#head = node.next;
node.next = undefined;
} else {
Expand All @@ -83,19 +83,23 @@ export class DoublyLinkedList<T> {
}

remove(node: DoublyLinkedNode<T>) {
if (this.#length === 0) return;
--this.#length;

if (this.#tail === node) {
this.#tail = node.previous;
}

}
if (this.#head === node) {
this.#head = node.next;
} else {
node.previous!.next = node.next;
node.previous = undefined;
if (node.previous) {
node.previous.next = node.next;
}
if (node.next) {
node.next.previous = node.previous;
}
}

node.previous = undefined;
node.next = undefined;
}

Expand All @@ -115,8 +119,9 @@ export class DoublyLinkedList<T> {
*nodes() {
let node = this.#head;
while(node) {
const next = node.next
yield node;
node = node.next;
node = next;
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions packages/client/lib/sentinel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,16 @@ class RedisSentinelInternal<
}
}

#handleSentinelFailure(node: RedisNode) {
const found = this.#sentinelRootNodes.findIndex(
(rootNode) => rootNode.host === node.host && rootNode.port === node.port
);
if (found !== -1) {
this.#sentinelRootNodes.splice(found, 1);
}
this.#reset();
}

async close() {
this.#destroy = true;

Expand Down Expand Up @@ -1197,8 +1207,9 @@ class RedisSentinelInternal<
error: err
};
this.emit('client-error', event);
this.#reset();
});
this.#handleSentinelFailure(node);
})
.on('end', () => this.#handleSentinelFailure(node));
this.#sentinelClient = client;

this.#trace(`transform: adding sentinel client connect() to promise list`);
Expand Down
Loading