Skip to content

Commit a1334b6

Browse files
refactor: add drain() seam to threadHandler interface (#2367)
Third step of the split suggested in #2287: land the handler-interface extension point that later handler types (background workers) need, without introducing any new behaviour. Each handler gains a `drain()` method, called by `drainWorkerThreads` right before `drainChan` is closed. All current implementations (`regularThread`, `workerThread`, `inactiveThread`, `taskThread`) are no-ops, so observable behaviour is unchanged. A later handler that needs to wake up a thread parked in a blocking C call (e.g. by closing a stop pipe) plugs its signal in here without modifying `drainWorkerThreads` again. ## What - `phpthread.go` — interface gains `drain()`. - `threadregular.go` / `threadworker.go` / `threadinactive.go` / `threadtasks_test.go` — empty `drain()` on each handler. - `worker.go` — `drainWorkerThreads` calls `thread.handler.drain()` right before `close(thread.drainChan)`. Net change: +15 / -0.
1 parent 09bf047 commit a1334b6

6 files changed

Lines changed: 15 additions & 0 deletions

File tree

phpthread.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ type threadHandler interface {
3434
afterScriptExecution(exitStatus int)
3535
context() context.Context
3636
frankenPHPContext() *frankenPHPContext
37+
// drain is a hook called by drainWorkerThreads right before drainChan is
38+
// closed. Handlers that need to wake up a thread parked in a blocking C
39+
// call (e.g. by closing a stop pipe) plug their signal in here. All
40+
// current handlers are no-ops; this is the seam later handler types use
41+
// without having to modify drainWorkerThreads.
42+
drain()
3743
}
3844

3945
func newPHPThread(threadIndex int) *phpThread {

threadinactive.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ func (handler *inactiveThread) context() context.Context {
5858
func (handler *inactiveThread) name() string {
5959
return "Inactive PHP Thread"
6060
}
61+
62+
func (handler *inactiveThread) drain() {}

threadregular.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func (handler *regularThread) name() string {
8383
return "Regular PHP Thread"
8484
}
8585

86+
func (handler *regularThread) drain() {}
87+
8688
func (handler *regularThread) waitForRequest() string {
8789
// max_requests reached: restart the thread to clean up all ZTS state
8890
if maxRequestsPerThread > 0 && handler.requestCount >= maxRequestsPerThread {

threadtasks_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ func (handler *taskThread) name() string {
7979
return "Task PHP Thread"
8080
}
8181

82+
func (handler *taskThread) drain() {}
83+
8284
func (handler *taskThread) waitForTasks() {
8385
for {
8486
select {

threadworker.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ func (handler *workerThread) name() string {
105105
return "Worker PHP Thread - " + handler.worker.fileName
106106
}
107107

108+
func (handler *workerThread) drain() {}
109+
108110
func setupWorkerScript(handler *workerThread, worker *worker) {
109111
metrics.StartWorker(worker.name)
110112

worker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func drainWorkerThreads() []*phpThread {
189189
continue
190190
}
191191

192+
thread.handler.drain()
192193
close(thread.drainChan)
193194
drainedThreads = append(drainedThreads, thread)
194195

0 commit comments

Comments
 (0)