debugstate.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package frankenphp
  2. import (
  3. "net/http"
  4. "time"
  5. )
  6. type ThreadDebugState struct {
  7. Index int
  8. Name string
  9. State string
  10. IsHandlingRequest bool
  11. Path string
  12. InRequestSinceMilliseconds int64
  13. WaitingSinceMilliseconds int64
  14. }
  15. type FrankenPHPDebugState struct {
  16. ThreadDebugStates []ThreadDebugState
  17. ReservedThreadCount int
  18. }
  19. // EXPERIMENTAL: DebugState prints the state of all PHP threads - debugging purposes only
  20. func DebugState() FrankenPHPDebugState {
  21. fullState := FrankenPHPDebugState{
  22. ThreadDebugStates: make([]ThreadDebugState, 0, len(phpThreads)),
  23. ReservedThreadCount: 0,
  24. }
  25. for _, thread := range phpThreads {
  26. if thread.state.is(stateReserved) {
  27. fullState.ReservedThreadCount++
  28. continue
  29. }
  30. fullState.ThreadDebugStates = append(fullState.ThreadDebugStates, threadDebugState(thread))
  31. }
  32. return fullState
  33. }
  34. // threadDebugState creates a small jsonable status message for debugging purposes
  35. func threadDebugState(thread *phpThread) ThreadDebugState {
  36. debugState := ThreadDebugState{
  37. Index: thread.threadIndex,
  38. Name: thread.handler.name(),
  39. State: thread.state.name(),
  40. WaitingSinceMilliseconds: thread.state.waitTime(),
  41. }
  42. var r *http.Request
  43. if r = thread.getActiveRequestSafely(); r == nil {
  44. return debugState
  45. }
  46. fc := r.Context().Value(contextKey).(*FrankenPHPContext)
  47. if fc.originalRequest != nil {
  48. debugState.Path = fc.originalRequest.URL.Path
  49. } else {
  50. debugState.Path = r.URL.Path
  51. }
  52. if fc.responseWriter != nil {
  53. debugState.IsHandlingRequest = true
  54. debugState.InRequestSinceMilliseconds = time.Since(fc.startedAt).Milliseconds()
  55. }
  56. return debugState
  57. }