state.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package frankenphp
  2. import (
  3. "slices"
  4. "sync"
  5. "time"
  6. )
  7. type stateID uint8
  8. const (
  9. // livecycle states of a thread
  10. stateReserved stateID = iota
  11. stateBooting
  12. stateShuttingDown
  13. stateDone
  14. // these states are 'stable' and safe to transition from at any time
  15. stateInactive
  16. stateReady
  17. // states necessary for restarting workers
  18. stateRestarting
  19. stateYielding
  20. // states necessary for transitioning between different handlers
  21. stateTransitionRequested
  22. stateTransitionInProgress
  23. stateTransitionComplete
  24. )
  25. var stateNames = map[stateID]string{
  26. stateReserved: "reserved",
  27. stateBooting: "booting",
  28. stateInactive: "inactive",
  29. stateReady: "ready",
  30. stateShuttingDown: "shutting down",
  31. stateDone: "done",
  32. stateRestarting: "restarting",
  33. stateYielding: "yielding",
  34. stateTransitionRequested: "transition requested",
  35. stateTransitionInProgress: "transition in progress",
  36. stateTransitionComplete: "transition complete",
  37. }
  38. type threadState struct {
  39. currentState stateID
  40. mu sync.RWMutex
  41. subscribers []stateSubscriber
  42. // how long threads have been waiting in stable states
  43. waitingSince time.Time
  44. isWaiting bool
  45. }
  46. type stateSubscriber struct {
  47. states []stateID
  48. ch chan struct{}
  49. }
  50. func newThreadState() *threadState {
  51. return &threadState{
  52. currentState: stateReserved,
  53. subscribers: []stateSubscriber{},
  54. mu: sync.RWMutex{},
  55. }
  56. }
  57. func (ts *threadState) is(state stateID) bool {
  58. ts.mu.RLock()
  59. ok := ts.currentState == state
  60. ts.mu.RUnlock()
  61. return ok
  62. }
  63. func (ts *threadState) compareAndSwap(compareTo stateID, swapTo stateID) bool {
  64. ts.mu.Lock()
  65. ok := ts.currentState == compareTo
  66. if ok {
  67. ts.currentState = swapTo
  68. ts.notifySubscribers(swapTo)
  69. }
  70. ts.mu.Unlock()
  71. return ok
  72. }
  73. func (ts *threadState) name() string {
  74. return stateNames[ts.get()]
  75. }
  76. func (ts *threadState) get() stateID {
  77. ts.mu.RLock()
  78. id := ts.currentState
  79. ts.mu.RUnlock()
  80. return id
  81. }
  82. func (ts *threadState) set(nextState stateID) {
  83. ts.mu.Lock()
  84. ts.currentState = nextState
  85. ts.notifySubscribers(nextState)
  86. ts.mu.Unlock()
  87. }
  88. func (ts *threadState) notifySubscribers(nextState stateID) {
  89. if len(ts.subscribers) == 0 {
  90. return
  91. }
  92. newSubscribers := []stateSubscriber{}
  93. // notify subscribers to the state change
  94. for _, sub := range ts.subscribers {
  95. if !slices.Contains(sub.states, nextState) {
  96. newSubscribers = append(newSubscribers, sub)
  97. continue
  98. }
  99. close(sub.ch)
  100. }
  101. ts.subscribers = newSubscribers
  102. }
  103. // block until the thread reaches a certain state
  104. func (ts *threadState) waitFor(states ...stateID) {
  105. ts.mu.Lock()
  106. if slices.Contains(states, ts.currentState) {
  107. ts.mu.Unlock()
  108. return
  109. }
  110. sub := stateSubscriber{
  111. states: states,
  112. ch: make(chan struct{}),
  113. }
  114. ts.subscribers = append(ts.subscribers, sub)
  115. ts.mu.Unlock()
  116. <-sub.ch
  117. }
  118. // safely request a state change from a different goroutine
  119. func (ts *threadState) requestSafeStateChange(nextState stateID) bool {
  120. ts.mu.Lock()
  121. switch ts.currentState {
  122. // disallow state changes if shutting down or done
  123. case stateShuttingDown, stateDone, stateReserved:
  124. ts.mu.Unlock()
  125. return false
  126. // ready and inactive are safe states to transition from
  127. case stateReady, stateInactive:
  128. ts.currentState = nextState
  129. ts.notifySubscribers(nextState)
  130. ts.mu.Unlock()
  131. return true
  132. }
  133. ts.mu.Unlock()
  134. // wait for the state to change to a safe state
  135. ts.waitFor(stateReady, stateInactive, stateShuttingDown)
  136. return ts.requestSafeStateChange(nextState)
  137. }
  138. // the thread reached a stable state and is waiting for requests or shutdown
  139. func (ts *threadState) markAsWaiting(isWaiting bool) {
  140. ts.mu.Lock()
  141. if isWaiting {
  142. ts.isWaiting = true
  143. ts.waitingSince = time.Now()
  144. } else {
  145. ts.isWaiting = false
  146. }
  147. ts.mu.Unlock()
  148. }
  149. // the time since the thread is waiting in a stable state in ms
  150. func (ts *threadState) waitTime() int64 {
  151. ts.mu.RLock()
  152. waitTime := int64(0)
  153. if ts.isWaiting {
  154. waitTime = time.Now().UnixMilli() - ts.waitingSince.UnixMilli()
  155. }
  156. ts.mu.RUnlock()
  157. return waitTime
  158. }