Repository navigation
Expand file tree
/
Copy pathpool.go
More file actions
122 lines (105 loc) · 2.37 KB
/
Copy pathpool.go
File metadata and controls
122 lines (105 loc) · 2.37 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
package graphdb
import (
"context"
"sync"
)
// Task represents a unit of work to be executed by the worker pool.
type Task func() (interface{}, error)
// taskResult holds the result of a completed task.
type taskResult struct {
Value interface{}
Err error
Index int
}
// workerPool manages a fixed set of goroutines for concurrent query execution.
type workerPool struct {
workers int
tasks chan poolTask
wg sync.WaitGroup
quit chan struct{}
once sync.Once
}
type poolTask struct {
fn Task
result chan<- taskResult
index int
}
// newWorkerPool creates and starts a pool with the given number of workers.
func newWorkerPool(size int) *workerPool {
if size <= 0 {
size = 1
}
p := &workerPool{
workers: size,
tasks: make(chan poolTask, size*4), // buffered for backpressure
quit: make(chan struct{}),
}
p.wg.Add(size)
for i := 0; i < size; i++ {
go p.worker()
}
return p
}
// worker is the main loop for a pool goroutine.
func (p *workerPool) worker() {
defer p.wg.Done()
for {
select {
case task, ok := <-p.tasks:
if !ok {
return
}
val, err := task.fn()
task.result <- taskResult{Value: val, Err: err, Index: task.index}
case <-p.quit:
return
}
}
}
// submit sends a task to the pool and returns immediately.
func (p *workerPool) submit(fn Task, resultCh chan<- taskResult, index int) {
p.tasks <- poolTask{fn: fn, result: resultCh, index: index}
}
// stop gracefully shuts down the worker pool.
func (p *workerPool) stop() {
p.once.Do(func() {
close(p.quit)
close(p.tasks)
p.wg.Wait()
})
}
// ExecuteConcurrent runs multiple tasks concurrently using the worker pool
// and returns all results in order. Respects context cancellation.
func (p *workerPool) ExecuteConcurrent(ctx context.Context, tasks []Task) []taskResult {
n := len(tasks)
if n == 0 {
return nil
}
resultCh := make(chan taskResult, n)
// Submit all tasks.
submitted := 0
for i, t := range tasks {
select {
case <-ctx.Done():
break
default:
p.submit(t, resultCh, i)
submitted++
}
}
// Collect results.
results := make([]taskResult, n)
for i := 0; i < submitted; i++ {
select {
case r := <-resultCh:
results[r.Index] = r
case <-ctx.Done():
// Fill remaining with context error.
for j := i; j < submitted; j++ {
results[j] = taskResult{Err: ctx.Err()}
}
return results
}
}
return results
}