MCPcopy Create free account
hub / github.com/appleboy/loadbalancer-algorithms / NextServer

Method NextServer

roundrobin/roundrobin.go:47–59  ·  view source on GitHub ↗

NextServer returns the next server in the round-robin algorithm. If there are no servers available, it returns nil. The server selection is based on an atomic counter that increments with each call. The selected server is determined by calculating the index using the modulo operation. This method is

()

Source from the content-addressed store, hash-verified

45// The selected server is determined by calculating the index using the modulo operation.
46// This method is thread-safe using atomic operations and read locks.
47func (r *roundrobin) NextServer() *proxy.Proxy {
48 index := atomic.AddUint32(&r.next, 1)
49
50 r.RLock()
51 count := uint32(len(r.servers))
52 if count == 0 {
53 r.RUnlock()
54 return nil
55 }
56 server := r.servers[(index-1)%count]
57 r.RUnlock()
58 return server
59}
60
61// AddServers adds the given servers to the roundrobin load balancer.
62// It takes a variadic parameter of type *proxy.Proxy, representing the servers to be added.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected