5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-22 14:10:28 +00:00

Merge pull request #83 from Arceliar/peerInfo

Add uptime and bytes sent/recvd to  the `peer` struct and getPeers results
This commit is contained in:
Neil Alexander 2018-05-19 10:09:42 +01:00 committed by GitHub
commit 89b7be1409
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) {