From fe2872149e3d8b22d55b8f9e048d1099b1d58ade Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 12:07:32 +0000 Subject: [PATCH] fix(proxy): count accepted tcp connections Co-Authored-By: Virgil --- proxy_runtime.go | 5 ++++- proxy_runtime_test.go | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/proxy_runtime.go b/proxy_runtime.go index f1fb45e..909f4ed 100644 --- a/proxy_runtime.go +++ b/proxy_runtime.go @@ -52,7 +52,6 @@ func New(config *Config) (*Proxy, error) { proxyInstance.miners[event.Miner.ID()] = event.Miner proxyInstance.minerMu.Unlock() } - statsValue.connections.Add(1) current := proxyInstance.currentMiners.Add(1) for { maximum := statsValue.maxMiners.Load() @@ -321,6 +320,10 @@ func (p *Proxy) Upstreams() UpstreamStats { } func (p *Proxy) acceptConn(conn net.Conn, localPort uint16) { + if p != nil && p.stats != nil { + p.stats.connections.Add(1) + } + var tlsConfig *tls.Config if _, ok := conn.(*tls.Conn); ok { tlsConfig = &tls.Config{} diff --git a/proxy_runtime_test.go b/proxy_runtime_test.go index f757a1a..44a3380 100644 --- a/proxy_runtime_test.go +++ b/proxy_runtime_test.go @@ -1,6 +1,9 @@ package proxy -import "testing" +import ( + "net" + "testing" +) func TestProxy_Reload_Good(t *testing.T) { cfg := &Config{ @@ -69,3 +72,25 @@ func TestProxy_CurrentMiners_Good(t *testing.T) { t.Fatalf("expected first proxy miner count to return to 0, got %d", got) } } + +func TestProxy_AcceptConn_Good(t *testing.T) { + cfg := &Config{ + Mode: "nicehash", + Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, + Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}}, + Workers: WorkersDisabled, + } + proxyValue, errorValue := New(cfg) + if errorValue != nil { + t.Fatal(errorValue) + } + + serverConn, clientConn := net.Pipe() + proxyValue.acceptConn(serverConn, 3333) + if got := proxyValue.stats.connections.Load(); got != 1 { + t.Fatalf("expected connection counter to increment on accept, got %d", got) + } + + _ = clientConn.Close() + _ = serverConn.Close() +}