thread-inactive.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package frankenphp
  2. import (
  3. "net/http"
  4. )
  5. // representation of a thread with no work assigned to it
  6. // implements the threadHandler interface
  7. // each inactive thread weighs around ~350KB
  8. // keeping threads at 'inactive' will consume more memory, but allow a faster transition
  9. type inactiveThread struct {
  10. thread *phpThread
  11. }
  12. func convertToInactiveThread(thread *phpThread) {
  13. thread.setHandler(&inactiveThread{thread: thread})
  14. }
  15. func (handler *inactiveThread) beforeScriptExecution() string {
  16. thread := handler.thread
  17. switch thread.state.get() {
  18. case stateTransitionRequested:
  19. return thread.transitionToNewHandler()
  20. case stateBooting, stateTransitionComplete:
  21. thread.state.set(stateInactive)
  22. // wait for external signal to start or shut down
  23. thread.state.markAsWaiting(true)
  24. thread.state.waitFor(stateTransitionRequested, stateShuttingDown)
  25. thread.state.markAsWaiting(false)
  26. return handler.beforeScriptExecution()
  27. case stateShuttingDown:
  28. // signal to stop
  29. return ""
  30. }
  31. panic("unexpected state: " + thread.state.name())
  32. }
  33. func (handler *inactiveThread) afterScriptExecution(exitStatus int) {
  34. panic("inactive threads should not execute scripts")
  35. }
  36. func (handler *inactiveThread) getActiveRequest() *http.Request {
  37. panic("inactive threads have no requests")
  38. }
  39. func (handler *inactiveThread) name() string {
  40. return "Inactive PHP Thread"
  41. }