Repository navigation
Expand file tree
/
Copy pathpool.go
More file actions
249 lines (221 loc) · 5.55 KB
/
Copy pathpool.go
File metadata and controls
249 lines (221 loc) · 5.55 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
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
package picoloom
import (
"errors"
"runtime"
"sync"
)
// Pool sizing constants.
const (
// MinPoolSize ensures at least one worker is available.
MinPoolSize = 1
// MaxPoolSize caps browser instances to limit memory (~200MB each).
MaxPoolSize = 8
// cpuDivisor leaves headroom for Chrome child processes.
cpuDivisor = 2
)
// ConverterPool manages a pool of Converter instances for parallel processing.
// Each converter has its own browser instance, enabling true parallelism.
// Converters are created lazily on first acquire to avoid startup delay.
type ConverterPool struct {
size int
opts []Option
converters []*Converter
sem chan *Converter
closedCh chan struct{}
mu sync.Mutex
created int
closed bool
initErr error // First error encountered during converter creation
}
// ServicePool is an alias for ConverterPool for backward compatibility.
//
// Deprecated: Use ConverterPool instead. This alias will be removed in v2.
type ServicePool = ConverterPool
// NewConverterPool creates a pool with capacity for n Converter instances.
// Converters are created lazily when acquired, not at pool creation.
// Options are applied to each converter when created.
//
// Each converter owns a browser process. Explicit sizes are not capped by the
// library; callers are responsible for choosing a size their environment can
// support. Use ResolvePoolSize(0) for the built-in conservative auto size.
func NewConverterPool(n int, opts ...Option) *ConverterPool {
if n < 1 {
n = 1
}
return &ConverterPool{
size: n,
opts: opts,
converters: make([]*Converter, 0, n),
sem: make(chan *Converter, n),
closedCh: make(chan struct{}),
}
}
// NewServicePool creates a pool with capacity for n Converter instances.
//
// Deprecated: Use NewConverterPool instead. NewServicePool will be removed in v2.
func NewServicePool(n int, opts ...Option) *ConverterPool {
return NewConverterPool(n, opts...)
}
// Acquire gets a converter from the pool, creating one if needed.
// Blocks if all converters are in use.
// Returns nil and sets internal error if converter creation fails.
// Use InitError() to check for initialization failures.
func (p *ConverterPool) Acquire() *Converter {
// Prefer closedCh before taking an idle converter. After Close(), callers
// must not receive converters that are being closed by the pool.
select {
case <-p.closedCh:
return nil
default:
}
// Try to get an existing converter (non-blocking)
select {
case conv := <-p.sem:
select {
case <-p.closedCh:
return nil
default:
return conv
}
default:
}
// Check if we can create a new converter
p.mu.Lock()
if p.closed {
p.mu.Unlock()
return nil
}
if p.initErr != nil {
p.mu.Unlock()
return nil
}
if p.created < p.size {
p.created++
p.mu.Unlock()
// Create new converter outside the lock
conv, err := NewConverter(p.opts...)
if err != nil {
p.mu.Lock()
if p.initErr == nil {
p.initErr = err
}
p.created--
p.mu.Unlock()
return nil
}
p.mu.Lock()
if p.closed {
p.created--
p.mu.Unlock()
_ = conv.Close()
return nil
}
p.converters = append(p.converters, conv)
p.mu.Unlock()
return conv
}
p.mu.Unlock()
// All converters created, wait for one to be released.
// Return nil if the pool is closed while waiting.
select {
case <-p.closedCh:
return nil
default:
}
select {
case conv := <-p.sem:
select {
case <-p.closedCh:
return nil
default:
return conv
}
case <-p.closedCh:
return nil
}
}
// InitError returns the first error encountered during converter creation.
// Returns nil if all converters were created successfully.
func (p *ConverterPool) InitError() error {
p.mu.Lock()
defer p.mu.Unlock()
return p.initErr
}
// Release returns a converter to the pool.
// The lock is released before sending to avoid deadlock when channel is full.
func (p *ConverterPool) Release(conv *Converter) {
if conv == nil {
return
}
p.mu.Lock()
if p.closed {
p.mu.Unlock()
return
}
p.mu.Unlock()
select {
case p.sem <- conv:
case <-p.closedCh:
}
}
// Close releases all browser resources.
// Returns an aggregated error if multiple converters fail to close.
func (p *ConverterPool) Close() error {
p.mu.Lock()
if p.closed {
p.mu.Unlock()
return nil
}
p.closed = true
close(p.closedCh)
converters := p.converters
p.mu.Unlock()
errCh := make(chan error, len(converters))
var wg sync.WaitGroup
for _, conv := range converters {
if conv == nil {
continue
}
wg.Add(1)
go func(c *Converter) {
defer wg.Done()
if err := c.Close(); err != nil {
errCh <- err
}
}(conv)
}
wg.Wait()
close(errCh)
var errs []error
for err := range errCh {
errs = append(errs, err)
}
return errors.Join(errs...)
}
// Size returns the pool capacity.
func (p *ConverterPool) Size() int {
return p.size
}
// ResolvePoolSize determines the recommended pool size.
// Priority: explicit workers > GOMAXPROCS-based calculation.
//
// Explicit worker counts are returned unchanged, even above MaxPoolSize, so
// library callers can opt into larger pools when they have enough memory and
// process capacity. The automatic path clamps to MaxPoolSize because each
// converter owns a browser process.
func ResolvePoolSize(workers int) int {
// Explicit value takes priority
if workers > 0 {
return workers
}
// Auto-calculate based on GOMAXPROCS (adjusted by automaxprocs for containers)
available := runtime.GOMAXPROCS(0)
n := available / cpuDivisor
if n < MinPoolSize {
return MinPoolSize
}
if n > MaxPoolSize {
return MaxPoolSize
}
return n
}