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 "sort"
import "strings" import "strings"
import "strconv" import "strconv"
import "sync/atomic"
import "time" import "time"
// TODO? Make all of this JSON // TODO? Make all of this JSON
@ -322,6 +323,9 @@ func (a *admin) getData_getPeers() []admin_nodeInfo {
info := admin_nodeInfo{ info := admin_nodeInfo{
{"IP", net.IP(addr[:]).String()}, {"IP", net.IP(addr[:]).String()},
{"port", fmt.Sprint(port)}, {"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) peerInfos = append(peerInfos, info)
} }

View File

@ -86,11 +86,14 @@ func (ps *peers) putPorts(ports map[switchPort]*peer) {
type peer struct { type peer struct {
// Rolling approximation of bandwidth, in bps, used by switch, updated by packet sends // Rolling approximation of bandwidth, in bps, used by switch, updated by packet sends
// use get/update methods only! (atomic accessors as float64) // use get/update methods only! (atomic accessors as float64)
bandwidth uint64 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 // BUG: sync/atomic, 32 bit platforms need the above to be the first element
box boxPubKey firstSeen time.Time // To track uptime for getPeers
sig sigPubKey box boxPubKey
shared boxSharedKey sig sigPubKey
shared boxSharedKey
//in <-chan []byte //in <-chan []byte
//out chan<- []byte //out chan<- []byte
//in func([]byte) //in func([]byte)
@ -133,11 +136,13 @@ func (p *peer) updateBandwidth(bytes int, duration time.Duration) {
func (ps *peers) newPeer(box *boxPubKey, func (ps *peers) newPeer(box *boxPubKey,
sig *sigPubKey) *peer { sig *sigPubKey) *peer {
now := time.Now()
p := peer{box: *box, p := peer{box: *box,
sig: *sig, sig: *sig,
shared: *getSharedKey(&ps.core.boxPriv, box), shared: *getSharedKey(&ps.core.boxPriv, box),
lastAnc: time.Now(), lastAnc: now,
core: ps.core} firstSeen: now,
core: ps.core}
ps.mutex.Lock() ps.mutex.Lock()
defer ps.mutex.Unlock() defer ps.mutex.Unlock()
oldPorts := ps.getPorts() oldPorts := ps.getPorts()
@ -221,6 +226,8 @@ func (p *peer) linkLoop(in <-chan []byte) {
} }
func (p *peer) handlePacket(packet []byte, linkIn 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) pType, pTypeLen := wire_decode_uint64(packet)
if pTypeLen == 0 { if pTypeLen == 0 {
return return
@ -277,6 +284,8 @@ func (p *peer) sendPacket(packet []byte) {
// Is there ever a case where something more complicated is needed? // Is there ever a case where something more complicated is needed?
// What if p.out blocks? // What if p.out blocks?
p.out(packet) 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) { func (p *peer) sendLinkPacket(packet []byte) {