AFAIRE
sdk
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
1.9 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### sdk ####### Copyright (c) 2021-2022 losyme ################################################# MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package workers
import (
"forge.chapril.org/dune/sdk"
"forge.chapril.org/losyme/errors"
)
const (
_defaultPoolSize = 10
_maxPoolSize = 20
)
type Logger interface {
Trace(msg string, kv ...interface{})
Info(msg string, kv ...interface{})
Warning(msg string, kv ...interface{})
}
type Config struct {
PoolSize int
Model sdk.Model
Runner sdk.Runner
Logger Logger
}
type ConfigOption func(*Config)
func WithPoolSize(poolSize int) ConfigOption {
return func(c *Config) {
c.PoolSize = poolSize
}
}
func WithModel(model sdk.Model) ConfigOption {
return func(c *Config) {
c.Model = model
}
}
func WithRunner(runner sdk.Runner) ConfigOption {
return func(c *Config) {
c.Runner = runner
}
}
func WithLogger(logger Logger) ConfigOption {
return func(c *Config) {
c.Logger = logger
}
}
func (c *Config) validate() error {
if c.PoolSize < 0 {
c.PoolSize = 0
} else if c.PoolSize > _maxPoolSize {
c.PoolSize = _maxPoolSize
}
if c.Runner == nil {
return errors.New("field 'Runner' cannot be nil") //////////////////////////////////////////////////////////////
}
return nil
}
func (c *Config) BuildWorkers() (*Workers, error) {
if err := c.validate(); err != nil {
return nil, err
}
state := newState()
pool := newPool(c, state)
supervisor := newSupervisor(c.PoolSize, c.Logger, pool)
ws := &Workers{
state: state,
supervisor: supervisor,
}
return ws, nil
}
/*
######################################################################################################## @(°_°)@ #######
*/