aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josh@tailscale.com>2021-01-26 10:54:59 -0800
committerJosh Bleecher Snyder <josh@tailscale.com>2021-01-26 13:29:34 -0800
commitcda9f6e73a3bf3af09481bf31d57cf35c8b342de (patch)
treef0d126d5a30ecc2685e19c8e4dd1ea555727a47f
parent23b2790aa06270485f3d951132609afaeb888c28 (diff)
downloadwireguard-go-js/peer-stats.tar.gz
wireguard-go-js/peer-stats.zip
device: add PeerStatsjs/peer-stats
Per-peer statistics are the sort of thing clients refresh very frequently, for UIs. They are available via UAPI, but the serialization and deserialization is quite heavy for frequent use. This commit adds a very cheap, direct means of retrieving peer stats. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
-rw-r--r--device/stats.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/device/stats.go b/device/stats.go
new file mode 100644
index 0000000..6ccc501
--- /dev/null
+++ b/device/stats.go
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
+ */
+
+package device
+
+import (
+ "sync/atomic"
+ "time"
+)
+
+// PeerStats are connection statistics for a given Peer.
+type PeerStats struct {
+ RxBytes uint64
+ TxBytes uint64
+ LastHandshakeInitiated time.Time
+}
+
+// PeerStats returns statistics for the peer with public key pk,
+// and reports whether the peer lookup succeeded.
+func (device *Device) PeerStats(pk NoisePublicKey) (stats PeerStats, ok bool) {
+ device.peers.RLock()
+ peer := device.peers.keyMap[pk]
+ device.peers.RUnlock()
+
+ if peer == nil {
+ return PeerStats{}, false
+ }
+
+ peer.RLock()
+ defer peer.RUnlock()
+ stats = PeerStats{
+ RxBytes: atomic.LoadUint64(&peer.stats.rxBytes),
+ TxBytes: atomic.LoadUint64(&peer.stats.txBytes),
+ LastHandshakeInitiated: time.Unix(0, atomic.LoadInt64(&peer.stats.lastHandshakeNano)),
+ }
+ return stats, true
+}