From ce7d3301fc3341255b36846b90f861628e90e7cb Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 20:53:49 +0000 Subject: [PATCH] refactor(proxy): clarify config path naming Co-Authored-By: Virgil --- api/router.go | 3 +++ config.go | 2 +- core_impl.go | 2 +- miner_login_test.go | 2 +- proxy.go | 12 +++--------- state_impl.go | 8 ++++---- 6 files changed, 13 insertions(+), 16 deletions(-) diff --git a/api/router.go b/api/router.go index 7a6f829..320d566 100644 --- a/api/router.go +++ b/api/router.go @@ -77,6 +77,9 @@ type ResultsResponse struct { } // RegisterRoutes wires the monitoring endpoints onto the supplied router. +// +// proxyapi.RegisterRoutes(mux, p) +// // GET /1/summary, /1/workers, and /1/miners are now live. func RegisterRoutes(r Router, p *proxy.Proxy) { if r == nil || p == nil { return diff --git a/config.go b/config.go index 057bead..1572415 100644 --- a/config.go +++ b/config.go @@ -22,7 +22,7 @@ type Config struct { RetryPause int `json:"retry-pause"` // seconds between retries Watch bool `json:"watch"` // hot-reload on file change RateLimit RateLimit `json:"rate-limit"` // per-IP connection rate limit - sourcePath string + configPath string } // BindAddr is one TCP listen endpoint. diff --git a/core_impl.go b/core_impl.go index 0472a8e..d42ada8 100644 --- a/core_impl.go +++ b/core_impl.go @@ -59,7 +59,7 @@ func LoadConfig(path string) (*Config, Result) { if err := json.Unmarshal(data, cfg); err != nil { return nil, errorResult(err) } - cfg.sourcePath = path + cfg.configPath = path return cfg, cfg.Validate() } diff --git a/miner_login_test.go b/miner_login_test.go index cb184d6..c51211d 100644 --- a/miner_login_test.go +++ b/miner_login_test.go @@ -95,7 +95,7 @@ func TestProxy_New_Watch_Good(t *testing.T) { Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, Pools: []PoolConfig{{URL: "pool.example:3333", Enabled: true}}, Watch: true, - sourcePath: "/tmp/proxy.json", + configPath: "/tmp/proxy.json", } proxyInstance, result := New(cfg) diff --git a/proxy.go b/proxy.go index f9ab0f1..c9a8e8d 100644 --- a/proxy.go +++ b/proxy.go @@ -91,11 +91,8 @@ type CloseEvent struct { } // ConfigWatcher polls a config file for mtime changes and calls onChange on modification. -// Uses 1-second polling; does not require fsnotify. // -// w := proxy.NewConfigWatcher("config.json", func(cfg *proxy.Config) { -// p.Reload(cfg) -// }) +// w := proxy.NewConfigWatcher("config.json", func(cfg *proxy.Config) { p.Reload(cfg) }) // w.Start() type ConfigWatcher struct { path string @@ -105,12 +102,9 @@ type ConfigWatcher struct { } // RateLimiter implements per-IP token bucket connection rate limiting. -// Each unique IP has a bucket initialised to MaxConnectionsPerMinute tokens. -// Each connection attempt consumes one token. Tokens refill at 1 per (60/max) seconds. -// An IP that empties its bucket is added to a ban list for BanDurationSeconds. // -// rl := proxy.NewRateLimiter(cfg.RateLimit) -// if !rl.Allow("1.2.3.4") { conn.Close(); return } +// rl := proxy.NewRateLimiter(proxy.RateLimit{MaxConnectionsPerMinute: 30, BanDurationSeconds: 300}) +// if rl.Allow("1.2.3.4:3333") { proceed() } type RateLimiter struct { cfg RateLimit buckets map[string]*tokenBucket diff --git a/state_impl.go b/state_impl.go index fc56bd8..04e1b88 100644 --- a/state_impl.go +++ b/state_impl.go @@ -77,8 +77,8 @@ func New(cfg *Config) (*Proxy, Result) { } p.events.Subscribe(EventAccept, p.onShareSettled) p.events.Subscribe(EventReject, p.onShareSettled) - if cfg.Watch && cfg.sourcePath != "" { - p.watcher = NewConfigWatcher(cfg.sourcePath, p.Reload) + if cfg.Watch && cfg.configPath != "" { + p.watcher = NewConfigWatcher(cfg.configPath, p.Reload) } if factory, ok := getSplitterFactory(cfg.Mode); ok { @@ -314,12 +314,12 @@ func (p *Proxy) Reload(cfg *Config) { preservedBind := append([]BindAddr(nil), p.config.Bind...) preservedMode := p.config.Mode preservedWorkers := p.config.Workers - preservedSourcePath := p.config.sourcePath + preservedConfigPath := p.config.configPath *p.config = *cfg p.config.Bind = preservedBind p.config.Mode = preservedMode p.config.Workers = preservedWorkers - p.config.sourcePath = preservedSourcePath + p.config.configPath = preservedConfigPath } if p.customDiff != nil { p.customDiff.globalDiff = cfg.CustomDiff