refactor(proxy): clarify internal helper naming
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
3debd08a64
commit
d8b4bf2775
2 changed files with 28 additions and 28 deletions
30
core_impl.go
30
core_impl.go
|
|
@ -23,16 +23,16 @@ type Result struct {
|
|||
Error error
|
||||
}
|
||||
|
||||
func successResult() Result {
|
||||
func newSuccessResult() Result {
|
||||
return Result{OK: true}
|
||||
}
|
||||
|
||||
func errorResult(err error) Result {
|
||||
func newErrorResult(err error) Result {
|
||||
return Result{OK: false, Error: err}
|
||||
}
|
||||
|
||||
var splitterFactoriesMu sync.RWMutex
|
||||
var splitterFactories = map[string]func(*Config, *EventBus) Splitter{}
|
||||
var splitterFactoriesByMode = map[string]func(*Config, *EventBus) Splitter{}
|
||||
|
||||
// Register a mode-specific splitter constructor.
|
||||
//
|
||||
|
|
@ -42,13 +42,13 @@ var splitterFactories = map[string]func(*Config, *EventBus) Splitter{}
|
|||
func RegisterSplitterFactory(mode string, factory func(*Config, *EventBus) Splitter) {
|
||||
splitterFactoriesMu.Lock()
|
||||
defer splitterFactoriesMu.Unlock()
|
||||
splitterFactories[strings.ToLower(mode)] = factory
|
||||
splitterFactoriesByMode[strings.ToLower(mode)] = factory
|
||||
}
|
||||
|
||||
func lookupSplitterFactory(mode string) (func(*Config, *EventBus) Splitter, bool) {
|
||||
func splitterFactoryForMode(mode string) (func(*Config, *EventBus) Splitter, bool) {
|
||||
splitterFactoriesMu.RLock()
|
||||
defer splitterFactoriesMu.RUnlock()
|
||||
factory, ok := splitterFactories[strings.ToLower(mode)]
|
||||
factory, ok := splitterFactoriesByMode[strings.ToLower(mode)]
|
||||
return factory, ok
|
||||
}
|
||||
|
||||
|
|
@ -57,12 +57,12 @@ func lookupSplitterFactory(mode string) (func(*Config, *EventBus) Splitter, bool
|
|||
func LoadConfig(path string) (*Config, Result) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, errorResult(err)
|
||||
return nil, newErrorResult(err)
|
||||
}
|
||||
|
||||
config := &Config{}
|
||||
if err := json.Unmarshal(data, config); err != nil {
|
||||
return nil, errorResult(err)
|
||||
return nil, newErrorResult(err)
|
||||
}
|
||||
config.configPath = path
|
||||
return config, config.Validate()
|
||||
|
|
@ -72,26 +72,26 @@ func LoadConfig(path string) (*Config, Result) {
|
|||
// if result := cfg.Validate(); !result.OK { return result }
|
||||
func (c *Config) Validate() Result {
|
||||
if c == nil {
|
||||
return errorResult(errors.New("config is nil"))
|
||||
return newErrorResult(errors.New("config is nil"))
|
||||
}
|
||||
if !isValidMode(c.Mode) {
|
||||
return errorResult(errors.New("mode must be \"nicehash\" or \"simple\""))
|
||||
return newErrorResult(errors.New("mode must be \"nicehash\" or \"simple\""))
|
||||
}
|
||||
if !isValidWorkersMode(c.Workers) {
|
||||
return errorResult(errors.New("workers must be one of \"rig-id\", \"user\", \"password\", \"agent\", \"ip\", or \"false\""))
|
||||
return newErrorResult(errors.New("workers must be one of \"rig-id\", \"user\", \"password\", \"agent\", \"ip\", or \"false\""))
|
||||
}
|
||||
if len(c.Bind) == 0 {
|
||||
return errorResult(errors.New("bind list is empty"))
|
||||
return newErrorResult(errors.New("bind list is empty"))
|
||||
}
|
||||
if len(c.Pools) == 0 {
|
||||
return errorResult(errors.New("pool list is empty"))
|
||||
return newErrorResult(errors.New("pool list is empty"))
|
||||
}
|
||||
for _, pool := range c.Pools {
|
||||
if pool.Enabled && strings.TrimSpace(pool.URL) == "" {
|
||||
return errorResult(errors.New("enabled pool url is empty"))
|
||||
return newErrorResult(errors.New("enabled pool url is empty"))
|
||||
}
|
||||
}
|
||||
return successResult()
|
||||
return newSuccessResult()
|
||||
}
|
||||
|
||||
func isValidMode(mode string) bool {
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ type MinerSnapshot struct {
|
|||
// }
|
||||
func New(config *Config) (*Proxy, Result) {
|
||||
if config == nil {
|
||||
return nil, errorResult(errors.New("config is nil"))
|
||||
return nil, newErrorResult(errors.New("config is nil"))
|
||||
}
|
||||
if result := config.Validate(); !result.OK {
|
||||
return nil, result
|
||||
|
|
@ -83,13 +83,13 @@ func New(config *Config) (*Proxy, Result) {
|
|||
p.watcher = NewConfigWatcher(config.configPath, p.Reload)
|
||||
}
|
||||
|
||||
if factory, ok := lookupSplitterFactory(config.Mode); ok {
|
||||
if factory, ok := splitterFactoryForMode(config.Mode); ok {
|
||||
p.splitter = factory(config, p.events)
|
||||
} else {
|
||||
p.splitter = &noopSplitter{}
|
||||
}
|
||||
|
||||
return p, successResult()
|
||||
return p, newSuccessResult()
|
||||
}
|
||||
|
||||
// p.Mode()
|
||||
|
|
@ -468,19 +468,19 @@ func (p *Proxy) acceptMiner(conn net.Conn, localPort uint16) {
|
|||
|
||||
func buildTLSConfig(cfg TLSConfig) (*tls.Config, Result) {
|
||||
if !cfg.Enabled {
|
||||
return nil, successResult()
|
||||
return nil, newSuccessResult()
|
||||
}
|
||||
if cfg.CertFile == "" || cfg.KeyFile == "" {
|
||||
return nil, errorResult(errors.New("tls certificate or key path is empty"))
|
||||
return nil, newErrorResult(errors.New("tls certificate or key path is empty"))
|
||||
}
|
||||
cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.KeyFile)
|
||||
if err != nil {
|
||||
return nil, errorResult(err)
|
||||
return nil, newErrorResult(err)
|
||||
}
|
||||
tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}}
|
||||
applyTLSProtocols(tlsConfig, cfg.Protocols)
|
||||
applyTLSCiphers(tlsConfig, cfg.Ciphers)
|
||||
return tlsConfig, successResult()
|
||||
return tlsConfig, newSuccessResult()
|
||||
}
|
||||
|
||||
func applyTLSProtocols(tlsConfig *tls.Config, protocols string) {
|
||||
|
|
@ -1683,7 +1683,7 @@ func NewServer(bind BindAddr, tlsCfg *tls.Config, limiter *RateLimiter, onAccept
|
|||
if result := server.listen(); !result.OK {
|
||||
return nil, result
|
||||
}
|
||||
return server, successResult()
|
||||
return server, newSuccessResult()
|
||||
}
|
||||
|
||||
// Start begins accepting connections in a goroutine.
|
||||
|
|
@ -1737,23 +1737,23 @@ func (s *Server) Stop() {
|
|||
|
||||
func (s *Server) listen() Result {
|
||||
if s == nil {
|
||||
return errorResult(errors.New("server is nil"))
|
||||
return newErrorResult(errors.New("server is nil"))
|
||||
}
|
||||
if s.listener != nil {
|
||||
return successResult()
|
||||
return newSuccessResult()
|
||||
}
|
||||
if s.addr.TLS && s.tlsCfg == nil {
|
||||
return errorResult(errors.New("tls listener requires a tls config"))
|
||||
return newErrorResult(errors.New("tls listener requires a tls config"))
|
||||
}
|
||||
ln, err := net.Listen("tcp", net.JoinHostPort(s.addr.Host, strconv.Itoa(int(s.addr.Port))))
|
||||
if err != nil {
|
||||
return errorResult(err)
|
||||
return newErrorResult(err)
|
||||
}
|
||||
if s.tlsCfg != nil {
|
||||
ln = tls.NewListener(ln, s.tlsCfg)
|
||||
}
|
||||
s.listener = ln
|
||||
return successResult()
|
||||
return newSuccessResult()
|
||||
}
|
||||
|
||||
// IsActive reports whether the limiter has enabled rate limiting.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue