mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-22 21:32:34 +02:00
29 lines
No EOL
573 B
Go
Executable file
29 lines
No EOL
573 B
Go
Executable file
package main
|
|
|
|
import "sync/atomic"
|
|
|
|
type httpLoadBalancePool struct {
|
|
pool []*HTTPRoute
|
|
curentIndex atomic.Int32
|
|
}
|
|
|
|
func NewHTTPLoadBalancePool() *httpLoadBalancePool {
|
|
return &httpLoadBalancePool{
|
|
pool: make([]*HTTPRoute, 0),
|
|
}
|
|
}
|
|
|
|
func (p *httpLoadBalancePool) Add(route *HTTPRoute) {
|
|
p.pool = append(p.pool, route)
|
|
}
|
|
|
|
func (p *httpLoadBalancePool) Iterator() []*HTTPRoute {
|
|
return p.pool
|
|
}
|
|
|
|
func (p *httpLoadBalancePool) Pick() *HTTPRoute {
|
|
// round-robin
|
|
index := int(p.curentIndex.Load())
|
|
defer p.curentIndex.Add(1)
|
|
return p.pool[index%len(p.pool)]
|
|
} |