Make configuration values injectable rather than hardcoded
Add config structs with sensible defaults for tunable parameters: - JetStreamConfig for stream retention (1 year) and replica count (1) - HashRingConfig for virtual nodes per physical node (150) - ShardConfig for shard count (1024) and replication factor (1) Each component gets a new WithConfig constructor that accepts custom configuration, while the original constructors continue to work with defaults. Zero values in configs fall back to defaults for backward compatibility. Closes #38 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit was merged in pull request #43.
This commit is contained in:
@@ -12,13 +12,24 @@ type ConsistentHashRing struct {
|
||||
ring map[uint32]string // hash -> node ID
|
||||
sortedHashes []uint32 // sorted hash keys
|
||||
nodes map[string]bool // active nodes
|
||||
virtualNodes int // number of virtual nodes per physical node
|
||||
}
|
||||
|
||||
// NewConsistentHashRing creates a new consistent hash ring
|
||||
// NewConsistentHashRing creates a new consistent hash ring with default configuration
|
||||
func NewConsistentHashRing() *ConsistentHashRing {
|
||||
return NewConsistentHashRingWithConfig(DefaultHashRingConfig())
|
||||
}
|
||||
|
||||
// NewConsistentHashRingWithConfig creates a new consistent hash ring with custom configuration
|
||||
func NewConsistentHashRingWithConfig(config HashRingConfig) *ConsistentHashRing {
|
||||
virtualNodes := config.VirtualNodes
|
||||
if virtualNodes == 0 {
|
||||
virtualNodes = DefaultVirtualNodes
|
||||
}
|
||||
return &ConsistentHashRing{
|
||||
ring: make(map[uint32]string),
|
||||
nodes: make(map[string]bool),
|
||||
ring: make(map[uint32]string),
|
||||
nodes: make(map[string]bool),
|
||||
virtualNodes: virtualNodes,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +42,7 @@ func (chr *ConsistentHashRing) AddNode(nodeID string) {
|
||||
chr.nodes[nodeID] = true
|
||||
|
||||
// Add virtual nodes for better distribution
|
||||
for i := 0; i < VirtualNodes; i++ {
|
||||
for i := 0; i < chr.virtualNodes; i++ {
|
||||
virtualKey := fmt.Sprintf("%s:%d", nodeID, i)
|
||||
hash := chr.hash(virtualKey)
|
||||
chr.ring[hash] = nodeID
|
||||
@@ -102,4 +113,9 @@ func (chr *ConsistentHashRing) GetNodes() []string {
|
||||
// IsEmpty returns true if the ring has no nodes
|
||||
func (chr *ConsistentHashRing) IsEmpty() bool {
|
||||
return len(chr.nodes) == 0
|
||||
}
|
||||
}
|
||||
|
||||
// GetVirtualNodes returns the number of virtual nodes per physical node
|
||||
func (chr *ConsistentHashRing) GetVirtualNodes() int {
|
||||
return chr.virtualNodes
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user