Skip to content

Commit d3b37b3

Browse files
committed
refactor: Re-add empty-string guard in setPoolLoadBalancingAlgorithm
With *string for LoadBalancingAlgorithm on this branch, makeEndpoint produces "" when the NATS field is absent (keepalive registrations). The guard prevents keepalives from triggering spurious algorithm changes or error logs.
1 parent 5968d85 commit d3b37b3

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/code.cloudfoundry.org/gorouter/route/pool.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,12 @@ func (p *EndpointPool) MarshalJSON() ([]byte, error) {
667667
}
668668

669669
// setPoolLoadBalancingAlgorithm overwrites the load balancing algorithm of a pool by that of a specified endpoint, if that is valid.
670+
// An empty algorithm (from a keepalive registration without algorithm field) is treated as "no change".
670671
func (p *EndpointPool) setPoolLoadBalancingAlgorithm(endpoint *Endpoint) {
672+
if endpoint.LoadBalancingAlgorithm == "" {
673+
return
674+
}
675+
671676
if endpoint.LoadBalancingAlgorithm != p.LoadBalancingAlgorithm {
672677
if config.IsLoadBalancingAlgorithmValid(endpoint.LoadBalancingAlgorithm) {
673678
previousAlgorithm := p.LoadBalancingAlgorithm

src/code.cloudfoundry.org/gorouter/route/pool_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,34 @@ var _ = Describe("EndpointPool", func() {
505505
Expect(pool.HashLookupTable).To(BeNil())
506506
Expect(pool.HashRoutingProperties).To(BeNil())
507507
})
508+
509+
It("keeps the current algorithm when the endpoint does not specify one", func() {
510+
pool := route.NewPool(&route.PoolOpts{
511+
Logger: logger.Logger,
512+
LoadBalancingAlgorithm: config.LOAD_BALANCE_RR,
513+
})
514+
515+
// Set up HB routing
516+
hbEndpoint := route.NewEndpoint(&route.EndpointOpts{
517+
Host: "host-1",
518+
Port: 1234,
519+
PrivateInstanceId: "id-1",
520+
LoadBalancingAlgorithm: config.LOAD_BALANCE_HB,
521+
HashBalanceFactor: 1.25,
522+
HashHeaderName: "X-Tenant",
523+
})
524+
pool.Put(hbEndpoint)
525+
Expect(pool.LoadBalancingAlgorithm).To(Equal(config.LOAD_BALANCE_HB))
526+
527+
// Register with empty algorithm (keepalive, field not specified) — should NOT reset
528+
noAlgoEndpoint := route.NewEndpoint(&route.EndpointOpts{
529+
Host: "host-1",
530+
Port: 1234,
531+
PrivateInstanceId: "id-1",
532+
})
533+
pool.Put(noAlgoEndpoint)
534+
Expect(pool.LoadBalancingAlgorithm).To(Equal(config.LOAD_BALANCE_HB))
535+
})
508536
})
509537
It("returns the route_service_url associated with the pool", func() {
510538
endpoint := &route.Endpoint{}

0 commit comments

Comments
 (0)