Source File
metrics.go
Belonging Package
github.com/libp2p/go-libp2p/p2p/host/peerstore
package peerstoreimport ()// LatencyEWMASmoothing governs the decay of the EWMA (the speed// at which it changes). This must be a normalized (0-1) value.// 1 is 100% change, 0 is no change.var LatencyEWMASmoothing = 0.1type metrics struct {mutex sync.RWMutexlatmap map[peer.ID]time.Duration}func () *metrics {return &metrics{latmap: make(map[peer.ID]time.Duration),}}// RecordLatency records a new latency measurementfunc ( *metrics) ( peer.ID, time.Duration) {:= float64():= LatencyEWMASmoothingif > 1 || < 0 {= 0.1 // ignore the knob. it's broken. look, it jiggles.}.mutex.Lock(), := .latmap[]:= float64()if ! {.latmap[] = // when no data, just take it as the mean.} else {= ((1.0 - ) * ) + ( * ).latmap[] = time.Duration()}.mutex.Unlock()}// LatencyEWMA returns an exponentially-weighted moving avg.// of all measurements of a peer's latency.func ( *metrics) ( peer.ID) time.Duration {.mutex.RLock()defer .mutex.RUnlock()return .latmap[]}func ( *metrics) ( peer.ID) {.mutex.Lock()delete(.latmap, ).mutex.Unlock()}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |