Implement default worker factory
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
// Types
|
// Types
|
||||||
|
|
||||||
type WorkerFactory func(id string, stop <-chan struct{}) Worker
|
type WorkerFactory func(id string, stop <-chan struct{}) (*Worker, error)
|
||||||
|
|
||||||
// Pool Config
|
// Pool Config
|
||||||
|
|
||||||
|
|||||||
@@ -67,6 +67,13 @@ func NewPool(config *PoolConfig, logger *slog.Logger) (*Pool, error) {
|
|||||||
config = GetDefaultPoolConfig()
|
config = GetDefaultPoolConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if a custom factory is supplied, config.WorkerConfig is not used
|
||||||
|
if config.WorkerFactory == nil {
|
||||||
|
config.WorkerFactory = func(id string, stop <-chan struct{}) (*Worker, error) {
|
||||||
|
return NewWorker(id, stop, config.WorkerConfig)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := ValidatePoolConfig(config); err != nil {
|
if err := ValidatePoolConfig(config); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,15 @@ func NewWorker(
|
|||||||
config *WorkerConfig,
|
config *WorkerConfig,
|
||||||
|
|
||||||
) (*Worker, error) {
|
) (*Worker, error) {
|
||||||
|
if config == nil {
|
||||||
|
config = GetDefaultWorkerConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
err := ValidateWorkerConfig(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
w := &Worker{
|
w := &Worker{
|
||||||
id: id,
|
id: id,
|
||||||
stop: stop,
|
stop: stop,
|
||||||
|
|||||||
Reference in New Issue
Block a user