From f16c9033e39e080f7bb48531ad4e0359f8df428d Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 14:16:33 +0000 Subject: [PATCH] refactor(proxy): use clearer runtime names Co-Authored-By: Virgil --- config_runtime.go | 6 ++--- pool/client.go | 34 +++++++++++++------------- pool/strategy.go | 18 +++++++------- proxy.go | 14 +++++------ proxy_runtime_test.go | 2 +- runtime_support.go | 28 ++++++++++----------- splitter/nicehash/mapper.go | 4 +-- splitter/simple/splitter.go | 42 ++++++++++++++++---------------- splitter/simple/splitter_test.go | 12 ++++----- 9 files changed, 80 insertions(+), 80 deletions(-) diff --git a/config_runtime.go b/config_runtime.go index b6fe6ee..1295e2d 100644 --- a/config_runtime.go +++ b/config_runtime.go @@ -76,7 +76,7 @@ func (w *ConfigWatcher) Start() { ticker := time.NewTicker(time.Second) defer ticker.Stop() if info, errorValue := os.Stat(w.path); errorValue == nil { - w.lastMod = info.ModTime() + w.lastModified = info.ModTime() } for { @@ -87,10 +87,10 @@ func (w *ConfigWatcher) Start() { continue } - if !info.ModTime().After(w.lastMod) { + if !info.ModTime().After(w.lastModified) { continue } - w.lastMod = info.ModTime() + w.lastModified = info.ModTime() config, errorValue := LoadConfig(w.path) if errorValue == nil && w.onChange != nil { diff --git a/pool/client.go b/pool/client.go index a0b61a7..53a2e6f 100644 --- a/pool/client.go +++ b/pool/client.go @@ -27,12 +27,12 @@ import ( // client := pool.NewStratumClient(poolCfg, listener) // client.Connect() type StratumClient struct { - cfg proxy.PoolConfig + config proxy.PoolConfig listener StratumListener conn net.Conn tlsConn *tls.Conn // nil if plain TCP sessionID string // pool-assigned session id from login reply - seq int64 // atomic JSON-RPC request id counter + sequence int64 // atomic JSON-RPC request id counter active bool // true once first job received disconnectOnce sync.Once sendMu sync.Mutex @@ -73,7 +73,7 @@ type jsonRPCErrorBody struct { // client := pool.NewStratumClient(poolCfg, listener) func NewStratumClient(cfg proxy.PoolConfig, listener StratumListener) *StratumClient { return &StratumClient{ - cfg: cfg, + config: cfg, listener: listener, } } @@ -85,18 +85,18 @@ func (c *StratumClient) Connect() error { var connection net.Conn var errorValue error dialer := net.Dialer{} - if c.cfg.Keepalive { + if c.config.Keepalive { dialer.KeepAlive = 30 * time.Second } - if c.cfg.TLS { - connection, errorValue = dialer.Dial("tcp", c.cfg.URL) + if c.config.TLS { + connection, errorValue = dialer.Dial("tcp", c.config.URL) if errorValue != nil { return errorValue } - serverName := c.cfg.URL - if host, _, splitError := net.SplitHostPort(c.cfg.URL); splitError == nil && host != "" { + serverName := c.config.URL + if host, _, splitError := net.SplitHostPort(c.config.URL); splitError == nil && host != "" { serverName = host } @@ -106,7 +106,7 @@ func (c *StratumClient) Connect() error { _ = connection.Close() return errorValue } - if c.cfg.TLSFingerprint != "" { + if c.config.TLSFingerprint != "" { state := tlsConnection.ConnectionState() if len(state.PeerCertificates) == 0 { _ = connection.Close() @@ -114,7 +114,7 @@ func (c *StratumClient) Connect() error { } fingerprint := sha256.Sum256(state.PeerCertificates[0].Raw) - if hex.EncodeToString(fingerprint[:]) != strings.ToLower(c.cfg.TLSFingerprint) { + if hex.EncodeToString(fingerprint[:]) != strings.ToLower(c.config.TLSFingerprint) { _ = connection.Close() return errors.New("pool fingerprint mismatch") } @@ -122,7 +122,7 @@ func (c *StratumClient) Connect() error { connection = tlsConnection c.tlsConn = tlsConnection } else { - connection, errorValue = dialer.Dial("tcp", c.cfg.URL) + connection, errorValue = dialer.Dial("tcp", c.config.URL) if errorValue != nil { return errorValue } @@ -139,12 +139,12 @@ func (c *StratumClient) Connect() error { // client.Login() func (c *StratumClient) Login() { params := map[string]interface{}{ - "login": c.cfg.User, - "pass": c.cfg.Pass, - "rigid": c.cfg.RigID, + "login": c.config.User, + "pass": c.config.Pass, + "rigid": c.config.RigID, } - if c.cfg.Algo != "" { - params["algo"] = []string{c.cfg.Algo} + if c.config.Algo != "" { + params["algo"] = []string{c.config.Algo} } _ = c.writeJSON(jsonRPCRequest{ @@ -158,7 +158,7 @@ func (c *StratumClient) Login() { // // seq := client.Submit(jobID, "deadbeef", "HASH64HEX", "cn/r") func (c *StratumClient) Submit(jobID string, nonce string, result string, algo string) int64 { - sequence := atomic.AddInt64(&c.seq, 1) + sequence := atomic.AddInt64(&c.sequence, 1) params := map[string]string{ "id": c.sessionID, "job_id": jobID, diff --git a/pool/strategy.go b/pool/strategy.go index 9fa6724..0224bf2 100644 --- a/pool/strategy.go +++ b/pool/strategy.go @@ -18,7 +18,7 @@ type FailoverStrategy struct { current int client *StratumClient listener StratumListener - cfg *proxy.Config + config *proxy.Config closed bool running bool mu sync.Mutex @@ -58,7 +58,7 @@ func NewFailoverStrategy(pools []proxy.PoolConfig, listener StratumListener, cfg return &FailoverStrategy{ pools: append([]proxy.PoolConfig(nil), pools...), listener: listener, - cfg: cfg, + config: cfg, } } @@ -87,8 +87,8 @@ func (s *FailoverStrategy) connectFrom(start int) { }() pools := s.pools - if s.cfg != nil && len(s.cfg.Pools) > 0 { - pools = s.cfg.Pools + if s.config != nil && len(s.config.Pools) > 0 { + pools = s.config.Pools } if len(pools) == 0 { return @@ -96,12 +96,12 @@ func (s *FailoverStrategy) connectFrom(start int) { retries := 1 pause := time.Duration(0) - if s.cfg != nil { - if s.cfg.Retries > 0 { - retries = s.cfg.Retries + if s.config != nil { + if s.config.Retries > 0 { + retries = s.config.Retries } - if s.cfg.RetryPause > 0 { - pause = time.Duration(s.cfg.RetryPause) * time.Second + if s.config.RetryPause > 0 { + pause = time.Duration(s.config.RetryPause) * time.Second } } diff --git a/proxy.go b/proxy.go index 97250ec..9d6310d 100644 --- a/proxy.go +++ b/proxy.go @@ -96,10 +96,10 @@ type CloseEvent struct { // }) // w.Start() type ConfigWatcher struct { - path string - onChange func(*Config) - lastMod time.Time - done chan struct{} + path string + onChange func(*Config) + lastModified time.Time + done chan struct{} } // RateLimiter implements per-IP token bucket connection rate limiting. @@ -110,7 +110,7 @@ type ConfigWatcher struct { // rl := proxy.NewRateLimiter(cfg.RateLimit) // if !rl.Allow("1.2.3.4") { conn.Close(); return } type RateLimiter struct { - cfg RateLimit + config RateLimit buckets map[string]*tokenBucket banned map[string]time.Time mu sync.Mutex @@ -128,8 +128,8 @@ type tokenBucket struct { // cd := proxy.NewCustomDiff(cfg.CustomDiff) // bus.Subscribe(proxy.EventLogin, cd.OnLogin) type CustomDiff struct { - globalDiff uint64 - mu sync.RWMutex + defaultDifficulty uint64 + mu sync.RWMutex } var splitterFactories = map[string]func(*Config, *EventBus) Splitter{ diff --git a/proxy_runtime_test.go b/proxy_runtime_test.go index 7974a25..92253b5 100644 --- a/proxy_runtime_test.go +++ b/proxy_runtime_test.go @@ -48,7 +48,7 @@ func TestProxy_Reload_Good(t *testing.T) { if proxyValue.config.CustomDiff != 250 { t.Fatalf("expected custom diff to reload, got %d", proxyValue.config.CustomDiff) } - if proxyValue.customDiff == nil || proxyValue.customDiff.globalDiff != 250 { + if proxyValue.customDiff == nil || proxyValue.customDiff.defaultDifficulty != 250 { t.Fatalf("expected live custom diff to update, got %+v", proxyValue.customDiff) } diff --git a/runtime_support.go b/runtime_support.go index 9aab3f3..369cc50 100644 --- a/runtime_support.go +++ b/runtime_support.go @@ -11,7 +11,7 @@ import ( // rl := proxy.NewRateLimiter(cfg.RateLimit) func NewRateLimiter(config RateLimit) *RateLimiter { return &RateLimiter{ - cfg: config, + config: config, buckets: make(map[string]*tokenBucket), banned: make(map[string]time.Time), } @@ -26,7 +26,7 @@ func (rateLimiter *RateLimiter) SetConfig(config RateLimit) { } rateLimiter.mu.Lock() - rateLimiter.cfg = config + rateLimiter.config = config rateLimiter.mu.Unlock() } @@ -44,7 +44,7 @@ func (rateLimiter *RateLimiter) Allow(ip string) bool { rateLimiter.mu.Lock() defer rateLimiter.mu.Unlock() - if rateLimiter.cfg.MaxConnectionsPerMinute <= 0 { + if rateLimiter.config.MaxConnectionsPerMinute <= 0 { return true } @@ -58,7 +58,7 @@ func (rateLimiter *RateLimiter) Allow(ip string) bool { bucket, exists := rateLimiter.buckets[host] if !exists { bucket = &tokenBucket{ - tokens: rateLimiter.cfg.MaxConnectionsPerMinute, + tokens: rateLimiter.config.MaxConnectionsPerMinute, lastRefill: now, } rateLimiter.buckets[host] = bucket @@ -66,8 +66,8 @@ func (rateLimiter *RateLimiter) Allow(ip string) bool { rateLimiter.refillBucket(bucket, now) if bucket.tokens <= 0 { - if rateLimiter.cfg.BanDurationSeconds > 0 { - rateLimiter.banned[host] = now.Add(time.Duration(rateLimiter.cfg.BanDurationSeconds) * time.Second) + if rateLimiter.config.BanDurationSeconds > 0 { + rateLimiter.banned[host] = now.Add(time.Duration(rateLimiter.config.BanDurationSeconds) * time.Second) } return false } @@ -88,7 +88,7 @@ func (rateLimiter *RateLimiter) Tick() { rateLimiter.mu.Lock() defer rateLimiter.mu.Unlock() - if rateLimiter.cfg.MaxConnectionsPerMinute <= 0 { + if rateLimiter.config.MaxConnectionsPerMinute <= 0 { return } @@ -104,11 +104,11 @@ func (rateLimiter *RateLimiter) Tick() { } func (rateLimiter *RateLimiter) refillBucket(bucket *tokenBucket, now time.Time) { - if bucket == nil || rateLimiter.cfg.MaxConnectionsPerMinute <= 0 { + if bucket == nil || rateLimiter.config.MaxConnectionsPerMinute <= 0 { return } - refillEvery := time.Minute / time.Duration(rateLimiter.cfg.MaxConnectionsPerMinute) + refillEvery := time.Minute / time.Duration(rateLimiter.config.MaxConnectionsPerMinute) if refillEvery <= 0 { refillEvery = time.Second } @@ -120,8 +120,8 @@ func (rateLimiter *RateLimiter) refillBucket(bucket *tokenBucket, now time.Time) tokensToAdd := int(elapsed / refillEvery) bucket.tokens += tokensToAdd - if bucket.tokens > rateLimiter.cfg.MaxConnectionsPerMinute { - bucket.tokens = rateLimiter.cfg.MaxConnectionsPerMinute + if bucket.tokens > rateLimiter.config.MaxConnectionsPerMinute { + bucket.tokens = rateLimiter.config.MaxConnectionsPerMinute } bucket.lastRefill = bucket.lastRefill.Add(time.Duration(tokensToAdd) * refillEvery) } @@ -130,7 +130,7 @@ func (rateLimiter *RateLimiter) refillBucket(bucket *tokenBucket, now time.Time) // // cd := proxy.NewCustomDiff(50000) func NewCustomDiff(globalDiff uint64) *CustomDiff { - return &CustomDiff{globalDiff: globalDiff} + return &CustomDiff{defaultDifficulty: globalDiff} } // SetGlobalDiff updates the default custom difficulty override. @@ -142,7 +142,7 @@ func (customDiff *CustomDiff) SetGlobalDiff(globalDiff uint64) { } customDiff.mu.Lock() - customDiff.globalDiff = globalDiff + customDiff.defaultDifficulty = globalDiff customDiff.mu.Unlock() } @@ -172,7 +172,7 @@ func (customDiff *CustomDiff) OnLogin(event Event) { } customDiff.mu.RLock() - globalDiff := customDiff.globalDiff + globalDiff := customDiff.defaultDifficulty customDiff.mu.RUnlock() if globalDiff > 0 { event.Miner.SetCustomDiff(globalDiff) diff --git a/splitter/nicehash/mapper.go b/splitter/nicehash/mapper.go index 8ebb8da..dafd453 100644 --- a/splitter/nicehash/mapper.go +++ b/splitter/nicehash/mapper.go @@ -18,7 +18,7 @@ type NonceMapper struct { storage *NonceStorage strategy pool.Strategy // manages pool client lifecycle and failover pending map[int64]SubmitContext // sequence → {requestID, minerID, jobID} - cfg *proxy.Config + config *proxy.Config events *proxy.EventBus active bool // true once pool has sent at least one job suspended int // > 0 when pool connection is in error/reconnecting @@ -46,7 +46,7 @@ func NewNonceMapper(id int64, cfg *proxy.Config, strategy pool.Strategy) *NonceM id: id, storage: NewNonceStorage(), strategy: strategy, - cfg: cfg, + config: cfg, pending: make(map[int64]SubmitContext), } } diff --git a/splitter/simple/splitter.go b/splitter/simple/splitter.go index 98246c3..baaff38 100644 --- a/splitter/simple/splitter.go +++ b/splitter/simple/splitter.go @@ -19,13 +19,13 @@ import ( // // s := simple.NewSimpleSplitter(cfg, eventBus, strategyFactory) type SimpleSplitter struct { - active map[int64]*SimpleMapper // minerID → mapper - idle map[int64]*SimpleMapper // mapperID → mapper (reuse pool, keyed by mapper seq) - cfg *proxy.Config - events *proxy.EventBus - factory pool.StrategyFactory - mu sync.Mutex - seq int64 // monotonic mapper sequence counter + active map[int64]*SimpleMapper // minerID → mapper + idle map[int64]*SimpleMapper // mapperID → mapper (reuse pool, keyed by mapper seq) + config *proxy.Config + events *proxy.EventBus + strategyFactory pool.StrategyFactory + mu sync.Mutex + sequence int64 // monotonic mapper sequence counter } // NewSimpleSplitter creates the passthrough splitter. @@ -33,11 +33,11 @@ type SimpleSplitter struct { // s := simple.NewSimpleSplitter(cfg, bus, factory) func NewSimpleSplitter(cfg *proxy.Config, events *proxy.EventBus, factory pool.StrategyFactory) *SimpleSplitter { return &SimpleSplitter{ - active: make(map[int64]*SimpleMapper), - idle: make(map[int64]*SimpleMapper), - cfg: cfg, - events: events, - factory: factory, + active: make(map[int64]*SimpleMapper), + idle: make(map[int64]*SimpleMapper), + config: cfg, + events: events, + strategyFactory: factory, } } @@ -52,8 +52,8 @@ func (s *SimpleSplitter) OnLogin(event *proxy.LoginEvent) { defer s.mu.Unlock() timeout := time.Duration(0) - if s.cfg != nil && s.cfg.ReuseTimeout > 0 { - timeout = time.Duration(s.cfg.ReuseTimeout) * time.Second + if s.config != nil && s.config.ReuseTimeout > 0 { + timeout = time.Duration(s.config.ReuseTimeout) * time.Second } var mapper *SimpleMapper @@ -72,12 +72,12 @@ func (s *SimpleSplitter) OnLogin(event *proxy.LoginEvent) { } if mapper == nil { - s.seq++ + s.sequence++ var strategy pool.Strategy - mapper = NewSimpleMapper(s.seq, nil) + mapper = NewSimpleMapper(s.sequence, nil) mapper.events = s.events - if s.factory != nil { - strategy = s.factory(mapper) + if s.strategyFactory != nil { + strategy = s.strategyFactory(mapper) } mapper.strategy = strategy if mapper.strategy != nil { @@ -152,7 +152,7 @@ func (s *SimpleSplitter) OnClose(event *proxy.CloseEvent) { mapper.clearPending() mapper.miner = nil mapper.idleAt = time.Now().UTC() - if s.cfg != nil && s.cfg.ReuseTimeout > 0 { + if s.config != nil && s.config.ReuseTimeout > 0 { s.idle[mapper.id] = mapper return } @@ -174,8 +174,8 @@ func (s *SimpleSplitter) GC() { defer s.mu.Unlock() timeout := time.Duration(0) - if s.cfg != nil && s.cfg.ReuseTimeout > 0 { - timeout = time.Duration(s.cfg.ReuseTimeout) * time.Second + if s.config != nil && s.config.ReuseTimeout > 0 { + timeout = time.Duration(s.config.ReuseTimeout) * time.Second } now := time.Now().UTC() diff --git a/splitter/simple/splitter_test.go b/splitter/simple/splitter_test.go index 9ebf6a1..06174da 100644 --- a/splitter/simple/splitter_test.go +++ b/splitter/simple/splitter_test.go @@ -39,8 +39,8 @@ func TestSimpleSplitter_OnLogin_Ugly(t *testing.T) { idleAt: time.Now().UTC(), }, }, - cfg: &proxy.Config{ReuseTimeout: 60}, - factory: func(listener pool.StratumListener) pool.Strategy { + config: &proxy.Config{ReuseTimeout: 60}, + strategyFactory: func(listener pool.StratumListener) pool.Strategy { return liveStrategy }, } @@ -73,8 +73,8 @@ func TestSimpleSplitter_OnLogin_Bad(t *testing.T) { idleAt: time.Now().UTC().Add(-2 * time.Minute), }, }, - cfg: &proxy.Config{ReuseTimeout: 60}, - factory: func(listener pool.StratumListener) pool.Strategy { + config: &proxy.Config{ReuseTimeout: 60}, + strategyFactory: func(listener pool.StratumListener) pool.Strategy { return activeStrategy }, } @@ -95,8 +95,8 @@ func TestSimpleSplitter_OnClose_Ugly(t *testing.T) { splitter := &SimpleSplitter{ active: make(map[int64]*SimpleMapper), idle: make(map[int64]*SimpleMapper), - cfg: &proxy.Config{ReuseTimeout: 60}, - factory: func(listener pool.StratumListener) pool.Strategy { + config: &proxy.Config{ReuseTimeout: 60}, + strategyFactory: func(listener pool.StratumListener) pool.Strategy { return activeStrategy }, }