MCPcopy
hub / github.com/grpc/grpc-go / weight

Method weight

balancer/weightedroundrobin/balancer.go:620–657  ·  view source on GitHub ↗

weight returns the current effective weight of the endpoint, taking into account the parameters. Returns 0 for blacked out or expired data, which will cause the backend weight to be treated as the mean of the weights of the other backends. If forScheduler is set to true, this function will emit met

(now time.Time, weightExpirationPeriod, blackoutPeriod time.Duration, recordMetrics bool)

Source from the content-addressed store, hash-verified

618// other backends. If forScheduler is set to true, this function will emit
619// metrics through the metrics registry.
620func (w *endpointWeight) weight(now time.Time, weightExpirationPeriod, blackoutPeriod time.Duration, recordMetrics bool) (weight float64) {
621 w.mu.Lock()
622 defer w.mu.Unlock()
623
624 if recordMetrics {
625 defer func() {
626 endpointWeightsMetric.Record(w.metricsRecorder, weight, w.target, w.locality, w.clusterName)
627 }()
628 }
629
630 // The endpoint has not received a load report (i.e. just turned READY with
631 // no load report).
632 if w.lastUpdated.Equal(time.Time{}) {
633 endpointWeightNotYetUsableMetric.Record(w.metricsRecorder, 1, w.target, w.locality, w.clusterName)
634 return 0
635 }
636
637 // If the most recent update was longer ago than the expiration period,
638 // reset nonEmptySince so that we apply the blackout period again if we
639 // start getting data again in the future, and return 0.
640 if now.Sub(w.lastUpdated) >= weightExpirationPeriod {
641 if recordMetrics {
642 endpointWeightStaleMetric.Record(w.metricsRecorder, 1, w.target, w.locality, w.clusterName)
643 }
644 w.nonEmptySince = time.Time{}
645 return 0
646 }
647
648 // If we don't have at least blackoutPeriod worth of data, return 0.
649 if blackoutPeriod != 0 && (w.nonEmptySince.Equal(time.Time{}) || now.Sub(w.nonEmptySince) < blackoutPeriod) {
650 if recordMetrics {
651 endpointWeightNotYetUsableMetric.Record(w.metricsRecorder, 1, w.target, w.locality, w.clusterName)
652 }
653 return 0
654 }
655
656 return w.weightVal
657}
658
659type backendServiceKey struct{}
660

Callers 2

endpointWeightsMethod · 0.80

Calls 4

EqualMethod · 0.65
LockMethod · 0.45
UnlockMethod · 0.45
RecordMethod · 0.45

Tested by 1