refactor(proxy): clarify runtime config naming

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-04 22:06:18 +00:00
parent 8bde2c14d0
commit 6422a948bf
11 changed files with 42 additions and 42 deletions

View file

@ -218,7 +218,7 @@ func (cd *CustomDiff) OnLogin(e Event) {
// if limiter.Allow("203.0.113.42:3333") { /* accept */ }
func NewRateLimiter(cfg RateLimit) *RateLimiter {
return &RateLimiter{
cfg: cfg,
config: cfg,
buckets: make(map[string]*tokenBucket),
banned: make(map[string]time.Time),
}
@ -226,7 +226,7 @@ func NewRateLimiter(cfg RateLimit) *RateLimiter {
// Allow returns true if the IP address is permitted to open a new connection.
func (rl *RateLimiter) Allow(ip string) bool {
if rl == nil || rl.cfg.MaxConnectionsPerMinute <= 0 {
if rl == nil || rl.config.MaxConnectionsPerMinute <= 0 {
return true
}
host := hostOnly(ip)
@ -244,14 +244,14 @@ func (rl *RateLimiter) Allow(ip string) bool {
bucket, ok := rl.buckets[host]
if !ok {
bucket = &tokenBucket{tokens: rl.cfg.MaxConnectionsPerMinute, lastRefill: now}
bucket = &tokenBucket{tokens: rl.config.MaxConnectionsPerMinute, lastRefill: now}
rl.buckets[host] = bucket
}
refillBucket(bucket, rl.cfg.MaxConnectionsPerMinute, now)
refillBucket(bucket, rl.config.MaxConnectionsPerMinute, now)
if bucket.tokens <= 0 {
if rl.cfg.BanDurationSeconds > 0 {
rl.banned[host] = now.Add(time.Duration(rl.cfg.BanDurationSeconds) * time.Second)
if rl.config.BanDurationSeconds > 0 {
rl.banned[host] = now.Add(time.Duration(rl.config.BanDurationSeconds) * time.Second)
}
return false
}
@ -263,7 +263,7 @@ func (rl *RateLimiter) Allow(ip string) bool {
// Tick removes expired ban entries and refills token buckets.
func (rl *RateLimiter) Tick() {
if rl == nil || rl.cfg.MaxConnectionsPerMinute <= 0 {
if rl == nil || rl.config.MaxConnectionsPerMinute <= 0 {
return
}
now := time.Now()
@ -277,7 +277,7 @@ func (rl *RateLimiter) Tick() {
}
}
for _, bucket := range rl.buckets {
refillBucket(bucket, rl.cfg.MaxConnectionsPerMinute, now)
refillBucket(bucket, rl.config.MaxConnectionsPerMinute, now)
}
}

View file

@ -19,7 +19,7 @@ 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

View file

@ -27,7 +27,7 @@ func NewStrategyFactory(cfg *proxy.Config) StrategyFactory {
// NewStratumClient constructs a pool client.
func NewStratumClient(cfg proxy.PoolConfig, listener StratumListener) *StratumClient {
return &StratumClient{
cfg: cfg,
config: cfg,
listener: listener,
pending: make(map[int64]struct{}),
}
@ -48,7 +48,7 @@ func (c *StratumClient) Connect() proxy.Result {
if c == nil {
return proxy.Result{OK: false, Error: errors.New("client is nil")}
}
addr := c.cfg.URL
addr := c.config.URL
if addr == "" {
return proxy.Result{OK: false, Error: errors.New("pool url is empty")}
}
@ -56,7 +56,7 @@ func (c *StratumClient) Connect() proxy.Result {
if err != nil {
return proxy.Result{OK: false, Error: err}
}
if c.cfg.TLS {
if c.config.TLS {
host := addr
if strings.Contains(addr, ":") {
host, _, _ = net.SplitHostPort(addr)
@ -67,7 +67,7 @@ func (c *StratumClient) Connect() proxy.Result {
_ = conn.Close()
return proxy.Result{OK: false, Error: err}
}
if fp := strings.TrimSpace(strings.ToLower(c.cfg.TLSFingerprint)); fp != "" {
if fp := strings.TrimSpace(strings.ToLower(c.config.TLSFingerprint)); fp != "" {
cert := tlsConn.ConnectionState().PeerCertificates
if len(cert) == 0 {
_ = tlsConn.Close()
@ -94,14 +94,14 @@ func (c *StratumClient) Login() {
return
}
params := map[string]any{
"login": c.cfg.User,
"pass": c.cfg.Pass,
"login": c.config.User,
"pass": c.config.Pass,
}
if c.cfg.RigID != "" {
params["rigid"] = c.cfg.RigID
if c.config.RigID != "" {
params["rigid"] = c.config.RigID
}
if c.cfg.Algo != "" {
params["algo"] = []string{c.cfg.Algo}
if c.config.Algo != "" {
params["algo"] = []string{c.config.Algo}
}
req := map[string]any{
"id": 1,
@ -339,7 +339,7 @@ func NewFailoverStrategy(pools []proxy.PoolConfig, listener StratumListener, cfg
return &FailoverStrategy{
pools: pools,
listener: listener,
cfg: cfg,
config: cfg,
}
}
@ -361,12 +361,12 @@ func (s *FailoverStrategy) connectLocked(start int) {
}
retries := 1
retryPause := time.Second
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 {
retryPause = time.Duration(s.cfg.RetryPause) * time.Second
if s.config.RetryPause > 0 {
retryPause = time.Duration(s.config.RetryPause) * time.Second
}
}
for attempt := 0; attempt < retries; attempt++ {
@ -389,8 +389,8 @@ func (s *FailoverStrategy) currentPools() []proxy.PoolConfig {
if s == nil {
return nil
}
if s.cfg != nil && len(s.cfg.Pools) > 0 {
return s.cfg.Pools
if s.config != nil && len(s.config.Pools) > 0 {
return s.config.Pools
}
return s.pools
}
@ -431,7 +431,7 @@ func (s *FailoverStrategy) Tick(ticks uint64) {
s.mu.Lock()
client := s.client
s.mu.Unlock()
if client != nil && client.cfg.Keepalive {
if client != nil && client.config.Keepalive {
client.Keepalive()
}
}

View file

@ -17,7 +17,7 @@ type FailoverStrategy struct {
current int
client *StratumClient
listener StratumListener
cfg *proxy.Config
config *proxy.Config
closing bool
mu sync.Mutex
}

View file

@ -106,7 +106,7 @@ type ConfigWatcher struct {
// rl := proxy.NewRateLimiter(proxy.RateLimit{MaxConnectionsPerMinute: 30, BanDurationSeconds: 300})
// if rl.Allow("1.2.3.4:3333") { proceed() }
type RateLimiter struct {
cfg RateLimit
config RateLimit
buckets map[string]*tokenBucket
banned map[string]time.Time
mu sync.Mutex

View file

@ -20,7 +20,7 @@ func NewNonceSplitter(cfg *proxy.Config, events *proxy.EventBus, factory pool.St
}
return &NonceSplitter{
byID: make(map[int64]*NonceMapper),
cfg: cfg,
config: cfg,
events: events,
strategyFactory: factory,
}
@ -171,7 +171,7 @@ func (s *NonceSplitter) Disconnect() {
func (s *NonceSplitter) addMapperLocked() *NonceMapper {
id := s.seq
s.seq++
mapper := NewNonceMapper(id, s.cfg, nil)
mapper := NewNonceMapper(id, s.config, nil)
mapper.events = s.events
mapper.lastUsed = time.Now()
mapper.strategy = s.strategyFactory(mapper)
@ -191,7 +191,7 @@ func NewNonceMapper(id int64, cfg *proxy.Config, strategy pool.Strategy) *NonceM
storage: NewNonceStorage(),
strategy: strategy,
pending: make(map[int64]SubmitContext),
cfg: cfg,
config: cfg,
}
}

View file

@ -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}
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

View file

@ -24,7 +24,7 @@ import (
type NonceSplitter struct {
mappers []*NonceMapper
byID map[int64]*NonceMapper
cfg *proxy.Config
config *proxy.Config
events *proxy.EventBus
strategyFactory pool.StrategyFactory
mu sync.RWMutex

View file

@ -21,7 +21,7 @@ func NewSimpleSplitter(cfg *proxy.Config, events *proxy.EventBus, factory pool.S
return &SimpleSplitter{
active: make(map[int64]*SimpleMapper),
idle: make(map[int64]*SimpleMapper),
cfg: cfg,
config: cfg,
events: events,
factory: factory,
}
@ -55,9 +55,9 @@ func (s *SimpleSplitter) OnLogin(event *proxy.LoginEvent) {
defer s.mu.Unlock()
now := time.Now()
if s.cfg.ReuseTimeout > 0 {
if s.config.ReuseTimeout > 0 {
for id, mapper := range s.idle {
if mapper.strategy != nil && mapper.strategy.IsActive() && !mapper.idleAt.IsZero() && now.Sub(mapper.idleAt) <= time.Duration(s.cfg.ReuseTimeout)*time.Second {
if mapper.strategy != nil && mapper.strategy.IsActive() && !mapper.idleAt.IsZero() && now.Sub(mapper.idleAt) <= time.Duration(s.config.ReuseTimeout)*time.Second {
delete(s.idle, id)
mapper.miner = event.Miner
mapper.idleAt = time.Time{}
@ -109,7 +109,7 @@ func (s *SimpleSplitter) OnClose(event *proxy.CloseEvent) {
mapper.miner = nil
mapper.idleAt = time.Now()
event.Miner.SetRouteID(-1)
if s.cfg.ReuseTimeout > 0 {
if s.config.ReuseTimeout > 0 {
s.idle[mapper.id] = mapper
return
}
@ -128,7 +128,7 @@ func (s *SimpleSplitter) GC() {
defer s.mu.Unlock()
now := time.Now()
for id, mapper := range s.idle {
if mapper.stopped || (s.cfg.ReuseTimeout > 0 && now.Sub(mapper.idleAt) > time.Duration(s.cfg.ReuseTimeout)*time.Second) {
if mapper.stopped || (s.config.ReuseTimeout > 0 && now.Sub(mapper.idleAt) > time.Duration(s.config.ReuseTimeout)*time.Second) {
if mapper.strategy != nil {
mapper.strategy.Disconnect()
}

View file

@ -20,7 +20,7 @@ import (
type SimpleSplitter struct {
active map[int64]*SimpleMapper // minerID → mapper
idle map[int64]*SimpleMapper // mapperID → mapper (reuse pool, keyed by mapper seq)
cfg *proxy.Config
config *proxy.Config
events *proxy.EventBus
factory pool.StrategyFactory
mu sync.Mutex

View file

@ -1663,7 +1663,7 @@ func (s *Server) listen() Result {
// IsActive reports whether the limiter has enabled rate limiting.
func (rl *RateLimiter) IsActive() bool {
return rl != nil && rl.cfg.MaxConnectionsPerMinute > 0
return rl != nil && rl.config.MaxConnectionsPerMinute > 0
}
func nextMinerID() int64 { return atomic.AddInt64(&minerSeq, 1) }