diff --git a/core_impl.go b/core_impl.go index ada5363..de75322 100644 --- a/core_impl.go +++ b/core_impl.go @@ -216,9 +216,9 @@ func (cd *CustomDiff) OnLogin(e Event) { // // limiter := proxy.NewRateLimiter(proxy.RateLimit{MaxConnectionsPerMinute: 30, BanDurationSeconds: 300}) // if limiter.Allow("203.0.113.42:3333") { /* accept */ } -func NewRateLimiter(cfg RateLimit) *RateLimiter { +func NewRateLimiter(config RateLimit) *RateLimiter { return &RateLimiter{ - config: cfg, + config: config, buckets: make(map[string]*tokenBucket), banned: make(map[string]time.Time), } @@ -286,13 +286,13 @@ func (rl *RateLimiter) Tick() { // watcher := proxy.NewConfigWatcher("config.json", func(cfg *proxy.Config) { // proxyInstance.Reload(cfg) // }) -func NewConfigWatcher(path string, onChange func(*Config)) *ConfigWatcher { +func NewConfigWatcher(configPath string, onChange func(*Config)) *ConfigWatcher { watcher := &ConfigWatcher{ - path: path, + path: configPath, onChange: onChange, done: make(chan struct{}), } - if info, err := os.Stat(path); err == nil { + if info, err := os.Stat(configPath); err == nil { watcher.lastMod = info.ModTime() } return watcher diff --git a/log/impl.go b/log/impl.go index dea337b..f11372b 100644 --- a/log/impl.go +++ b/log/impl.go @@ -51,10 +51,10 @@ func (l *ShareLog) OnReject(e proxy.Event) { l.writeRejectLine(e.Miner.User(), e.Error) } -func (l *AccessLog) writeLine(kind, ip, user, agent string, rx, tx uint64) { - l.mu.Lock() - defer l.mu.Unlock() - if err := l.ensureFile(); err != nil { +func (accessLog *AccessLog) writeLine(kind, ip, user, agent string, rx, tx uint64) { + accessLog.mu.Lock() + defer accessLog.mu.Unlock() + if err := accessLog.ensureFile(); err != nil { return } var builder strings.Builder @@ -72,13 +72,13 @@ func (l *AccessLog) writeLine(kind, ip, user, agent string, rx, tx uint64) { builder.WriteString(" tx=") builder.WriteString(strconv.FormatUint(tx, 10)) builder.WriteByte('\n') - _, _ = l.f.WriteString(builder.String()) + _, _ = accessLog.f.WriteString(builder.String()) } -func (l *ShareLog) writeAcceptLine(user string, diff uint64, latency uint64) { - l.mu.Lock() - defer l.mu.Unlock() - if err := l.ensureFile(); err != nil { +func (shareLog *ShareLog) writeAcceptLine(user string, diff uint64, latency uint64) { + shareLog.mu.Lock() + defer shareLog.mu.Unlock() + if err := shareLog.ensureFile(); err != nil { return } var builder strings.Builder @@ -92,13 +92,13 @@ func (l *ShareLog) writeAcceptLine(user string, diff uint64, latency uint64) { builder.WriteString(strconv.FormatUint(latency, 10)) builder.WriteString("ms") builder.WriteByte('\n') - _, _ = l.f.WriteString(builder.String()) + _, _ = shareLog.f.WriteString(builder.String()) } -func (l *ShareLog) writeRejectLine(user, reason string) { - l.mu.Lock() - defer l.mu.Unlock() - if err := l.ensureFile(); err != nil { +func (shareLog *ShareLog) writeRejectLine(user, reason string) { + shareLog.mu.Lock() + defer shareLog.mu.Unlock() + if err := shareLog.ensureFile(); err != nil { return } var builder strings.Builder @@ -108,29 +108,29 @@ func (l *ShareLog) writeRejectLine(user, reason string) { builder.WriteString(" reason=\"") builder.WriteString(reason) builder.WriteString("\"\n") - _, _ = l.f.WriteString(builder.String()) + _, _ = shareLog.f.WriteString(builder.String()) } -func (l *AccessLog) ensureFile() error { - if l.f != nil { +func (accessLog *AccessLog) ensureFile() error { + if accessLog.f != nil { return nil } - f, err := os.OpenFile(l.path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) + f, err := os.OpenFile(accessLog.path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) if err != nil { return err } - l.f = f + accessLog.f = f return nil } -func (l *ShareLog) ensureFile() error { - if l.f != nil { +func (shareLog *ShareLog) ensureFile() error { + if shareLog.f != nil { return nil } - f, err := os.OpenFile(l.path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) + f, err := os.OpenFile(shareLog.path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) if err != nil { return err } - l.f = f + shareLog.f = f return nil } diff --git a/pool/impl.go b/pool/impl.go index 64acfb9..8ef2184 100644 --- a/pool/impl.go +++ b/pool/impl.go @@ -18,16 +18,16 @@ import ( ) // NewStrategyFactory creates a StrategyFactory for the supplied config. -func NewStrategyFactory(cfg *proxy.Config) StrategyFactory { +func NewStrategyFactory(config *proxy.Config) StrategyFactory { return func(listener StratumListener) Strategy { - return NewFailoverStrategy(cfg.Pools, listener, cfg) + return NewFailoverStrategy(config.Pools, listener, config) } } // NewStratumClient constructs a pool client. -func NewStratumClient(cfg proxy.PoolConfig, listener StratumListener) *StratumClient { +func NewStratumClient(poolConfig proxy.PoolConfig, listener StratumListener) *StratumClient { return &StratumClient{ - config: cfg, + config: poolConfig, listener: listener, pending: make(map[int64]struct{}), } @@ -335,11 +335,11 @@ func (c *StratumClient) handleMessage(line []byte) { } // NewFailoverStrategy creates the ordered pool failover wrapper. -func NewFailoverStrategy(pools []proxy.PoolConfig, listener StratumListener, cfg *proxy.Config) *FailoverStrategy { +func NewFailoverStrategy(pools []proxy.PoolConfig, listener StratumListener, config *proxy.Config) *FailoverStrategy { return &FailoverStrategy{ pools: pools, listener: listener, - config: cfg, + config: config, } } diff --git a/splitter/nicehash/impl.go b/splitter/nicehash/impl.go index 542acaa..eb8d8aa 100644 --- a/splitter/nicehash/impl.go +++ b/splitter/nicehash/impl.go @@ -14,13 +14,13 @@ func init() { } // NewNonceSplitter creates a NiceHash splitter. -func NewNonceSplitter(cfg *proxy.Config, events *proxy.EventBus, factory pool.StrategyFactory) *NonceSplitter { +func NewNonceSplitter(config *proxy.Config, events *proxy.EventBus, factory pool.StrategyFactory) *NonceSplitter { if factory == nil { - factory = pool.NewStrategyFactory(cfg) + factory = pool.NewStrategyFactory(config) } return &NonceSplitter{ byID: make(map[int64]*NonceMapper), - config: cfg, + config: config, events: events, strategyFactory: factory, } diff --git a/splitter/simple/impl.go b/splitter/simple/impl.go index 3fee973..36ea906 100644 --- a/splitter/simple/impl.go +++ b/splitter/simple/impl.go @@ -14,14 +14,14 @@ func init() { } // NewSimpleSplitter creates the passthrough splitter. -func NewSimpleSplitter(cfg *proxy.Config, events *proxy.EventBus, factory pool.StrategyFactory) *SimpleSplitter { +func NewSimpleSplitter(config *proxy.Config, events *proxy.EventBus, factory pool.StrategyFactory) *SimpleSplitter { if factory == nil { - factory = pool.NewStrategyFactory(cfg) + factory = pool.NewStrategyFactory(config) } return &SimpleSplitter{ active: make(map[int64]*SimpleMapper), idle: make(map[int64]*SimpleMapper), - config: cfg, + config: config, events: events, factory: factory, } diff --git a/state_impl.go b/state_impl.go index 4c70211..3a4e721 100644 --- a/state_impl.go +++ b/state_impl.go @@ -37,25 +37,25 @@ type MinerSnapshot struct { // if !result.OK { // return // } -func New(cfg *Config) (*Proxy, Result) { - if cfg == nil { +func New(config *Config) (*Proxy, Result) { + if config == nil { return nil, errorResult(errors.New("config is nil")) } - if result := cfg.Validate(); !result.OK { + if result := config.Validate(); !result.OK { return nil, result } p := &Proxy{ - config: cfg, + config: config, events: NewEventBus(), stats: NewStats(), - workers: NewWorkers(cfg.Workers, nil), + workers: NewWorkers(config.Workers, nil), miners: make(map[int64]*Miner), - customDiff: NewCustomDiff(cfg.CustomDiff), - customDiffBuckets: NewCustomDiffBuckets(cfg.CustomDiffStats), - rateLimit: NewRateLimiter(cfg.RateLimit), - accessLog: newAccessLogSink(cfg.AccessLogFile), - shareLog: newShareLogSink(cfg.ShareLogFile), + customDiff: NewCustomDiff(config.CustomDiff), + customDiffBuckets: NewCustomDiffBuckets(config.CustomDiffStats), + rateLimit: NewRateLimiter(config.RateLimit), + accessLog: newAccessLogSink(config.AccessLogFile), + shareLog: newShareLogSink(config.ShareLogFile), done: make(chan struct{}), } p.events.Subscribe(EventLogin, p.customDiff.OnLogin) @@ -78,12 +78,12 @@ func New(cfg *Config) (*Proxy, Result) { } p.events.Subscribe(EventAccept, p.onShareSettled) p.events.Subscribe(EventReject, p.onShareSettled) - if cfg.Watch && cfg.configPath != "" { - p.watcher = NewConfigWatcher(cfg.configPath, p.Reload) + if config.Watch && config.configPath != "" { + p.watcher = NewConfigWatcher(config.configPath, p.Reload) } - if factory, ok := getSplitterFactory(cfg.Mode); ok { - p.splitter = factory(cfg, p.events) + if factory, ok := getSplitterFactory(config.Mode); ok { + p.splitter = factory(config, p.events) } else { p.splitter = &noopSplitter{} } @@ -312,40 +312,40 @@ func (p *Proxy) closeAllMiners() { // Reload swaps the live configuration and updates dependent state. // // p.Reload(updatedCfg) -func (p *Proxy) Reload(cfg *Config) { - if p == nil || cfg == nil { +func (p *Proxy) Reload(config *Config) { + if p == nil || config == nil { return } if p.config == nil { - p.config = cfg + p.config = config } else { preservedBind := append([]BindAddr(nil), p.config.Bind...) preservedMode := p.config.Mode preservedWorkers := p.config.Workers preservedConfigPath := p.config.configPath - *p.config = *cfg + *p.config = *config p.config.Bind = preservedBind p.config.Mode = preservedMode p.config.Workers = preservedWorkers p.config.configPath = preservedConfigPath } if p.customDiff != nil { - p.customDiff.globalDiff = cfg.CustomDiff + p.customDiff.globalDiff = config.CustomDiff } if p.customDiffBuckets != nil { - p.customDiffBuckets.SetEnabled(cfg.CustomDiffStats) + p.customDiffBuckets.SetEnabled(config.CustomDiffStats) } - p.rateLimit = NewRateLimiter(cfg.RateLimit) + p.rateLimit = NewRateLimiter(config.RateLimit) for _, server := range p.servers { if server != nil { server.limiter = p.rateLimit } } if p.accessLog != nil { - p.accessLog.SetPath(cfg.AccessLogFile) + p.accessLog.SetPath(config.AccessLogFile) } if p.shareLog != nil { - p.shareLog.SetPath(cfg.ShareLogFile) + p.shareLog.SetPath(config.ShareLogFile) } }