|
| 1 | +package async |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "sync" |
| 7 | + "sync/atomic" |
| 8 | +) |
| 9 | + |
| 10 | +// ExecutorStatus represents the status of an [ExecutorService]. |
| 11 | +type ExecutorStatus uint32 |
| 12 | + |
| 13 | +const ( |
| 14 | + ExecutorStatusRunning ExecutorStatus = iota |
| 15 | + ExecutorStatusTerminating |
| 16 | + ExecutorStatusShutdown |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + ErrExecutorQueueFull = errors.New("async: executor queue is full") |
| 21 | + ErrExecutorShutdown = errors.New("async: executor is shut down") |
| 22 | +) |
| 23 | + |
| 24 | +// ExecutorService is an interface that defines a task executor. |
| 25 | +type ExecutorService[T any] interface { |
| 26 | + // Submit submits a function to the executor service. |
| 27 | + // The function will be executed asynchronously and the result will be |
| 28 | + // available via the returned future. |
| 29 | + Submit(func(context.Context) (T, error)) (Future[T], error) |
| 30 | + |
| 31 | + // Shutdown shuts down the executor service. |
| 32 | + // Once the executor service is shut down, no new tasks can be submitted |
| 33 | + // and any pending tasks will be cancelled. |
| 34 | + Shutdown() error |
| 35 | + |
| 36 | + // Status returns the current status of the executor service. |
| 37 | + Status() ExecutorStatus |
| 38 | +} |
| 39 | + |
| 40 | +// ExecutorConfig represents the Executor configuration. |
| 41 | +type ExecutorConfig struct { |
| 42 | + WorkerPoolSize int |
| 43 | + QueueSize int |
| 44 | +} |
| 45 | + |
| 46 | +// NewExecutorConfig returns a new [ExecutorConfig]. |
| 47 | +func NewExecutorConfig(workerPoolSize, queueSize int) *ExecutorConfig { |
| 48 | + return &ExecutorConfig{ |
| 49 | + WorkerPoolSize: workerPoolSize, |
| 50 | + QueueSize: queueSize, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Executor implements the [ExecutorService] interface. |
| 55 | +type Executor[T any] struct { |
| 56 | + cancel context.CancelFunc |
| 57 | + queue chan job[T] |
| 58 | + status atomic.Uint32 |
| 59 | +} |
| 60 | + |
| 61 | +var _ ExecutorService[any] = (*Executor[any])(nil) |
| 62 | + |
| 63 | +type job[T any] struct { |
| 64 | + promise Promise[T] |
| 65 | + task func(context.Context) (T, error) |
| 66 | +} |
| 67 | + |
| 68 | +// NewExecutor returns a new [Executor]. |
| 69 | +func NewExecutor[T any](ctx context.Context, config *ExecutorConfig) *Executor[T] { |
| 70 | + ctx, cancel := context.WithCancel(ctx) |
| 71 | + executor := &Executor[T]{ |
| 72 | + cancel: cancel, |
| 73 | + queue: make(chan job[T], config.QueueSize), |
| 74 | + } |
| 75 | + // init the workers pool |
| 76 | + go executor.startWorkers(ctx, config.WorkerPoolSize) |
| 77 | + |
| 78 | + // set status to terminating when ctx is done |
| 79 | + go executor.monitorCtx(ctx) |
| 80 | + |
| 81 | + // set the executor status to running |
| 82 | + executor.status.Store(uint32(ExecutorStatusRunning)) |
| 83 | + |
| 84 | + return executor |
| 85 | +} |
| 86 | + |
| 87 | +func (e *Executor[T]) monitorCtx(ctx context.Context) { |
| 88 | + <-ctx.Done() |
| 89 | + e.status.Store(uint32(ExecutorStatusTerminating)) |
| 90 | +} |
| 91 | + |
| 92 | +func (e *Executor[T]) startWorkers(ctx context.Context, poolSize int) { |
| 93 | + var wg sync.WaitGroup |
| 94 | + for i := 0; i < poolSize; i++ { |
| 95 | + wg.Add(1) |
| 96 | + go func() { |
| 97 | + defer wg.Done() |
| 98 | + loop: |
| 99 | + for ExecutorStatus(e.status.Load()) == ExecutorStatusRunning { |
| 100 | + select { |
| 101 | + case job := <-e.queue: |
| 102 | + result, err := job.task(ctx) |
| 103 | + if err != nil { |
| 104 | + job.promise.Failure(err) |
| 105 | + } else { |
| 106 | + job.promise.Success(result) |
| 107 | + } |
| 108 | + case <-ctx.Done(): |
| 109 | + break loop |
| 110 | + } |
| 111 | + } |
| 112 | + }() |
| 113 | + } |
| 114 | + |
| 115 | + // wait for all workers to exit |
| 116 | + wg.Wait() |
| 117 | + // close the queue and cancel all pending tasks |
| 118 | + close(e.queue) |
| 119 | + for job := range e.queue { |
| 120 | + job.promise.Failure(ErrExecutorShutdown) |
| 121 | + } |
| 122 | + // mark the executor as shut down |
| 123 | + e.status.Store(uint32(ExecutorStatusShutdown)) |
| 124 | +} |
| 125 | + |
| 126 | +// Submit submits a function to the executor. |
| 127 | +// The function will be executed asynchronously and the result will be |
| 128 | +// available via the returned future. |
| 129 | +func (e *Executor[T]) Submit(f func(context.Context) (T, error)) (Future[T], error) { |
| 130 | + promise := NewPromise[T]() |
| 131 | + if ExecutorStatus(e.status.Load()) == ExecutorStatusRunning { |
| 132 | + select { |
| 133 | + case e.queue <- job[T]{promise, f}: |
| 134 | + default: |
| 135 | + return nil, ErrExecutorQueueFull |
| 136 | + } |
| 137 | + } else { |
| 138 | + return nil, ErrExecutorShutdown |
| 139 | + } |
| 140 | + return promise.Future(), nil |
| 141 | +} |
| 142 | + |
| 143 | +// Shutdown shuts down the executor. |
| 144 | +// Once the executor service is shut down, no new tasks can be submitted |
| 145 | +// and any pending tasks will be cancelled. |
| 146 | +func (e *Executor[T]) Shutdown() error { |
| 147 | + e.cancel() |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +// Status returns the current status of the executor. |
| 152 | +func (e *Executor[T]) Status() ExecutorStatus { |
| 153 | + return ExecutorStatus(e.status.Load()) |
| 154 | +} |
0 commit comments