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
5 changes: 5 additions & 0 deletions .changeset/brave-rings-update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Update existing `HashRing` nodes when adding a value with the same primary key.
1 change: 1 addition & 0 deletions packages/effect/src/HashRing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export const addMany: {
const key = PrimaryKey.value(node)
const entry = self.nodes.get(key)
if (entry) {
entry[0] = node
if (entry[1] === weight) continue
toRemove ??= new Set()
toRemove.add(key)
Expand Down
47 changes: 47 additions & 0 deletions packages/effect/test/HashRing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { assert, describe, it } from "@effect/vitest"
import * as HashRing from "effect/HashRing"
import * as PrimaryKey from "effect/PrimaryKey"

describe("HashRing", () => {
it("updates the stored node when adding the same primary key", () => {
const first = {
name: "first",
[PrimaryKey.symbol]() {
return "node"
}
}
const updated = {
name: "updated",
[PrimaryKey.symbol]() {
return "node"
}
}
const ring = HashRing.make<typeof first>()

HashRing.add(ring, first)
HashRing.add(ring, updated)

assert.strictEqual(HashRing.get(ring, "request"), updated)
})

it("updates the stored node when changing its weight", () => {
const first = {
name: "first",
[PrimaryKey.symbol]() {
return "node"
}
}
const updated = {
name: "updated",
[PrimaryKey.symbol]() {
return "node"
}
}
const ring = HashRing.make<typeof first>()

HashRing.add(ring, first)
HashRing.add(ring, updated, { weight: 2 })

assert.strictEqual(HashRing.get(ring, "request"), updated)
})
})
Loading