refactor: вынести stats, netadapt, bond в internal/
- internal/stats: throughputStats/countingConn/форматтеры, общие для клиента и сервера - internal/netadapt: DirectNet, ConnectedUDPConn, SplitFirstWriteConn - internal/bond: wire-кодек (Frame/Hello, Read/Write, CloseWrite) Убирает дублирование между client/main.go и server/main.go. Wire-формат и поведение CLI не изменены; round-trip кодека покрыт тестами.
This commit is contained in:
1 parent
948108a33a
commit
ada0c746aa
8 files changed
+817
-580
No files matched your search
@@ -0,0 +1,130 @@
|
||||
// Package stats provides throughput counters and a counting net.Conn wrapper
|
||||
// shared between the client and server binaries.
|
||||
package stats
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Stats tracks tx/rx byte counters. When Enabled is false, Add* are no-ops and
|
||||
// LogEvery returns immediately, matching the original isDebug-gated behavior.
|
||||
type Stats struct {
|
||||
tx atomic.Uint64
|
||||
rx atomic.Uint64
|
||||
enabled bool
|
||||
}
|
||||
|
||||
// New returns a Stats with the given enabled flag.
|
||||
func New(enabled bool) *Stats {
|
||||
return &Stats{enabled: enabled}
|
||||
}
|
||||
|
||||
// AddTx records n bytes sent.
|
||||
func (s *Stats) AddTx(n int) {
|
||||
if !s.enabled || n <= 0 {
|
||||
return
|
||||
}
|
||||
s.tx.Add(uint64(n))
|
||||
}
|
||||
|
||||
// AddRx records n bytes received.
|
||||
func (s *Stats) AddRx(n int) {
|
||||
if !s.enabled || n <= 0 {
|
||||
return
|
||||
}
|
||||
s.rx.Add(uint64(n))
|
||||
}
|
||||
|
||||
// LogEvery emits a throughput summary every 5s via logf until ctx is canceled.
|
||||
// No-op if Stats is disabled.
|
||||
func (s *Stats) LogEvery(ctx context.Context, logf func(string, ...any), label, txName, rxName string) {
|
||||
if !s.enabled {
|
||||
return
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(5 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
var prevTx, prevRx uint64
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
tx := s.tx.Load()
|
||||
rx := s.rx.Load()
|
||||
deltaTx := tx - prevTx
|
||||
deltaRx := rx - prevRx
|
||||
prevTx = tx
|
||||
prevRx = rx
|
||||
|
||||
if deltaTx == 0 && deltaRx == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
logf(
|
||||
"%s throughput: %s=%s %s=%s total_%s=%s total_%s=%s",
|
||||
label,
|
||||
txName,
|
||||
FormatBitsPerSecond(deltaTx, 5*time.Second),
|
||||
rxName,
|
||||
FormatBitsPerSecond(deltaRx, 5*time.Second),
|
||||
txName,
|
||||
FormatByteCount(tx),
|
||||
rxName,
|
||||
FormatByteCount(rx),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// FormatBitsPerSecond renders a bandwidth value from a byte count and interval.
|
||||
func FormatBitsPerSecond(bytes uint64, interval time.Duration) string {
|
||||
if interval <= 0 {
|
||||
interval = time.Second
|
||||
}
|
||||
|
||||
bps := float64(bytes*8) / interval.Seconds()
|
||||
if bps >= 1_000_000 {
|
||||
return fmt.Sprintf("%.2f Mbit/s", bps/1_000_000)
|
||||
}
|
||||
if bps >= 1_000 {
|
||||
return fmt.Sprintf("%.1f kbit/s", bps/1_000)
|
||||
}
|
||||
return fmt.Sprintf("%.0f bit/s", bps)
|
||||
}
|
||||
|
||||
// FormatByteCount renders a human-readable byte count.
|
||||
func FormatByteCount(bytes uint64) string {
|
||||
if bytes >= 1024*1024 {
|
||||
return fmt.Sprintf("%.2f MiB", float64(bytes)/(1024*1024))
|
||||
}
|
||||
if bytes >= 1024 {
|
||||
return fmt.Sprintf("%.1f KiB", float64(bytes)/1024)
|
||||
}
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
|
||||
// CountingConn wraps a net.Conn and accumulates rx/tx byte counters in Stats.
|
||||
type CountingConn struct {
|
||||
net.Conn
|
||||
Stats *Stats
|
||||
}
|
||||
|
||||
// Read reads from the underlying conn and updates rx counter.
|
||||
func (c *CountingConn) Read(p []byte) (int, error) {
|
||||
n, err := c.Conn.Read(p)
|
||||
c.Stats.AddRx(n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
// Write writes to the underlying conn and updates tx counter.
|
||||
func (c *CountingConn) Write(p []byte) (int, error) {
|
||||
n, err := c.Conn.Write(p)
|
||||
c.Stats.AddTx(n)
|
||||
return n, err
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFormatBitsPerSecond(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cases := []struct {
|
||||
bytes uint64
|
||||
interval time.Duration
|
||||
want string
|
||||
}{
|
||||
{0, time.Second, "0 bit/s"},
|
||||
{125, time.Second, "1.0 kbit/s"}, // 1000 bit/s
|
||||
{125_000, time.Second, "1.00 Mbit/s"}, // 1_000_000 bit/s
|
||||
{50, time.Second, "400 bit/s"},
|
||||
{1_000_000, 5 * time.Second, "1.60 Mbit/s"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := FormatBitsPerSecond(c.bytes, c.interval)
|
||||
if got != c.want {
|
||||
t.Errorf("FormatBitsPerSecond(%d, %s) = %q, want %q", c.bytes, c.interval, got, c.want)
|
||||
}
|
||||
}
|
||||
|
||||
// interval <= 0 should not panic and should default to 1s.
|
||||
if got := FormatBitsPerSecond(125, 0); !strings.Contains(got, "kbit/s") {
|
||||
t.Errorf("zero interval fallback: got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatByteCount(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cases := []struct {
|
||||
bytes uint64
|
||||
want string
|
||||
}{
|
||||
{0, "0 B"},
|
||||
{1023, "1023 B"},
|
||||
{1024, "1.0 KiB"},
|
||||
{1024 * 1024, "1.00 MiB"},
|
||||
{2 * 1024 * 1024, "2.00 MiB"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := FormatByteCount(c.bytes)
|
||||
if got != c.want {
|
||||
t.Errorf("FormatByteCount(%d) = %q, want %q", c.bytes, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsDisabledNoOp(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s := New(false)
|
||||
s.AddTx(1234)
|
||||
s.AddRx(5678)
|
||||
if got := s.tx.Load(); got != 0 {
|
||||
t.Errorf("disabled AddTx leaked: tx=%d", got)
|
||||
}
|
||||
if got := s.rx.Load(); got != 0 {
|
||||
t.Errorf("disabled AddRx leaked: rx=%d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsEnabledAccumulates(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s := New(true)
|
||||
s.AddTx(100)
|
||||
s.AddTx(50)
|
||||
s.AddRx(7)
|
||||
s.AddTx(-5) // negative: ignored
|
||||
s.AddRx(0) // zero: ignored
|
||||
if got := s.tx.Load(); got != 150 {
|
||||
t.Errorf("tx=%d, want 150", got)
|
||||
}
|
||||
if got := s.rx.Load(); got != 7 {
|
||||
t.Errorf("rx=%d, want 7", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user