-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathengine_pool.go
More file actions
395 lines (348 loc) · 10.3 KB
/
Copy pathengine_pool.go
File metadata and controls
395 lines (348 loc) · 10.3 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
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
392
393
394
395
package script_engine
import (
"context"
"errors"
"fmt"
"sync"
"github.com/tx7do/go-scripts/source"
)
// EnginePool manages a fixed number of independent Engine instances to support
// concurrent execution. The pool is created via NewEnginePool, which uses the
// factory registered for typ to instantiate each Engine.
type EnginePool struct {
pool chan Engine
size int
mu sync.Mutex
closed bool
}
// NewEnginePool creates and initializes a pool of size Engines.
// typ selects the registered factory used to create each Engine.
func NewEnginePool(size int, typ Type) (*EnginePool, error) {
if size < 1 {
return nil, errors.New("pool size must be >= 1")
}
p := &EnginePool{
pool: make(chan Engine, size),
size: size,
}
// Create and initialize each Engine; clean up on failure.
created := make([]Engine, 0, size)
for i := 0; i < size; i++ {
eng, err := NewScriptEngine(typ)
if err != nil {
// Clean up already-created engines.
for _, e := range created {
_ = e.Close()
}
return nil, fmt.Errorf("factory failed: %w", err)
}
// Call Init; on failure clean up and bail out.
if initErr := eng.Init(context.Background()); initErr != nil {
_ = eng.Close()
for _, e := range created {
_ = e.Close()
}
return nil, fmt.Errorf("init failed: %w", initErr)
}
created = append(created, eng)
}
for _, e := range created {
p.pool <- e
}
return p, nil
}
// Acquire takes an Engine out of the pool. It blocks until one becomes available.
// Returns an error if the pool is already closed.
func (p *EnginePool) Acquire() (Engine, error) {
p.mu.Lock()
if p.closed {
p.mu.Unlock()
return nil, errors.New("engine pool closed")
}
p.mu.Unlock()
eng, ok := <-p.pool
if !ok {
return nil, errors.New("engine pool closed")
}
return eng, nil
}
// Release returns an Engine to the pool. If the pool is already closed, the
// Engine is Closed instead.
func (p *EnginePool) Release(e Engine) {
if e == nil {
return
}
p.mu.Lock()
closed := p.closed
p.mu.Unlock()
if closed {
_ = e.Close()
return
}
// Guard against send-on-closed panic caused by concurrent Close.
defer func() {
if r := recover(); r != nil {
_ = e.Close()
}
}()
select {
case p.pool <- e:
default:
_ = e.Close()
}
}
// Close closes the pool and destroys all pooled Engines.
func (p *EnginePool) Close() error {
p.mu.Lock()
if p.closed {
p.mu.Unlock()
return nil
}
p.closed = true
close(p.pool)
p.mu.Unlock()
var lastErr error
for eng := range p.pool {
if err := eng.Close(); err != nil {
lastErr = err
}
}
return lastErr
}
// IsClosed reports whether the pool has been closed.
func (p *EnginePool) IsClosed() bool {
p.mu.Lock()
defer p.mu.Unlock()
return p.closed
}
// The following methods are common wrappers that follow the pattern
// Acquire -> invoke -> Release.
// They exist so callers can avoid the Acquire/Release boilerplate for trivial
// one-shot calls. They mirror the current Engine interface 1:1.
//
// IMPORTANT: per-call wrappers acquire a single Engine, perform the operation,
// and release it back. Stateful bindings (SetSource, RegisterGlobal,
// RegisterFunction, RegisterModule) are therefore LOCAL TO THAT ENGINE INSTANCE.
// If you need a binding to apply to every Engine in the pool, iterate over
// Acquire/Release yourself or pre-configure each Engine before pooling.
// InitAll re-initializes every Engine in the pool.
// It acquires all instances, calls Init on each, and releases them back.
func (p *EnginePool) InitAll(ctx context.Context) error {
// Acquire every instance in the pool.
engines := make([]Engine, 0, p.size)
for i := 0; i < p.size; i++ {
eng, err := p.Acquire()
if err != nil {
for _, e := range engines {
_ = e.Close()
}
return err
}
engines = append(engines, eng)
}
// Call Init on each instance.
for _, eng := range engines {
if err := eng.Init(ctx); err != nil {
for _, e := range engines {
_ = e.Close()
}
return fmt.Errorf("init failed: %w", err)
}
}
// Release them back to the pool.
for _, eng := range engines {
p.Release(eng)
}
return nil
}
////////////////////////////////////////////////////////////////////////////////
// ScriptSource injection & access
////////////////////////////////////////////////////////////////////////////////
// SetSource binds a ScriptSource on an acquired Engine.
// Note: the binding is LOCAL to that Engine instance; other engines in the pool
// are unaffected. See the package-level note above for pool-wide setup.
func (p *EnginePool) SetSource(source source.Reader) {
eng, err := p.Acquire()
if err != nil {
return
}
defer p.Release(eng)
eng.SetSource(source)
}
// GetSource returns the ScriptSource bound to an acquired Engine (or nil).
func (p *EnginePool) GetSource() source.Reader {
eng, err := p.Acquire()
if err != nil {
return nil
}
defer p.Release(eng)
return eng.GetSource()
}
////////////////////////////////////////////////////////////////////////////////
// Script loading
////////////////////////////////////////////////////////////////////////////////
// Load loads a single script from the bound Source into an acquired Engine.
func (p *EnginePool) Load(ctx context.Context, key string) error {
eng, err := p.Acquire()
if err != nil {
return err
}
defer p.Release(eng)
return eng.Load(ctx, key)
}
// LoadMulti loads multiple scripts from the bound Source into an acquired Engine.
func (p *EnginePool) LoadMulti(ctx context.Context, keys []string) error {
eng, err := p.Acquire()
if err != nil {
return err
}
defer p.Release(eng)
return eng.LoadMulti(ctx, keys)
}
// LoadString compiles an inline script (name+code) on an acquired Engine.
// It does NOT go through the bound Source.
func (p *EnginePool) LoadString(ctx context.Context, name string, code string) error {
eng, err := p.Acquire()
if err != nil {
return err
}
defer p.Release(eng)
return eng.LoadString(ctx, name, code)
}
////////////////////////////////////////////////////////////////////////////////
// Script execution
////////////////////////////////////////////////////////////////////////////////
// Execute runs every previously-loaded script on an acquired Engine and returns
// the combined result.
func (p *EnginePool) Execute(ctx context.Context) (any, error) {
eng, err := p.Acquire()
if err != nil {
return nil, err
}
defer p.Release(eng)
return eng.Execute(ctx)
}
// ExecuteFromKey loads (via the bound Source) and immediately runs the script
// identified by key on an acquired Engine.
func (p *EnginePool) ExecuteFromKey(ctx context.Context, key string) (any, error) {
eng, err := p.Acquire()
if err != nil {
return nil, err
}
defer p.Release(eng)
return eng.ExecuteFromKey(ctx, key)
}
// ExecuteFromKeys is the multi-key variant of ExecuteFromKey; results are
// returned in the same order as keys.
func (p *EnginePool) ExecuteFromKeys(ctx context.Context, keys []string) ([]any, error) {
eng, err := p.Acquire()
if err != nil {
return nil, err
}
defer p.Release(eng)
return eng.ExecuteFromKeys(ctx, keys)
}
// ExecuteString compiles and immediately runs the inline script (name+code) on
// an acquired Engine, bypassing the bound Source.
func (p *EnginePool) ExecuteString(ctx context.Context, name string, code string) (any, error) {
eng, err := p.Acquire()
if err != nil {
return nil, err
}
defer p.Release(eng)
return eng.ExecuteString(ctx, name, code)
}
////////////////////////////////////////////////////////////////////////////////
// Globals, functions, modules
////////////////////////////////////////////////////////////////////////////////
// RegisterGlobal registers a global variable on an acquired Engine.
// Note: the registration is LOCAL to that Engine instance.
func (p *EnginePool) RegisterGlobal(name string, value any) error {
eng, err := p.Acquire()
if err != nil {
return err
}
defer p.Release(eng)
return eng.RegisterGlobal(name, value)
}
// GetGlobal reads a global variable from an acquired Engine.
func (p *EnginePool) GetGlobal(name string) (any, error) {
eng, err := p.Acquire()
if err != nil {
return nil, err
}
defer p.Release(eng)
return eng.GetGlobal(name)
}
// RegisterFunction registers a host function on an acquired Engine.
// Note: the registration is LOCAL to that Engine instance.
func (p *EnginePool) RegisterFunction(name string, fn any) error {
eng, err := p.Acquire()
if err != nil {
return err
}
defer p.Release(eng)
return eng.RegisterFunction(name, fn)
}
// CallFunction invokes the named script function on an acquired Engine.
func (p *EnginePool) CallFunction(ctx context.Context, name string, args ...any) (any, error) {
eng, err := p.Acquire()
if err != nil {
return nil, err
}
defer p.Release(eng)
return eng.CallFunction(ctx, name, args...)
}
// RegisterModule registers a module on an acquired Engine.
// Note: the registration is LOCAL to that Engine instance.
func (p *EnginePool) RegisterModule(name string, module any) error {
eng, err := p.Acquire()
if err != nil {
return err
}
defer p.Release(eng)
return eng.RegisterModule(name, module)
}
////////////////////////////////////////////////////////////////////////////////
// Hot Reload (Watch)
////////////////////////////////////////////////////////////////////////////////
// StartWatch starts watching the script identified by `key` on an acquired Engine.
// Note: the watch is LOCAL to that Engine instance.
func (p *EnginePool) StartWatch(ctx context.Context, key string) error {
eng, err := p.Acquire()
if err != nil {
return err
}
defer p.Release(eng)
return eng.StartWatch(ctx, key)
}
// StopWatch stops watching the script identified by `key` on an acquired Engine.
func (p *EnginePool) StopWatch(key string) error {
eng, err := p.Acquire()
if err != nil {
return err
}
defer p.Release(eng)
return eng.StopWatch(key)
}
////////////////////////////////////////////////////////////////////////////////
// Error handling
////////////////////////////////////////////////////////////////////////////////
// GetLastError returns the last error from an acquired Engine.
func (p *EnginePool) GetLastError() error {
eng, err := p.Acquire()
if err != nil {
return err
}
defer p.Release(eng)
return eng.GetLastError()
}
// ClearError clears the last error on an acquired Engine.
func (p *EnginePool) ClearError() {
eng, err := p.Acquire()
if err != nil {
return
}
defer p.Release(eng)
eng.ClearError()
}