5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-25 21:15:43 +00:00

Restore uptime, bytes_sent and bytes_recvd to getPeers (#888)

* Restore `uptime`, `bytes_sent` and `bytes_recvd` to the admin API for peers

* Wrap conn in Yggdrasil instead, so not necessary to do so in Ironwood

* Shuffle struct for alignment
This commit is contained in:
Neil Alexander 2022-02-01 13:37:45 +00:00 committed by GitHub
parent 9f5cc0eecb
commit 2d2ad4692b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 7 deletions

View File

@ -19,6 +19,9 @@ type PeerEntry struct {
Port uint64 `json:"port"`
Coords []uint64 `json:"coords"`
Remote string `json:"remote"`
RXBytes uint64 `json:"bytes_recvd"`
TXBytes uint64 `json:"bytes_sent"`
Uptime float64 `json:"uptime"`
}
func (a *AdminSocket) getPeersHandler(req *GetPeersRequest, res *GetPeersResponse) error {
@ -31,6 +34,9 @@ func (a *AdminSocket) getPeersHandler(req *GetPeersRequest, res *GetPeersRespons
Port: p.Port,
Coords: p.Coords,
Remote: p.Remote,
RXBytes: p.RXBytes,
TXBytes: p.TXBytes,
Uptime: p.Uptime.Seconds(),
}
}
return nil

View File

@ -2,12 +2,16 @@ package core
import (
"crypto/ed25519"
"sync/atomic"
"time"
//"encoding/hex"
"encoding/json"
//"errors"
//"fmt"
"net"
"net/url"
//"sort"
//"time"
@ -24,11 +28,14 @@ type Self struct {
}
type Peer struct {
Key ed25519.PublicKey
Root ed25519.PublicKey
Coords []uint64
Port uint64
Remote string
Key ed25519.PublicKey
Root ed25519.PublicKey
Coords []uint64
Port uint64
Remote string
RXBytes uint64
TXBytes uint64
Uptime time.Duration
}
type DHTEntry struct {
@ -74,6 +81,11 @@ func (c *Core) GetPeers() []Peer {
if name := names[p.Conn]; name != "" {
info.Remote = name
}
if linkconn, ok := p.Conn.(*linkConn); ok {
info.RXBytes = atomic.LoadUint64(&linkconn.rx)
info.TXBytes = atomic.LoadUint64(&linkconn.tx)
info.Uptime = time.Since(linkconn.up)
}
peers = append(peers, info)
}
return peers

View File

@ -14,6 +14,8 @@ import (
//"sync/atomic"
"time"
"sync/atomic"
"github.com/yggdrasil-network/yggdrasil-go/src/address"
"github.com/yggdrasil-network/yggdrasil-go/src/util"
"golang.org/x/net/proxy"
@ -40,7 +42,7 @@ type linkInfo struct {
type link struct {
lname string
links *links
conn net.Conn
conn *linkConn
options linkOptions
info linkInfo
incoming bool
@ -124,7 +126,10 @@ func (l *links) call(u *url.URL, sintf string) error {
func (l *links) create(conn net.Conn, name, linkType, local, remote string, incoming, force bool, options linkOptions) (*link, error) {
// Technically anything unique would work for names, but let's pick something human readable, just for debugging
intf := link{
conn: conn,
conn: &linkConn{
Conn: conn,
up: time.Now(),
},
lname: name,
links: l,
options: options,
@ -272,3 +277,24 @@ func (intf *link) close() {
func (intf *link) name() string {
return intf.lname
}
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
net.Conn
}
func (c *linkConn) Read(p []byte) (n int, err error) {
n, err = c.Conn.Read(p)
atomic.AddUint64(&c.rx, uint64(n))
return
}
func (c *linkConn) Write(p []byte) (n int, err error) {
n, err = c.Conn.Write(p)
atomic.AddUint64(&c.tx, uint64(n))
return
}