-
Notifications
You must be signed in to change notification settings - Fork 23
[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
base: main
Are you sure you want to change the base?
Conversation
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`
There was a problem hiding this 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, |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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."
Implementes graceful shutdown - closes #20