Files
godoxy/internal/net/gphttp/loadbalancer/round_robin.go
yusing d33ff2192a refactor(loadbalancer): implement sticky sessions and improve algorithm separation
- Refactor load balancer interface to separate server selection (ChooseServer) from request handling
- Add cookie-based sticky session support with configurable max-age and secure cookie handling
- Integrate idlewatcher requests with automatic sticky session assignment
- Improve algorithm implementations:
  * Replace fnv with xxhash3 for better performance in IP hash and server keys
  * Add proper bounds checking and error handling in all algorithms
  * Separate concerns between server selection and request processing
- Add Sticky and StickyMaxAge fields to LoadBalancerConfig
- Create dedicated sticky.go for session management utilities
2025-11-07 15:24:57 +08:00

30 lines
704 B
Go

package loadbalancer
import (
"net/http"
"sync/atomic"
"github.com/yusing/godoxy/internal/types"
)
type roundRobin struct {
index atomic.Uint32
}
var _ impl = (*roundRobin)(nil)
func (*LoadBalancer) newRoundRobin() impl { return &roundRobin{} }
func (lb *roundRobin) OnAddServer(srv types.LoadBalancerServer) {}
func (lb *roundRobin) OnRemoveServer(srv types.LoadBalancerServer) {}
func (lb *roundRobin) ChooseServer(srvs types.LoadBalancerServers, r *http.Request) types.LoadBalancerServer {
if len(srvs) == 0 {
return nil
}
index := lb.index.Add(1) % uint32(len(srvs))
if lb.index.Load() >= 2*uint32(len(srvs)) {
lb.index.Store(0)
}
return srvs[index]
}