-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy paththread-regular.go
78 lines (67 loc) · 2.03 KB
/
thread-regular.go
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
package frankenphp
// #include "frankenphp.h"
import "C"
import (
"net/http"
)
// representation of a non-worker PHP thread
// executes PHP scripts in a web context
// implements the threadHandler interface
type regularThread struct {
state *threadState
thread *phpThread
activeRequest *http.Request
}
func convertToRegularThread(thread *phpThread) {
thread.setHandler(®ularThread{
thread: thread,
state: thread.state,
})
}
// beforeScriptExecution returns the name of the script or an empty string on shutdown
func (handler *regularThread) beforeScriptExecution() string {
switch handler.state.get() {
case stateTransitionRequested:
return handler.thread.transitionToNewHandler()
case stateTransitionComplete:
handler.state.set(stateReady)
return handler.waitForRequest()
case stateReady:
return handler.waitForRequest()
case stateShuttingDown:
// signal to stop
return ""
}
panic("unexpected state: " + handler.state.name())
}
// return true if the worker should continue to run
func (handler *regularThread) afterScriptExecution(exitStatus int) {
handler.afterRequest(exitStatus)
}
func (handler *regularThread) getActiveRequest() *http.Request {
return handler.activeRequest
}
func (handler *regularThread) waitForRequest() string {
select {
case <-handler.thread.drainChan:
// go back to beforeScriptExecution
return handler.beforeScriptExecution()
case r := <-requestChan:
handler.activeRequest = r
fc := r.Context().Value(contextKey).(*FrankenPHPContext)
if err := updateServerContext(handler.thread, r, true, false); err != nil {
rejectRequest(fc.responseWriter, err.Error())
handler.afterRequest(0)
// go back to beforeScriptExecution
return handler.beforeScriptExecution()
}
// set the scriptFilename that should be executed
return fc.scriptFilename
}
}
func (handler *regularThread) afterRequest(exitStatus int) {
fc := handler.activeRequest.Context().Value(contextKey).(*FrankenPHPContext)
fc.exitStatus = exitStatus
maybeCloseContext(fc)
handler.activeRequest = nil
}