Use for loop and ticker in worker pattern
Replace goto/LOOP with a for/select on time.Ticker for clearer control flow. Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -4,7 +4,7 @@ Background workers follow a poll-based pattern with bounded concurrency. The str
|
|||||||
|
|
||||||
## Run loop
|
## Run loop
|
||||||
|
|
||||||
The `Run(ctx context.Context) error` method loops with a `select` on `ctx.Done()` and `time.After(interval)`. On each tick it recovers stale rows, then drains available work via `processNext`. Work items are claimed inside a transaction with `FOR UPDATE SKIP LOCKED`, marked as processing, then handled concurrently in goroutines bounded by a semaphore channel. Use `context.WithoutCancel` for work that must complete even after shutdown, and `sync.WaitGroup` with `defer wg.Wait()` to ensure in-flight goroutines finish before `Run` returns.
|
The `Run(ctx context.Context) error` method uses a `time.Ticker` in a `for`/`select` loop. On each tick it recovers stale rows, then drains available work via `processNext`. Work items are claimed inside a transaction with `FOR UPDATE SKIP LOCKED`, marked as processing, then handled concurrently in goroutines bounded by a semaphore channel. Use `context.WithoutCancel` for work that must complete even after shutdown, and `sync.WaitGroup` with `defer wg.Wait()` to ensure in-flight goroutines finish before `Run` returns.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type (
|
type (
|
||||||
@@ -41,14 +41,16 @@ func (w *FooWorker) Run(ctx context.Context) error {
|
|||||||
var (
|
var (
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
sem = make(chan struct{}, w.maxConcurrency)
|
sem = make(chan struct{}, w.maxConcurrency)
|
||||||
|
ticker = time.NewTicker(w.interval)
|
||||||
)
|
)
|
||||||
|
defer ticker.Stop()
|
||||||
defer wg.Wait()
|
defer wg.Wait()
|
||||||
|
|
||||||
LOOP:
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
case <-time.After(w.interval):
|
case <-ticker.C:
|
||||||
nonCancelableCtx := context.WithoutCancel(ctx)
|
nonCancelableCtx := context.WithoutCancel(ctx)
|
||||||
w.recoverStaleRows(nonCancelableCtx)
|
w.recoverStaleRows(nonCancelableCtx)
|
||||||
for {
|
for {
|
||||||
@@ -59,7 +61,7 @@ LOOP:
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
goto LOOP
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user