-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathpool.go
More file actions
49 lines (45 loc) · 1.31 KB
/
pool.go
File metadata and controls
49 lines (45 loc) · 1.31 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
package queue
// NewPool creates a ready-to-use in-memory queue with the Ring buffer worker.
// This is the recommended way to create a queue for most use cases.
//
// Key differences from NewQueue:
// - Automatically creates and attaches a Ring buffer worker (no need for WithWorker)
// - Calls Start() automatically so the queue begins processing immediately
// - Panics on error instead of returning an error (simplifies initialization)
//
// Parameters:
// - size: Number of worker goroutines (if <= 0, defaults to runtime.NumCPU())
// - opts: Additional options to customize the queue (WithLogger, WithQueueSize, etc.)
//
// Example:
//
// // Create a pool with 5 workers and custom capacity
// q := queue.NewPool(5, queue.WithQueueSize(100))
// defer q.Release()
//
// // Queue tasks
// q.QueueTask(func(ctx context.Context) error {
// // Process task
// return nil
// })
//
// Use NewQueue instead if you need:
// - Custom worker implementations (NSQ, NATS, Redis, etc.)
// - Manual control over when to start the queue
// - Error handling during queue creation
func NewPool(size int64, opts ...Option) *Queue {
o := []Option{
WithWorkerCount(size),
WithWorker(NewRing(opts...)),
}
o = append(
o,
opts...,
)
q, err := NewQueue(o...)
if err != nil {
panic(err)
}
q.Start()
return q
}