-
Notifications
You must be signed in to change notification settings - Fork 43
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
Add automatic shutdown after inactivity #1590
Open
mgjm
wants to merge
1
commit into
containers:main
Choose a base branch
from
mgjm-fork:shutdown-delay
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,128 @@ | ||||||
use std::{ | ||||||
future, process, | ||||||
sync::{ | ||||||
atomic::{AtomicUsize, Ordering}, | ||||||
Arc, | ||||||
}, | ||||||
time::Duration, | ||||||
}; | ||||||
|
||||||
use tokio::sync::{futures::Notified, Notify}; | ||||||
|
||||||
/// Track activity and reacto to inactivity. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// | ||||||
/// Can be used to exit accept loops after inactivity. | ||||||
#[derive(Debug, Clone)] | ||||||
pub struct Inactivity(Option<Arc<Inner>>); | ||||||
|
||||||
impl Inactivity { | ||||||
/// Create a new inactivity tracker. | ||||||
pub fn new() -> Self { | ||||||
Self(Some(Arc::default())) | ||||||
} | ||||||
|
||||||
/// Create a disabled inactivity tracker. | ||||||
/// | ||||||
/// The wait function will never return. There is always activity. | ||||||
pub const fn disabled() -> Self { | ||||||
Self(None) | ||||||
} | ||||||
|
||||||
/// Start tracking an activity. | ||||||
pub fn activity(&self) -> Activity { | ||||||
Activity::new(self.0.as_ref()) | ||||||
} | ||||||
|
||||||
/// Async "block" until there is no activity and then wait for an additional`timeout`. | ||||||
pub async fn wait(&self, timeout: Duration) { | ||||||
if let Some(inner) = &self.0 { | ||||||
loop { | ||||||
let changed = inner.changed(); | ||||||
if inner.no_activity() { | ||||||
let _ = tokio::time::timeout(timeout, changed).await; | ||||||
if inner.no_activity() { | ||||||
break; | ||||||
} | ||||||
} else { | ||||||
changed.await | ||||||
} | ||||||
} | ||||||
} else { | ||||||
future::pending().await | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
/// Track an activity. Can be cloned to track additional activities. | ||||||
/// | ||||||
/// The Activity stops on drop. | ||||||
#[derive(Debug)] | ||||||
pub struct Activity(Option<Arc<Inner>>); | ||||||
|
||||||
impl Activity { | ||||||
fn new(inner: Option<&Arc<Inner>>) -> Self { | ||||||
Self(inner.map(Inner::increment)) | ||||||
} | ||||||
|
||||||
/// Explicitly stop an activity. Just a wrapper for `drop(activity)`. | ||||||
pub fn stop(self) { | ||||||
// nothing to to, we take self by value | ||||||
} | ||||||
} | ||||||
|
||||||
impl Clone for Activity { | ||||||
fn clone(&self) -> Self { | ||||||
Self::new(self.0.as_ref()) | ||||||
} | ||||||
} | ||||||
|
||||||
impl Drop for Activity { | ||||||
fn drop(&mut self) { | ||||||
if let Some(inner) = &self.0 { | ||||||
inner.decrement(); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
#[derive(Debug, Default)] | ||||||
struct Inner { | ||||||
// number of current activities | ||||||
active: AtomicUsize, | ||||||
// gets notofied whenever the result of `no_activity` changes | ||||||
notify: Notify, | ||||||
} | ||||||
|
||||||
impl Inner { | ||||||
/// Abort if more then isize::MAX activities are active. | ||||||
/// | ||||||
/// This prevents integer overflow of the `active` counter in case | ||||||
/// someone is `mem::forget`ing activities. | ||||||
/// | ||||||
/// The same logic is applied internally by the `Arc` implementation. | ||||||
const MAX_ACTIVE: usize = isize::MAX as usize; | ||||||
|
||||||
fn increment(self: &Arc<Self>) -> Arc<Self> { | ||||||
match self.active.fetch_add(1, Ordering::Relaxed) { | ||||||
0 => self.notify.notify_waiters(), | ||||||
1..=Self::MAX_ACTIVE => {} | ||||||
_ => process::abort(), | ||||||
} | ||||||
self.clone() | ||||||
} | ||||||
|
||||||
fn decrement(&self) { | ||||||
match self.active.fetch_sub(1, Ordering::Relaxed) { | ||||||
1 => self.notify.notify_waiters(), | ||||||
2..=Self::MAX_ACTIVE => {} | ||||||
_ => process::abort(), | ||||||
} | ||||||
} | ||||||
|
||||||
fn no_activity(&self) -> bool { | ||||||
self.active.load(Ordering::Relaxed) == 0 | ||||||
} | ||||||
|
||||||
fn changed(&self) -> Notified { | ||||||
self.notify.notified() | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 we should make this something like Option<
humantime::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.
Hmm... the string representation of a go
time.Duration
is not compatible with thehumantime::Duration
parser. But we could write a custom "to string" method in go. Do we expect the conmon server to be invoked by humans or only by the go client?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.
IMO we should only support go client
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.
And even for humans it is still relatively ergonomic to specify the duration in (fractional) seconds.
1.5
vs1s 500ms
Especially considering that I expect the most common durations to be in the range of a few full seconds.
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.
Is specifying milliseconds really a use case? I'm also fine with using seconds only.