(mut self)
| 140 | /// Run the session loop: read frames, route by opcode, write responses. |
| 141 | #[instrument(skip(self), fields(peer = %self.peer_addr))] |
| 142 | pub async fn run(mut self) -> crate::Result<()> { |
| 143 | // Perform the version-negotiation handshake before any frame exchange. |
| 144 | let limits = self.state.limits.clone(); |
| 145 | self.proto_ver = |
| 146 | super::handshake::perform_server_handshake(&mut self.stream, &limits).await?; |
| 147 | |
| 148 | let idle_timeout_secs = self.state.idle_timeout_secs(); |
| 149 | let absolute_timeout_secs = self.state.session_absolute_timeout_secs(); |
| 150 | |
| 151 | loop { |
| 152 | // Enforce absolute session lifetime (SQLSTATE 57P01 "admin shutdown"). |
| 153 | if absolute_timeout_secs > 0 |
| 154 | && self.connected_at.elapsed().as_secs() >= absolute_timeout_secs |
| 155 | { |
| 156 | debug!( |
| 157 | "session absolute timeout ({}s), closing connection", |
| 158 | absolute_timeout_secs |
| 159 | ); |
| 160 | let shutdown_resp = NativeResponse::error( |
| 161 | 0, |
| 162 | "57P01", |
| 163 | "session timeout: absolute lifetime exceeded", |
| 164 | ); |
| 165 | if let Ok(bytes) = super::codec::encode_response( |
| 166 | &shutdown_resp, |
| 167 | self.format.unwrap_or(FrameFormat::MessagePack), |
| 168 | ) { |
| 169 | let _ = super::codec::write_frame(&mut self.stream, &bytes).await; |
| 170 | } |
| 171 | return Ok(()); |
| 172 | } |
| 173 | |
| 174 | // Read a frame with idle timeout. |
| 175 | let frame_result = if idle_timeout_secs > 0 { |
| 176 | match tokio::time::timeout( |
| 177 | Duration::from_secs(idle_timeout_secs), |
| 178 | codec::read_frame(&mut self.stream), |
| 179 | ) |
| 180 | .await |
| 181 | { |
| 182 | Ok(result) => result, |
| 183 | Err(_) => { |
| 184 | debug!("session idle timeout ({}s)", idle_timeout_secs); |
| 185 | return Ok(()); |
| 186 | } |
| 187 | } |
| 188 | } else { |
| 189 | codec::read_frame(&mut self.stream).await |
| 190 | }; |
| 191 | |
| 192 | let payload = match frame_result { |
| 193 | Ok(Some(p)) => p, |
| 194 | Ok(None) => return Ok(()), // clean EOF |
| 195 | Err(crate::Error::BadRequest { detail }) => { |
| 196 | // Send a typed error before closing so the client knows why. |
| 197 | let err_resp = |
| 198 | NativeResponse::error(0, "54000", format!("frame rejected: {detail}")); |
| 199 | let format = self.format.unwrap_or(FrameFormat::MessagePack); |
nothing calls this directly
no test coverage detected