Skip to content

Latency-based node selection#321

Merged
meling merged 8 commits intomasterfrom
meling/320/latency-based-node-selection
Apr 11, 2026
Merged

Latency-based node selection#321
meling merged 8 commits intomasterfrom
meling/320/latency-based-node-selection

Conversation

@meling
Copy link
Copy Markdown
Member

@meling meling commented Apr 9, 2026

Add Configuration.SortBy and the Latency node comparator so callers can explicitly order or subset nodes by observed round-trip latency without hiding mutable network measurements behind implicit call behavior.

  • internal/stream: add NewMessageRouterWithLatency test helper
  • node.go: expand Node.Latency() doc with measurement limits; add Latency comparator (compatible with slices.SortFunc and Configuration.SortBy)
  • config.go: add Configuration.SortBy using stable sort
  • node_test.go: tests for Latency comparator ordering and tie-breaking
  • config_test.go: tests for SortBy including empty config and composition
  • doc/user-guide.md: new "Latency-Based Node Selection" section covering fast sub-configurations, re-sort frequency guidance, and measurement limits

Note: Using Configuration.SortBy does not magically make quorum calls faster, since quorum calls contact all nodes independent of their placement in the Configuration slice. Thus, to take advantage of a latency-sorted configuration, a quorum call's Configuration should be sliced to the fastest quorum size. This assumes that the quorum call does not update the Node's state, which should be updated on all nodes. However, for read-only quorum calls, using only the fastest quorum size should be fine.

Co-Authored-By: Claude Sonnet 4.6

Fixes #320

Copilot AI review requested due to automatic review settings April 9, 2026 21:02
@deepsource-io
Copy link
Copy Markdown
Contributor

deepsource-io Bot commented Apr 9, 2026

DeepSource Code Review

We reviewed changes in 6caf79e...ad82cb4 on this pull request. Below is the summary for the review, and you can see the individual issues we found as inline review comments.

See full review on DeepSource ↗

PR Report Card

Overall Grade   Security  

Reliability  

Complexity  

Hygiene  

Code Review Summary

Analyzer Status Updated (UTC) Details
Go Apr 11, 2026 10:06a.m. Review ↗
Shell Apr 11, 2026 10:06a.m. Review ↗

Important

AI Review is run only on demand for your team. We're only showing results of static analysis review right now. To trigger AI Review, comment @deepsourcebot review on this thread.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an explicit, caller-controlled way to order and subset configurations by observed round-trip latency, avoiding implicit behavior changes while enabling “fast node” selection patterns.

Changes:

  • Add Configuration.SortBy (stable sort + copy) to derive ordered configurations without mutating the original.
  • Add a gorums.Latency node comparator (with “unmeasured last” semantics) and expand Node.Latency() documentation.
  • Add tests for the comparator and SortBy, plus user-guide documentation describing how to apply latency-based selection safely.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
node.go Expands Node.Latency() docs and introduces Latency comparator alongside existing comparators.
config.go Introduces Configuration.SortBy using stable sort over a cloned node slice.
internal/stream/router.go Adds a test helper constructor to preset router latency.
node_test.go Adds unit tests for latency comparator ordering and tie-breaking composition with ID.
config_test.go Adds tests for SortBy behavior (size preservation, immutability, empty handling, composition).
doc/user-guide.md Documents latency-based node selection patterns, cautions, and re-sort guidance.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/stream/router.go Outdated
Comment thread doc/user-guide.md Outdated
Comment thread doc/user-guide.md Outdated
Comment thread config_test.go
meling added 2 commits April 10, 2026 11:40
Add Configuration.SortBy and the Latency node comparator so callers can
explicitly order or subset nodes by observed round-trip latency without
hiding mutable network measurements behind implicit call behavior.

- internal/stream: add NewMessageRouterWithLatency test helper
- node.go: expand Node.Latency() doc with measurement limits; add Latency
  comparator (compatible with slices.SortFunc and Configuration.SortBy)
- config.go: add Configuration.SortBy using stable sort
- node_test.go: tests for Latency comparator ordering and tie-breaking
- config_test.go: tests for SortBy including empty config and composition
- doc/user-guide.md: new "Latency-Based Node Selection" section covering
  fast sub-configurations, re-sort frequency guidance, and measurement limits

Fixes #320
Introduces Configuration.Watch, a new method that starts a background goroutine
to periodically re-derive a sub-configuration and emit it on a channel only when
it differs from the previous result. The initial result is always emitted before
the first tick, ensuring callers receive a valid configuration immediately.

The returned channel has a 1-element buffer; if the consumer is slow, emissions
are skipped and re-evaluated on the next tick.

This replaces the bespoke mutex-based refresh pattern previously documented in
the user guide. Examples are updated to show common use cases: latency-based
top-k selection and error-filtering with latency sorting.

Also adds SetLatency test helper to MessageRouter to support configuration
watch tests via latency simulation.
@meling meling force-pushed the meling/320/latency-based-node-selection branch from 8b24cfd to 87a7ef4 Compare April 10, 2026 11:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread config.go
Comment thread doc/user-guide.md
meling added 5 commits April 11, 2026 10:15
The Latency comparator orders nodes without latency measurements last.
The LLM hallucinated some bad calculations for the quorum size and
tolerated failures value.
This fixes some LLM misconceptions in the measurement limits section and
adds some additional limitations to clarify what the latency measurement
is actually measuring (RPC compute included and shared across all RPCs
hosted on the node).
@meling meling merged commit fb4423d into master Apr 11, 2026
5 checks passed
@meling meling deleted the meling/320/latency-based-node-selection branch April 11, 2026 10:24
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.

feat: add support for latency-based node selection

2 participants