mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-12-05 07:32:23 +00:00
Expose download/upload rate per peer (#1206)
This commit is contained in:
@@ -33,6 +33,8 @@ type PeerInfo struct {
|
||||
Cost uint64
|
||||
RXBytes uint64
|
||||
TXBytes uint64
|
||||
RXRate uint64
|
||||
TXRate uint64
|
||||
Uptime time.Duration
|
||||
Latency time.Duration
|
||||
}
|
||||
@@ -87,6 +89,8 @@ func (c *Core) GetPeers() []PeerInfo {
|
||||
peerinfo.Inbound = state.linkType == linkTypeIncoming
|
||||
peerinfo.RXBytes = atomic.LoadUint64(&c.rx)
|
||||
peerinfo.TXBytes = atomic.LoadUint64(&c.tx)
|
||||
peerinfo.RXRate = atomic.LoadUint64(&c.rxrate)
|
||||
peerinfo.TXRate = atomic.LoadUint64(&c.txrate)
|
||||
peerinfo.Uptime = time.Since(c.up)
|
||||
}
|
||||
if p, ok := conns[conn]; ok {
|
||||
|
||||
@@ -99,9 +99,36 @@ func (l *links) init(c *Core) error {
|
||||
l._links = make(map[linkInfo]*link)
|
||||
l._listeners = make(map[*Listener]context.CancelFunc)
|
||||
|
||||
l.Act(nil, l._updateAverages)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *links) _updateAverages() {
|
||||
select {
|
||||
case <-l.core.ctx.Done():
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
for _, l := range l._links {
|
||||
if l._conn == nil {
|
||||
continue
|
||||
}
|
||||
rx := atomic.LoadUint64(&l._conn.rx)
|
||||
tx := atomic.LoadUint64(&l._conn.tx)
|
||||
lastrx := atomic.LoadUint64(&l._conn.lastrx)
|
||||
lasttx := atomic.LoadUint64(&l._conn.lasttx)
|
||||
atomic.StoreUint64(&l._conn.rxrate, rx-lastrx)
|
||||
atomic.StoreUint64(&l._conn.txrate, tx-lasttx)
|
||||
atomic.StoreUint64(&l._conn.lastrx, rx)
|
||||
atomic.StoreUint64(&l._conn.lasttx, tx)
|
||||
}
|
||||
|
||||
time.AfterFunc(time.Second, func() {
|
||||
l.Act(nil, l._updateAverages)
|
||||
})
|
||||
}
|
||||
|
||||
func (l *links) shutdown() {
|
||||
phony.Block(l, func() {
|
||||
for listener := range l._listeners {
|
||||
@@ -699,9 +726,13 @@ func urlForLinkInfo(u url.URL) url.URL {
|
||||
type linkConn struct {
|
||||
// tx and rx are at the beginning of the struct to ensure 64-bit alignment
|
||||
// on 32-bit platforms, see https://pkg.go.dev/sync/atomic#pkg-note-BUG
|
||||
rx uint64
|
||||
tx uint64
|
||||
up time.Time
|
||||
rx uint64
|
||||
tx uint64
|
||||
rxrate uint64
|
||||
txrate uint64
|
||||
lastrx uint64
|
||||
lasttx uint64
|
||||
up time.Time
|
||||
net.Conn
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user