-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathbackground_worker.go
More file actions
385 lines (319 loc) · 10.9 KB
/
background_worker.go
File metadata and controls
385 lines (319 loc) · 10.9 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
package frankenphp
// #include <stdint.h>
// #include "frankenphp.h"
import "C"
import (
"fmt"
"log/slog"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"
)
var backgroundScopeCounter atomic.Uint64
// NextBackgroundWorkerScope returns a unique scope ID for background worker isolation.
// Each php_server block should call this once during provisioning.
func NextBackgroundWorkerScope() string {
return fmt.Sprintf("php_server_%d", backgroundScopeCounter.Add(1))
}
// defaultMaxBackgroundWorkers is the default safety cap for catch-all background workers.
const defaultMaxBackgroundWorkers = 16
// backgroundLookups maps scope IDs to their background worker lookups.
// Each php_server block gets its own scope. The global frankenphp block
// uses the empty string as its scope ID.
var backgroundLookups map[string]*backgroundWorkerLookup
// backgroundWorkerLookup maps worker names to registries, enabling multiple entrypoint files.
type backgroundWorkerLookup struct {
byName map[string]*backgroundWorkerRegistry
catchAll *backgroundWorkerRegistry
}
func newBackgroundWorkerLookup() *backgroundWorkerLookup {
return &backgroundWorkerLookup{
byName: make(map[string]*backgroundWorkerRegistry),
}
}
func (l *backgroundWorkerLookup) AddNamed(name string, registry *backgroundWorkerRegistry) {
l.byName[name] = registry
}
func (l *backgroundWorkerLookup) SetCatchAll(registry *backgroundWorkerRegistry) {
l.catchAll = registry
}
// Resolve returns the registry for the given name, falling back to catch-all.
func (l *backgroundWorkerLookup) Resolve(name string) *backgroundWorkerRegistry {
if r, ok := l.byName[name]; ok {
return r
}
return l.catchAll
}
type backgroundWorkerRegistry struct {
entrypoint string
num int // threads per background worker (0 = lazy-start with 1 thread)
maxWorkers int // max lazy-started instances (0 = unlimited)
mu sync.Mutex
workers map[string]*backgroundWorkerState
}
func newBackgroundWorkerRegistry(entrypoint string) *backgroundWorkerRegistry {
return &backgroundWorkerRegistry{
entrypoint: entrypoint,
workers: make(map[string]*backgroundWorkerState),
}
}
func (registry *backgroundWorkerRegistry) MaxThreads() int {
if registry.num > 0 {
return registry.num
}
return 1
}
func (registry *backgroundWorkerRegistry) SetNum(num int) {
registry.num = num
}
func (registry *backgroundWorkerRegistry) SetMaxWorkers(max int) {
registry.maxWorkers = max
}
// buildBackgroundWorkerLookups constructs per-scope background worker lookups
// from worker options. Each scope (php_server block) gets its own lookup.
func buildBackgroundWorkerLookups(workers []*worker, opts []workerOpt) map[string]*backgroundWorkerLookup {
lookups := make(map[string]*backgroundWorkerLookup)
scopeRegistries := make(map[string]map[string]*backgroundWorkerRegistry)
for i, o := range opts {
if !o.isBackgroundWorker {
continue
}
scope := o.backgroundScope
lookup, ok := lookups[scope]
if !ok {
lookup = newBackgroundWorkerLookup()
lookups[scope] = lookup
scopeRegistries[scope] = make(map[string]*backgroundWorkerRegistry)
}
entrypoint := o.fileName
registry, ok := scopeRegistries[scope][entrypoint]
if !ok {
registry = newBackgroundWorkerRegistry(entrypoint)
scopeRegistries[scope][entrypoint] = registry
}
workers[i].backgroundScope = scope
w := workers[i]
phpName := strings.TrimPrefix(w.name, "m#")
if phpName != "" && phpName != w.fileName {
// Named background worker
if o.num > 0 {
registry.SetNum(o.num)
}
lookup.AddNamed(phpName, registry)
} else {
// Catch-all background worker; maxThreads > 1 means user set it explicitly
maxW := defaultMaxBackgroundWorkers
if o.maxThreads > 1 {
maxW = o.maxThreads
}
registry.SetMaxWorkers(maxW)
lookup.SetCatchAll(registry)
}
w.backgroundRegistry = registry
}
if len(lookups) == 0 {
return nil
}
return lookups
}
func (registry *backgroundWorkerRegistry) reserve(name string) (*backgroundWorkerState, bool, error) {
registry.mu.Lock()
defer registry.mu.Unlock()
if bgw := registry.workers[name]; bgw != nil {
return bgw, true, nil
}
if registry.maxWorkers > 0 && len(registry.workers) >= registry.maxWorkers {
return nil, false, fmt.Errorf("cannot start background worker %q: limit of %d reached - increase max_threads on the catch-all background worker or declare it as a named worker", name, registry.maxWorkers)
}
bgw := &backgroundWorkerState{
ready: make(chan struct{}),
}
registry.workers[name] = bgw
return bgw, false, nil
}
func (registry *backgroundWorkerRegistry) remove(name string, bgw *backgroundWorkerState) {
registry.mu.Lock()
defer registry.mu.Unlock()
if registry.workers[name] == bgw {
delete(registry.workers, name)
}
}
func startBackgroundWorker(thread *phpThread, bgWorkerName string) error {
if bgWorkerName == "" {
return fmt.Errorf("background worker name must not be empty")
}
lookup := getLookup(thread)
if lookup == nil {
return fmt.Errorf("no background worker configured in this php_server")
}
registry := lookup.Resolve(bgWorkerName)
if registry == nil || registry.entrypoint == "" {
return fmt.Errorf("no background worker configured in this php_server")
}
return startBackgroundWorkerWithRegistry(registry, bgWorkerName)
}
func startBackgroundWorkerWithRegistry(registry *backgroundWorkerRegistry, bgWorkerName string) error {
bgw, exists, err := registry.reserve(bgWorkerName)
if err != nil {
return err
}
if exists {
return nil
}
numThreads := registry.MaxThreads()
worker, err := newWorker(workerOpt{
name: bgWorkerName,
fileName: registry.entrypoint,
num: numThreads,
isBackgroundWorker: true,
env: PrepareEnv(nil),
watch: []string{},
maxConsecutiveFailures: -1,
})
if err != nil {
registry.remove(bgWorkerName, bgw)
return fmt.Errorf("failed to create background worker: %w", err)
}
worker.isBackgroundWorker = true
worker.backgroundWorker = bgw
worker.backgroundRegistry = registry
for i := 0; i < numThreads; i++ {
bgWorkerThread := getInactivePHPThread()
if bgWorkerThread == nil {
if i == 0 {
registry.remove(bgWorkerName, bgw)
}
return fmt.Errorf("no available PHP thread for background worker (increase max_threads)")
}
scalingMu.Lock()
workers = append(workers, worker)
scalingMu.Unlock()
convertToBackgroundWorkerThread(bgWorkerThread, worker)
}
if globalLogger.Enabled(globalCtx, slog.LevelInfo) {
globalLogger.LogAttrs(globalCtx, slog.LevelInfo, "background worker started", slog.String("name", bgWorkerName), slog.Int("threads", numThreads))
}
return nil
}
func getLookup(thread *phpThread) *backgroundWorkerLookup {
if handler, ok := thread.handler.(*workerThread); ok && handler.worker.backgroundLookup != nil {
return handler.worker.backgroundLookup
}
if handler, ok := thread.handler.(*backgroundWorkerThread); ok && handler.worker.backgroundLookup != nil {
return handler.worker.backgroundLookup
}
// Non-worker requests: resolve scope from context
if fc, ok := fromContext(thread.context()); ok && fc.backgroundScope != "" {
if backgroundLookups != nil {
return backgroundLookups[fc.backgroundScope]
}
}
// Fall back to global scope
if backgroundLookups != nil {
return backgroundLookups[""]
}
return nil
}
// go_frankenphp_get_worker_vars starts background workers if needed, waits for them
// to be ready, takes read locks, copies vars via C helper, and releases locks.
// All locking/unlocking happens within this single Go call.
//
// callerVersions/outVersions: if callerVersions is non-nil and all versions match,
// the copy is skipped entirely (returns 1). outVersions receives current versions.
//
//export go_frankenphp_get_worker_vars
func go_frankenphp_get_worker_vars(threadIndex C.uintptr_t, names **C.char, nameLens *C.size_t, nameCount C.int, timeoutMs C.int, returnValue *C.zval, callerVersions *C.uint64_t, outVersions *C.uint64_t) *C.char {
thread := phpThreads[threadIndex]
lookup := getLookup(thread)
if lookup == nil {
return C.CString("no background worker configured in this php_server")
}
n := int(nameCount)
nameSlice := unsafe.Slice(names, n)
nameLenSlice := unsafe.Slice(nameLens, n)
sks := make([]*backgroundWorkerState, n)
goNames := make([]string, n)
for i := 0; i < n; i++ {
goNames[i] = C.GoStringN(nameSlice[i], C.int(nameLenSlice[i]))
// Start background worker if not already running
if err := startBackgroundWorker(thread, goNames[i]); err != nil {
return C.CString(err.Error())
}
registry := lookup.Resolve(goNames[i])
if registry == nil {
return C.CString("background worker not found: " + goNames[i])
}
registry.mu.Lock()
sks[i] = registry.workers[goNames[i]]
registry.mu.Unlock()
if sks[i] == nil {
return C.CString("background worker not found: " + goNames[i])
}
}
// Wait for all workers to be ready (shared deadline across all workers)
deadline := time.After(time.Duration(timeoutMs) * time.Millisecond)
for i, sk := range sks {
select {
case <-sk.ready:
// background worker has called set_worker_vars
case <-deadline:
return C.CString(fmt.Sprintf("timeout waiting for background worker: %s", goNames[i]))
}
}
// Fast path: if all caller versions match, skip lock + copy entirely.
// Read each version once and write to outVersions for the C side to compare.
if callerVersions != nil && outVersions != nil {
callerVSlice := unsafe.Slice(callerVersions, n)
outVSlice := unsafe.Slice(outVersions, n)
allMatch := true
for i, sk := range sks {
v := sk.varsVersion.Load()
outVSlice[i] = C.uint64_t(v)
if uint64(callerVSlice[i]) != v {
allMatch = false
}
}
if allMatch {
return nil // C side sees out == caller, uses cached value
}
}
// Take all read locks, collect pointers, copy via C helper, then release
ptrs := make([]unsafe.Pointer, n)
for i, sk := range sks {
sk.mu.RLock()
ptrs[i] = sk.varsPtr
}
C.frankenphp_worker_copy_vars(returnValue, C.int(n), names, nameLens, (*unsafe.Pointer)(unsafe.Pointer(&ptrs[0])))
// Write versions while locks are still held
if outVersions != nil {
outVSlice := unsafe.Slice(outVersions, n)
for i, sk := range sks {
outVSlice[i] = C.uint64_t(sk.varsVersion.Load())
}
}
for _, sk := range sks {
sk.mu.RUnlock()
}
return nil
}
//export go_frankenphp_set_worker_vars
func go_frankenphp_set_worker_vars(threadIndex C.uintptr_t, varsPtr unsafe.Pointer, oldPtr *unsafe.Pointer) *C.char {
thread := phpThreads[threadIndex]
bgHandler, ok := thread.handler.(*backgroundWorkerThread)
if !ok || bgHandler.worker.backgroundWorker == nil {
return C.CString("frankenphp_set_worker_vars() can only be called from a background worker")
}
sk := bgHandler.worker.backgroundWorker
sk.mu.Lock()
*oldPtr = sk.varsPtr
sk.varsPtr = varsPtr
sk.varsVersion.Add(1)
sk.mu.Unlock()
sk.readyOnce.Do(func() {
bgHandler.markBackgroundReady()
close(sk.ready)
})
return nil
}