mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-19 23:41:38 +02:00
- 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
30 lines
704 B
Go
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]
|
|
}
|