Returns index of endpoint with closest hash to key's hash, which is also below the target load skipEndpoint function is used to skip endpoints we don't want, for example, fading endpoints
(key string, balanceFactor float64, ctx *routing.LBContext)
| 175 | // Returns index of endpoint with closest hash to key's hash, which is also below the target load |
| 176 | // skipEndpoint function is used to skip endpoints we don't want, for example, fading endpoints |
| 177 | func (ch *consistentHash) boundedLoadSearch(key string, balanceFactor float64, ctx *routing.LBContext) int { |
| 178 | ringIndex := ch.searchRing(key, ctx) |
| 179 | averageLoad := computeLoadAverage(ctx) |
| 180 | targetLoad := averageLoad * balanceFactor |
| 181 | // Loop round ring, starting at endpoint with closest hash. Stop when we find one whose load is less than targetLoad. |
| 182 | for i := 0; i < ch.Len(); i++ { |
| 183 | endpointIndex := ch.hashRing[ringIndex].index |
| 184 | if skipEndpoint(ctx, endpointIndex) { |
| 185 | continue |
| 186 | } |
| 187 | load := ctx.Route.LBEndpoints[endpointIndex].Metrics.InflightRequests() |
| 188 | // We know there must be an endpoint whose load <= average load. |
| 189 | // Since targetLoad >= average load (balancerFactor >= 1), there must also be an endpoint with load <= targetLoad. |
| 190 | if float64(load) <= targetLoad { |
| 191 | break |
| 192 | } |
| 193 | ringIndex = (ringIndex + 1) % ch.Len() |
| 194 | } |
| 195 | |
| 196 | return ch.hashRing[ringIndex].index |
| 197 | } |
| 198 | |
| 199 | // Apply implements routing.LBAlgorithm with a consistent hash algorithm. |
| 200 | func (ch *consistentHash) Apply(ctx *routing.LBContext) routing.LBEndpoint { |
no test coverage detected