-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
391 lines (334 loc) · 9.11 KB
/
pool.go
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package qpool
import (
"context"
"fmt"
"sync"
"time"
"github.com/theapemachine/errnie"
)
/*
Q is our hybrid worker pool/message queue implementation.
It combines traditional worker pool functionality with quantum-inspired state management.
The pool maintains a balance between worker availability and job scheduling while
providing quantum-like properties such as state superposition and entanglement through
its integration with QSpace.
Key features:
- Dynamic worker scaling
- Circuit breaker pattern support
- Quantum-inspired state management
- Metrics collection and monitoring
*/
type Q struct {
ctx context.Context
cancel context.CancelFunc
quit chan struct{}
wg sync.WaitGroup
workers chan chan Job
jobs chan Job
space *QSpace
scaler *Scaler
metrics *Metrics
breakers map[string]*CircuitBreaker
workerMu sync.Mutex
workerList []*Worker
breakersMu sync.RWMutex
config *Config
}
/*
NewQ creates a new quantum pool with the specified worker constraints and configuration.
The pool initializes with the minimum number of workers and scales dynamically based on load.
Parameters:
- ctx: Parent context for lifecycle management
- minWorkers: Minimum number of workers to maintain
- maxWorkers: Maximum number of workers allowed
- config: Pool configuration parameters
Returns:
- *Q: A new quantum pool instance
*/
func NewQ(ctx context.Context, minWorkers, maxWorkers int, config *Config) *Q {
ctx, cancel := context.WithCancel(ctx)
q := &Q{
ctx: ctx,
cancel: cancel,
breakers: make(map[string]*CircuitBreaker),
workerList: make([]*Worker, 0),
quit: make(chan struct{}),
jobs: make(chan Job, maxWorkers*10),
workers: make(chan chan Job, maxWorkers),
space: NewQSpace(),
metrics: NewMetrics(),
config: config,
}
// Start initial workers
for i := 0; i < minWorkers; i++ {
q.startWorker()
}
// Start the manager goroutine
q.wg.Add(1)
go func() {
defer q.wg.Done()
q.manage()
}()
// Start metrics collection
q.wg.Add(1)
go func() {
defer q.wg.Done()
q.collectMetrics()
}()
// Start scaler with appropriate configuration
scalerConfig := &ScalerConfig{
TargetLoad: 2.0, // Reasonable target load
ScaleUpThreshold: 4.0, // Scale up when load is high
ScaleDownThreshold: 1.0, // Scale down when load is low
Cooldown: time.Millisecond * 500, // Reasonable cooldown
}
q.scaler = NewScaler(q, minWorkers, maxWorkers, scalerConfig)
return q
}
/*
manage handles the core job scheduling loop of the quantum pool.
It distributes jobs to available workers while respecting timeouts and
maintaining quantum state consistency through QSpace integration.
This method runs as a goroutine and continues until the pool's context is cancelled.
*/
func (q *Q) manage() {
for {
select {
case <-q.ctx.Done():
return
case job := <-q.jobs:
// Wait for a worker with timeout
select {
case <-q.ctx.Done():
return
case workerChan := <-q.workers:
// Send job to worker
select {
case workerChan <- job:
// Job successfully sent to worker
case <-q.ctx.Done():
return
}
case <-time.After(q.getSchedulingTimeout()):
errnie.Warn("No available workers for job: %s, timeout occurred", job.ID)
// Store error result since we couldn't process the job
q.space.Store(job.ID, nil, []State{{
Value: fmt.Errorf("no available workers"),
Probability: 1.0,
}}, job.TTL)
}
}
}
}
/*
collectMetrics collects and updates metrics for the quantum pool.
It periodically updates metrics such as job queue size, active workers, and
other relevant statistics. This method runs as a goroutine and continues until
the pool's context is cancelled.
*/
func (q *Q) collectMetrics() {
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-q.ctx.Done():
return
case <-ticker.C:
q.metrics.mu.Lock()
q.metrics.JobQueueSize = len(q.jobs)
q.metrics.ActiveWorkers = len(q.workers)
q.metrics.mu.Unlock()
}
}
}
/*
Schedule submits a job to the quantum pool for execution.
The job is processed according to quantum-inspired principles, maintaining
state history and uncertainty levels through QSpace integration.
Parameters:
- id: Unique identifier for the job
- fn: The function to execute
- opts: Optional job configuration parameters
Returns:
- chan *QValue: Channel that will receive the job's result
*/
func (q *Q) Schedule(id string, fn func() (any, error), opts ...JobOption) chan *QValue {
// Create context with configured timeout
ctx, cancel := context.WithTimeout(q.ctx, q.getSchedulingTimeout())
defer cancel()
startTime := time.Now()
job := Job{
ID: id,
Fn: fn,
RetryPolicy: &RetryPolicy{
MaxAttempts: 3,
Strategy: &ExponentialBackoff{Initial: time.Second},
},
StartTime: startTime,
}
// Apply options
for _, opt := range opts {
opt(&job)
}
// Check circuit breaker if configured
if job.CircuitID != "" {
breaker := q.getCircuitBreaker(job)
if breaker != nil && !breaker.Allow() {
ch := make(chan *QValue, 1)
ch <- &QValue{
Error: fmt.Errorf("circuit breaker %s is open", job.CircuitID),
CreatedAt: time.Now(),
}
close(ch)
return ch
}
}
// Try to schedule job with context timeout
select {
case q.jobs <- job:
// Use the pointer channel directly from QSpace
return q.space.Await(id)
case <-ctx.Done():
ch := make(chan *QValue, 1)
ch <- &QValue{
Error: fmt.Errorf("job scheduling timeout: %w", ctx.Err()),
CreatedAt: time.Now(),
}
close(ch)
// Update metrics for scheduling failure
q.metrics.mu.Lock()
q.metrics.SchedulingFailures++
q.metrics.mu.Unlock()
return ch
}
}
/*
CreateBroadcastGroup creates a new broadcast group.
Initializes a new broadcast group with specified parameters and default
quantum properties such as minimum uncertainty.
*/
func (q *Q) CreateBroadcastGroup(id string, ttl time.Duration) *BroadcastGroup {
return q.space.CreateBroadcastGroup(id, ttl)
}
/*
Subscribe returns a channel for receiving values from a broadcast group.
Provides a channel for receiving quantum values from a specific broadcast group.
*/
func (q *Q) Subscribe(groupID string) chan QValue {
qvChan := q.space.Subscribe(groupID)
if qvChan == nil {
return nil
}
resultChan := make(chan QValue, 10)
go func() {
defer close(resultChan)
for qv := range qvChan {
if qv != nil {
resultChan <- QValue{
Value: qv.Value,
Error: qv.Error,
CreatedAt: qv.CreatedAt,
}
}
}
}()
return resultChan
}
/*
startWorker starts a new worker.
Initializes a new worker and adds it to the pool's worker list.
*/
func (q *Q) startWorker() {
worker := &Worker{
pool: q,
jobs: make(chan Job),
cancel: nil,
}
q.workerMu.Lock()
q.workerList = append(q.workerList, worker)
q.workerMu.Unlock()
q.metrics.mu.Lock()
q.metrics.WorkerCount++
q.metrics.mu.Unlock()
q.wg.Add(1)
go func() {
defer q.wg.Done()
worker.run()
}()
errnie.Info("Started worker, total workers: %d", q.metrics.WorkerCount)
}
/*
WithTTL configures TTL for a job.
Sets the time-to-live (TTL) for a job, which determines how long the job will
remain in the system before being discarded.
*/
func WithTTL(ttl time.Duration) JobOption {
return func(j *Job) {
j.TTL = ttl
}
}
/*
getCircuitBreaker returns the circuit breaker for a job.
If the job does not have a circuit ID or configuration, it returns nil.
*/
func (q *Q) getCircuitBreaker(job Job) *CircuitBreaker {
if job.CircuitID == "" || job.CircuitConfig == nil {
return nil
}
q.breakersMu.Lock()
defer q.breakersMu.Unlock()
breaker, exists := q.breakers[job.CircuitID]
if !exists {
breaker = &CircuitBreaker{
maxFailures: job.CircuitConfig.MaxFailures,
resetTimeout: job.CircuitConfig.ResetTimeout,
halfOpenMax: job.CircuitConfig.HalfOpenMax,
state: CircuitClosed,
}
q.breakers[job.CircuitID] = breaker
}
return breaker
}
/*
getSchedulingTimeout returns the scheduling timeout from the configuration or
uses a default value if not specified.
*/
func (q *Q) getSchedulingTimeout() time.Duration {
if q.config != nil && q.config.SchedulingTimeout > 0 {
return q.config.SchedulingTimeout
}
return 5 * time.Second // Default timeout
}
/*
Close gracefully shuts down the quantum pool.
It ensures all workers complete their current jobs and cleans up resources.
The shutdown process:
1. Cancels the pool's context
2. Waits for all goroutines to complete
3. Closes all channels safely
4. Cleans up worker resources
*/
func (q *Q) Close() {
if q == nil {
return
}
errnie.Info("Closing Quantum Pool")
// Cancel context first to stop all operations
if q.cancel != nil {
errnie.Info("Cancelling context")
q.cancel()
}
// Wait for all goroutines to finish before closing channels
q.wg.Wait()
// Now it's safe to close channels as no goroutines are using them
q.workerMu.Lock()
for _, worker := range q.workerList {
close(worker.jobs)
}
q.workerList = nil
q.workerMu.Unlock()
close(q.quit)
close(q.jobs)
close(q.workers)
errnie.Info("Quantum Pool closed")
}