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
()
| 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. |
| 47 | func (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. |
nothing calls this directly
no outgoing calls
no test coverage detected