Skip to content

[feat] - Graceful shutdown #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

MrDwarf7
Copy link

@MrDwarf7 MrDwarf7 commented Jan 7, 2025

Implementes graceful shutdown - closes #20

feat(configure/server): add graceful shutdown to server struct
Reason:
Configuration option was previously named "grace_shutdown" this was
updated to "grace_shutdown_secs" for clarity. Originally all settings
files were not updated, this has now been corrected to align with the
configuration struct in `src/configure/server.rs`
Copy link
Owner

@robatipoor robatipoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the pull request! Please get the latest update from the main branch first.

@@ -6,6 +6,7 @@ use serde::Deserialize;
pub struct ServerConfig {
pub addr: String,
pub port: u16,
pub grace_shutdown_secs: i64,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the data type to std::time::Duration:

#[serde(deserialize_with = "deserialize_duration")]
pub grace_shutdown_secs: Duration,

axum::serve(self.tcp, router).await?;
let shutdown = self.grace_shutdown_time();
let router = create_router_app(self.state)
.layer(TraceLayer::new_for_http()) // Visibility of the request and response, change as needed.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the two layers to the create_router_app function.

@@ -20,8 +23,46 @@ impl AppServer {
}

pub async fn run(self) -> AppResult<()> {
let router = create_router_app(self.state);
axum::serve(self.tcp, router).await?;
let shutdown = self.grace_shutdown_time();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove this and directly pass the timeout to the layer.

Ok(())
}

fn grace_shutdown_time(&self) -> std::time::Duration {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this function.

.send(e)
.await
.unwrap_or_else(|_| unreachable!("This channel never closed."));
let _ = sender.send(e).await;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change does not explain why to ignore the result, but the previous code does. It might be better to add a comment : "This channel never closed."

} else {
error!("A task failed: {e}.");
}
}
});
}

// Explicitly drop the sender to close the channel.
drop(sender);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Please change the comment to: "Drop the original unused sender."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feat] - Graceful shutdown - Hosting Services & clean exit
2 participants