()
| 513 | const maximumNodeLatency = 1 * time.Minute |
| 514 | |
| 515 | func (n *clusterNode) updateLatency() { |
| 516 | const numProbe = 10 |
| 517 | var dur uint64 |
| 518 | |
| 519 | successes := 0 |
| 520 | for i := 0; i < numProbe; i++ { |
| 521 | time.Sleep(time.Duration(10+rand.Intn(10)) * time.Millisecond) |
| 522 | |
| 523 | start := time.Now() |
| 524 | err := n.Client.Ping(context.TODO()).Err() |
| 525 | if err == nil { |
| 526 | dur += uint64(time.Since(start) / time.Microsecond) |
| 527 | successes++ |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | var latency float64 |
| 532 | if successes == 0 { |
| 533 | // If none of the pings worked, set latency to some arbitrarily high value so this node gets |
| 534 | // least priority. |
| 535 | latency = float64(maximumNodeLatency / time.Microsecond) |
| 536 | } else { |
| 537 | latency = float64(dur) / float64(successes) |
| 538 | } |
| 539 | atomic.StoreUint32(&n.latency, uint32(latency+0.5)) |
| 540 | n.SetLastLatencyMeasurement(time.Now()) |
| 541 | } |
| 542 | |
| 543 | func (n *clusterNode) Latency() time.Duration { |
| 544 | latency := atomic.LoadUint32(&n.latency) |
no test coverage detected