5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-25 15:01:34 +00:00

add uptime and bytes sent/recvd to peer struct and getPeers

This commit is contained in:
Arceliar 2018-05-18 20:41:02 -05:00
parent fdb826578f
commit 5c0636eb3d
2 changed files with 21 additions and 8 deletions

View File

@ -10,6 +10,7 @@ import "net/url"
import "sort"
import "strings"
import "strconv"
import "sync/atomic"
import "time"
// TODO? Make all of this JSON
@ -322,6 +323,9 @@ func (a *admin) getData_getPeers() []admin_nodeInfo {
info := admin_nodeInfo{
{"IP", net.IP(addr[:]).String()},
{"port", fmt.Sprint(port)},
{"uptime", fmt.Sprint(time.Since(p.firstSeen))},
{"bytesSent", fmt.Sprint(atomic.LoadUint64(&p.bytesSent))},
{"bytesRecvd", fmt.Sprint(atomic.LoadUint64(&p.bytesRecvd))},
}
peerInfos = append(peerInfos, info)
}

View File

@ -87,7 +87,10 @@ type peer struct {
// Rolling approximation of bandwidth, in bps, used by switch, updated by packet sends
// use get/update methods only! (atomic accessors as float64)
bandwidth uint64
bytesSent uint64 // To track bandwidth usage for getPeers
bytesRecvd uint64 // To track bandwidth usage for getPeers
// BUG: sync/atomic, 32 bit platforms need the above to be the first element
firstSeen time.Time // To track uptime for getPeers
box boxPubKey
sig sigPubKey
shared boxSharedKey
@ -133,10 +136,12 @@ func (p *peer) updateBandwidth(bytes int, duration time.Duration) {
func (ps *peers) newPeer(box *boxPubKey,
sig *sigPubKey) *peer {
now := time.Now()
p := peer{box: *box,
sig: *sig,
shared: *getSharedKey(&ps.core.boxPriv, box),
lastAnc: time.Now(),
lastAnc: now,
firstSeen: now,
core: ps.core}
ps.mutex.Lock()
defer ps.mutex.Unlock()
@ -221,6 +226,8 @@ func (p *peer) linkLoop(in <-chan []byte) {
}
func (p *peer) handlePacket(packet []byte, linkIn chan<- []byte) {
// TODO See comment in sendPacket about atomics technically being done wrong
atomic.AddUint64(&p.bytesRecvd, uint64(len(packet)))
pType, pTypeLen := wire_decode_uint64(packet)
if pTypeLen == 0 {
return
@ -277,6 +284,8 @@ func (p *peer) sendPacket(packet []byte) {
// Is there ever a case where something more complicated is needed?
// What if p.out blocks?
p.out(packet)
// TODO this should really happen at the interface, to account for LIFO packet drops and additional per-packet/per-message overhead, but this should be pretty close... better to move it to the tcp/udp stuff *after* rewriting both to give a common interface
atomic.AddUint64(&p.bytesSent, uint64(len(packet)))
}
func (p *peer) sendLinkPacket(packet []byte) {