Skip to content

Commit 8a11cf4

Browse files
committed
feat: initial release of go-coldbrew/workers
Worker lifecycle library built on thejerf/suture with: - Worker struct with builder pattern (NewWorker, WithRestart, Every, WithFailureBackoff, WithFailureThreshold, WithFailureDecay, WithBackoffJitter, WithTimeout) - WorkerContext interface extending context.Context with Name(), Attempt(), Add(), Remove(), Children() for dynamic worker management - Every worker is a supervisor subtree — scoped child lifecycle - Tracing via go-coldbrew/tracing (OTEL spans per worker execution) - Logging via go-coldbrew/log - Helpers: EveryInterval, ChannelWorker, BatchChannelWorker - Run(ctx, []*Worker) and RunWorker(ctx, *Worker) entry points - 12 examples including dynamic worker pool reconciliation - GitHub Actions CI (build, test, lint)
0 parents  commit 8a11cf4

15 files changed

Lines changed: 3451 additions & 0 deletions

.github/workflows/go.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Go
5+
6+
on:
7+
push:
8+
branches: [ "main" ]
9+
pull_request:
10+
branches: [ "main" ]
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
17+
build:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
21+
22+
- name: Set up Go
23+
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
24+
with:
25+
go-version-file: go.mod
26+
27+
- name: Build
28+
run: make build
29+
30+
test:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
34+
35+
- name: Set up Go
36+
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
37+
with:
38+
go-version-file: go.mod
39+
40+
- name: Test
41+
run: make test
42+
43+
lint:
44+
runs-on: ubuntu-latest
45+
steps:
46+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
47+
48+
- name: Set up Go
49+
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
50+
with:
51+
go-version-file: go.mod
52+
53+
- name: Lint
54+
run: make lint

.golangci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: "2"
2+
linters:
3+
exclusions:
4+
generated: lax
5+
presets:
6+
- comments
7+
- common-false-positives
8+
- legacy
9+
- std-error-handling
10+
rules:
11+
- linters:
12+
- funlen
13+
- goconst
14+
- errcheck
15+
- ineffassign
16+
- staticcheck
17+
path: (.+)_test\.go
18+
paths:
19+
- third_party$
20+
- builtin$
21+
- examples$
22+
formatters:
23+
exclusions:
24+
generated: lax
25+
paths:
26+
- third_party$
27+
- builtin$
28+
- examples$

AGENTS.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# AGENTS.md
2+
3+
## Package Overview
4+
5+
`go-coldbrew/workers` is a worker lifecycle library built on [thejerf/suture](https://github.com/thejerf/suture). It manages background goroutines with panic recovery, configurable restart, tracing, and structured shutdown.
6+
7+
## Build & Test
8+
9+
```bash
10+
make build # go build ./...
11+
make test # go test -race ./...
12+
make lint # golangci-lint + govulncheck
13+
make bench # benchmarks
14+
make doc # regenerate README.md via gomarkdoc
15+
```
16+
17+
## Architecture
18+
19+
Every worker runs inside its own suture supervisor subtree:
20+
21+
```
22+
Root Supervisor (created by Run)
23+
├── Worker-A supervisor
24+
│ ├── Worker-A run func
25+
│ ├── Child-A1 supervisor (added via ctx.Add)
26+
│ │ └── Child-A1 run func
27+
│ └── Child-A2 supervisor
28+
│ └── Child-A2 run func
29+
└── Worker-B supervisor
30+
└── Worker-B run func
31+
```
32+
33+
Key properties:
34+
- **Scoped lifecycle**: when a parent stops, all children stop
35+
- **Independent restart**: each worker restarts independently via suture
36+
- **Panic recovery**: suture catches panics and converts to errors
37+
- **Backoff**: configurable exponential backoff with jitter on restart
38+
- **Tracing**: each worker execution gets an OTEL span via `go-coldbrew/tracing`
39+
40+
## Key Types
41+
42+
- `Worker` — struct with builder pattern (`NewWorker().WithRestart().Every()`)
43+
- `WorkerContext` — extends `context.Context` with `Name()`, `Attempt()`, `Add()`, `Remove()`, `Children()`
44+
- `Run(ctx, []*Worker) error` — starts all workers, blocks until ctx cancelled
45+
- `RunWorker(ctx, *Worker)` — runs a single worker
46+
47+
## Helpers
48+
49+
- `EveryInterval(d, fn)` — periodic ticker loop
50+
- `ChannelWorker[T](ch, fn)` — consume from channel
51+
- `BatchChannelWorker[T](ch, maxSize, maxDelay, fn)` — batch with size/timer flush
52+
53+
## Rules
54+
55+
- Always run `make doc` after changing exported APIs or docstrings
56+
- Always run `make test` and `make lint` before committing
57+
- Don't add `replace` directives to go.mod

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@AGENTS.md

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.PHONY: build test doc lint bench
2+
build:
3+
go build ./...
4+
5+
test:
6+
go test -race ./...
7+
8+
doc:
9+
go tool gomarkdoc --output '{{.Dir}}/README.md' ./...
10+
11+
lint:
12+
go tool golangci-lint run
13+
go tool govulncheck ./...
14+
15+
bench:
16+
go test -run=^$$ -bench=. -benchmem ./...

0 commit comments

Comments
 (0)