Update the backpressure state based on current queue utilization. Returns the new state if a transition occurred, or `None` if unchanged. The caller SHOULD emit a tracing event on transitions.
(&self, utilization_percent: u8)
| 99 | /// Returns the new state if a transition occurred, or `None` if unchanged. |
| 100 | /// The caller SHOULD emit a tracing event on transitions. |
| 101 | pub fn update(&self, utilization_percent: u8) -> Option<PressureState> { |
| 102 | let current = PressureState::from_u8(self.state.load(Ordering::Relaxed)); |
| 103 | |
| 104 | let new_state = match current { |
| 105 | PressureState::Normal => { |
| 106 | if utilization_percent >= self.config.suspend_enter { |
| 107 | PressureState::Suspended |
| 108 | } else if utilization_percent >= self.config.throttle_enter { |
| 109 | PressureState::Throttled |
| 110 | } else { |
| 111 | return None; |
| 112 | } |
| 113 | } |
| 114 | PressureState::Throttled => { |
| 115 | if utilization_percent >= self.config.suspend_enter { |
| 116 | PressureState::Suspended |
| 117 | } else if utilization_percent < self.config.throttle_exit { |
| 118 | PressureState::Normal |
| 119 | } else { |
| 120 | return None; |
| 121 | } |
| 122 | } |
| 123 | PressureState::Suspended => { |
| 124 | if utilization_percent < self.config.suspend_exit { |
| 125 | if utilization_percent < self.config.throttle_exit { |
| 126 | PressureState::Normal |
| 127 | } else { |
| 128 | PressureState::Throttled |
| 129 | } |
| 130 | } else { |
| 131 | return None; |
| 132 | } |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | self.state.store(new_state as u8, Ordering::Release); |
| 137 | |
| 138 | match new_state { |
| 139 | PressureState::Throttled => { |
| 140 | self.throttle_count.fetch_add(1, Ordering::Relaxed); |
| 141 | } |
| 142 | PressureState::Suspended => { |
| 143 | self.suspend_count.fetch_add(1, Ordering::Relaxed); |
| 144 | } |
| 145 | PressureState::Normal => {} |
| 146 | } |
| 147 | |
| 148 | Some(new_state) |
| 149 | } |
| 150 | |
| 151 | /// Current backpressure state. |
| 152 | pub fn state(&self) -> PressureState { |