MCPcopy Create free account
hub / github.com/DefGuard/gateway / start

Method start

src/setup.rs:295–355  ·  view source on GitHub ↗
(&self, request: Request<()>)

Source from the content-addressed store, hash-verified

293
294 #[instrument(skip(self, request))]
295 async fn start(&self, request: Request<()>) -> Result<Response<Self::StartStream>, Status> {
296 debug!("Core initiated setup process, preparing to stream logs");
297 if self.adoption_expired.load(Ordering::Relaxed) {
298 let error_message = "Gateway adoption expired and is now blocked. Restart the Gateway to enable adoption.";
299 error!("{error_message}");
300 return Err(Status::failed_precondition(error_message));
301 }
302 if self.is_setup_in_progress() {
303 error!("Setup already in progress, rejecting new setup request");
304 return Err(Status::resource_exhausted("Setup already in progress"));
305 }
306
307 debug!("Authenticating setup session with Core");
308 let token = request
309 .metadata()
310 .get(AUTH_HEADER)
311 .and_then(|v| v.to_str().ok())
312 .and_then(|s| s.strip_prefix("Bearer "))
313 .ok_or_else(|| Status::unauthenticated("Missing or invalid authorization token"))?;
314
315 debug!("Setup session authenticated successfully");
316 self.initialize_setup_session(token.to_string());
317
318 debug!("Preparing to forward Gateway logs to Core in real-time");
319 let logs_rx = self.logs_rx.clone();
320
321 let (tx, rx) = mpsc::unbounded_channel();
322 let self_clone = self.clone();
323
324 debug!("Starting log streaming to Core");
325 tokio::spawn(async move {
326 loop {
327 let maybe_log_entry = logs_rx.lock().await.try_recv();
328 match maybe_log_entry {
329 Ok(log_entry) => {
330 if tx.send(Ok(log_entry)).is_err() {
331 debug!(
332 "Failed to send log entry to gRPC stream: receiver disconnected"
333 );
334 break;
335 }
336 }
337 Err(tokio::sync::mpsc::error::TryRecvError::Empty) => {
338 if tx.is_closed() {
339 debug!("gRPC stream receiver disconnected");
340 break;
341 }
342 tokio::task::yield_now().await;
343 }
344 Err(tokio::sync::mpsc::error::TryRecvError::Disconnected) => {
345 debug!("Logs receiver disconnected");
346 break;
347 }
348 }
349 }
350 self_clone.clear_setup_session();
351 });
352

Callers

nothing calls this directly

Calls 4

is_setup_in_progressMethod · 0.80
cloneMethod · 0.80
clear_setup_sessionMethod · 0.80

Tested by

no test coverage detected