-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathllms.txt
More file actions
219 lines (178 loc) · 6.27 KB
/
llms.txt
File metadata and controls
219 lines (178 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# pool - Go Worker Pool Library
> LLM-optimized documentation. For full docs see README.md
## Quick Reference
```go
import "github.com/go-pkgz/pool"
import "github.com/go-pkgz/pool/metrics"
import "github.com/go-pkgz/pool/middleware"
// Worker interface
type Worker[T any] interface {
Do(ctx context.Context, v T) error
}
// Stateless: single shared worker
p := pool.New[T](workers, pool.WorkerFunc[T](func(ctx context.Context, v T) error {
return nil
}))
// Stateful: per-worker instances
p := pool.NewStateful[T](workers, func() pool.Worker[T] {
return &MyWorker{conn: openConn()}
})
// Lifecycle
p.Go(ctx) // start workers
p.Submit(item) // submit work (single producer)
p.Send(item) // submit work (concurrent-safe)
p.Close(ctx) // signal done + wait
p.Wait(ctx) // wait only (if Close called elsewhere)
```
## When to Use This Library
Use pool when you need:
- Stateful workers (each worker has own state/resources)
- Key-based routing (same key → same worker via WithChunkFn)
- Batching for high-throughput
- Built-in metrics and observability
- Middleware (retry, timeout, recovery, rate limiting)
- Multi-stage pipelines with collectors
Don't use if:
- Simple fan-out with no state → use errgroup directly
- Single worker → just use a goroutine
## Configuration Options
```go
p := pool.New[T](workers, worker).
WithBatchSize(10). // batch items before processing
WithWorkerChanSize(100). // buffer size per worker
WithChunkFn(func(v T) string {...}) // route by key (consistent hashing)
WithContinueOnError(). // don't stop on first error
WithWorkerCompleteFn(fn). // called when each worker finishes
WithPoolCompleteFn(fn) // called when ALL workers finish
```
## Common Patterns
### Pattern 1: Basic Processing
→ See: examples/basic/main.go
```go
p := pool.New[string](3, worker)
p.Go(ctx)
for _, item := range items {
p.Submit(item)
}
p.Close(ctx)
```
### Pattern 2: Collect Results
→ See: examples/collector_errors/main.go
```go
collector := pool.NewCollector[Result](ctx, bufferSize)
worker := pool.WorkerFunc[Input](func(ctx context.Context, v Input) error {
collector.Submit(process(v))
return nil
})
// consume with: for result, err := range collector.Iter() {...}
```
### Pattern 3: Key-Based Routing
→ See: examples/chunking/main.go
```go
// All items with same UserID go to same worker
p := pool.New[Event](3, worker).WithChunkFn(func(e Event) string {
return e.UserID
})
```
### Pattern 4: Stateful Workers
→ See: examples/tokenizer_stateful/main.go
```go
p := pool.NewStateful[T](workers, func() pool.Worker[T] {
return &MyWorker{
counts: make(map[string]int),
conn: openConnection(),
}
})
```
### Pattern 5: Multi-Stage Pipeline
→ See: examples/direct_chain/main.go, examples/collectors_chain/main.go
```go
// Stage 1 → Stage 2 coordination via pool completion
p1 := pool.New[A](...).WithPoolCompleteFn(func(ctx context.Context) error {
return p2.Close(ctx)
})
```
### Pattern 6: Middleware Stack
→ See: examples/middleware/main.go
```go
p := pool.New[T](workers, worker).Use(
middleware.Retry[T](3, time.Second),
middleware.Timeout[T](5*time.Second),
middleware.Recovery[T](panicHandler),
middleware.RateLimiter[T](10, 5),
)
```
## Metrics
```go
// Inside worker: track custom metrics
m := metrics.Get(ctx)
m.Inc("custom-counter")
m.Add("bytes-processed", n)
// After processing: get stats
stats := p.Metrics().GetStats()
stats.Processed // items processed
stats.Errors // items failed
stats.RatePerSec // throughput
stats.AvgLatency // wall-clock time per item
stats.Utilization // worker busy percentage
```
## File Map
| File | Purpose |
|------|---------|
| pool.go | Core WorkerGroup, New(), NewStateful(), Submit/Send/Close/Wait |
| collector.go | Collector for async result gathering |
| metrics/metrics.go | Stats tracking, custom counters, timers |
| middleware/*.go | Retry, Timeout, Recovery, Validator, RateLimiter |
| examples/basic/ | Minimal hello world |
| examples/chunking/ | WithChunkFn for key-based routing |
| examples/pool_completion/ | Pool vs worker completion callbacks |
| examples/tokenizer_stateful/ | Stateful workers with per-worker state |
| examples/tokenizer_stateless/ | Stateless workers with collector |
| examples/collector_errors/ | Error collection and categorization |
| examples/middleware/ | All built-in middleware demonstrated |
| examples/direct_chain/ | Multi-stage pipeline with Send() |
| examples/collectors_chain/ | Pipeline with collectors |
| examples/parallel_files/ | Chunk-based file processing |
## API Summary
### pool.WorkerGroup[T]
- `New[T](size int, worker Worker[T]) *WorkerGroup[T]`
- `NewStateful[T](size int, maker func() Worker[T]) *WorkerGroup[T]`
- `Go(ctx) error` - start workers
- `Submit(v T)` - submit item (not thread-safe)
- `Send(v T)` - submit item (thread-safe)
- `Close(ctx) error` - close + wait
- `Wait(ctx) error` - wait only
- `Metrics() *metrics.Value` - get metrics
- `Use(middlewares...) *WorkerGroup[T]` - add middleware
- `With*(...)` - configuration (call before Go)
### pool.Collector[V]
- `NewCollector[V](ctx, size) *Collector[V]`
- `Submit(v V)` - send result
- `Close()` - signal done
- `Iter() iter.Seq2[V, error]` - iterate results
- `All() ([]V, error)` - collect all
### metrics.Value
- `Get(ctx) *Value` - get from context
- `Inc(name)` - increment counter
- `Add(name, n)` - add to counter
- `GetStats() Stats` - aggregated statistics
### Middleware
- `type Middleware[T any] func(Worker[T]) Worker[T]` - wraps worker for cross-cutting concerns
## Error Handling
- Default: first error stops the pool
- `WithContinueOnError()`: accumulates errors, returns last error with count
- Worker errors don't stop other workers when continue mode enabled
- Context cancellation signals workers to stop (workers must check ctx.Done())
## Middleware Order
Middleware executes in order provided (first = outermost):
```go
p.Use(logging, retry, timeout)
// execution: logging → retry → timeout → worker
```
## Architecture Notes
For AI agents working on this codebase, see CLAUDE.md for:
- Channel architecture details
- Work distribution algorithms
- Batching implementation
- Error handling semantics
- Key implementation details