package metrics
import (
"time"
"github.com/libp2p/go-flow-metrics"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
)
type BandwidthCounter struct {
totalIn flow .Meter
totalOut flow .Meter
protocolIn flow .MeterRegistry
protocolOut flow .MeterRegistry
peerIn flow .MeterRegistry
peerOut flow .MeterRegistry
}
func NewBandwidthCounter () *BandwidthCounter {
return new (BandwidthCounter )
}
func (bwc *BandwidthCounter ) LogSentMessage (size int64 ) {
bwc .totalOut .Mark (uint64 (size ))
}
func (bwc *BandwidthCounter ) LogRecvMessage (size int64 ) {
bwc .totalIn .Mark (uint64 (size ))
}
func (bwc *BandwidthCounter ) LogSentMessageStream (size int64 , proto protocol .ID , p peer .ID ) {
bwc .protocolOut .Get (string (proto )).Mark (uint64 (size ))
bwc .peerOut .Get (string (p )).Mark (uint64 (size ))
}
func (bwc *BandwidthCounter ) LogRecvMessageStream (size int64 , proto protocol .ID , p peer .ID ) {
bwc .protocolIn .Get (string (proto )).Mark (uint64 (size ))
bwc .peerIn .Get (string (p )).Mark (uint64 (size ))
}
func (bwc *BandwidthCounter ) GetBandwidthForPeer (p peer .ID ) (out Stats ) {
inSnap := bwc .peerIn .Get (string (p )).Snapshot ()
outSnap := bwc .peerOut .Get (string (p )).Snapshot ()
return Stats {
TotalIn : int64 (inSnap .Total ),
TotalOut : int64 (outSnap .Total ),
RateIn : inSnap .Rate ,
RateOut : outSnap .Rate ,
}
}
func (bwc *BandwidthCounter ) GetBandwidthForProtocol (proto protocol .ID ) (out Stats ) {
inSnap := bwc .protocolIn .Get (string (proto )).Snapshot ()
outSnap := bwc .protocolOut .Get (string (proto )).Snapshot ()
return Stats {
TotalIn : int64 (inSnap .Total ),
TotalOut : int64 (outSnap .Total ),
RateIn : inSnap .Rate ,
RateOut : outSnap .Rate ,
}
}
func (bwc *BandwidthCounter ) GetBandwidthTotals () (out Stats ) {
inSnap := bwc .totalIn .Snapshot ()
outSnap := bwc .totalOut .Snapshot ()
return Stats {
TotalIn : int64 (inSnap .Total ),
TotalOut : int64 (outSnap .Total ),
RateIn : inSnap .Rate ,
RateOut : outSnap .Rate ,
}
}
func (bwc *BandwidthCounter ) GetBandwidthByPeer () map [peer .ID ]Stats {
peers := make (map [peer .ID ]Stats )
bwc .peerIn .ForEach (func (p string , meter *flow .Meter ) {
id := peer .ID (p )
snap := meter .Snapshot ()
stat := peers [id ]
stat .TotalIn = int64 (snap .Total )
stat .RateIn = snap .Rate
peers [id ] = stat
})
bwc .peerOut .ForEach (func (p string , meter *flow .Meter ) {
id := peer .ID (p )
snap := meter .Snapshot ()
stat := peers [id ]
stat .TotalOut = int64 (snap .Total )
stat .RateOut = snap .Rate
peers [id ] = stat
})
return peers
}
func (bwc *BandwidthCounter ) GetBandwidthByProtocol () map [protocol .ID ]Stats {
protocols := make (map [protocol .ID ]Stats )
bwc .protocolIn .ForEach (func (p string , meter *flow .Meter ) {
id := protocol .ID (p )
snap := meter .Snapshot ()
stat := protocols [id ]
stat .TotalIn = int64 (snap .Total )
stat .RateIn = snap .Rate
protocols [id ] = stat
})
bwc .protocolOut .ForEach (func (p string , meter *flow .Meter ) {
id := protocol .ID (p )
snap := meter .Snapshot ()
stat := protocols [id ]
stat .TotalOut = int64 (snap .Total )
stat .RateOut = snap .Rate
protocols [id ] = stat
})
return protocols
}
func (bwc *BandwidthCounter ) Reset () {
bwc .totalIn .Reset ()
bwc .totalOut .Reset ()
bwc .protocolIn .Clear ()
bwc .protocolOut .Clear ()
bwc .peerIn .Clear ()
bwc .peerOut .Clear ()
}
func (bwc *BandwidthCounter ) TrimIdle (since time .Time ) {
bwc .peerIn .TrimIdle (since )
bwc .peerOut .TrimIdle (since )
bwc .protocolIn .TrimIdle (since )
bwc .protocolOut .TrimIdle (since )
}
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 .