-
Notifications
You must be signed in to change notification settings - Fork 814
Make Non-blocking shutdown timeout configurable #3231
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
Amberley-Sz
wants to merge
4
commits into
tokio-rs:v0.1.x
Choose a base branch
from
Amberley-Sz:v0.1.x
base: v0.1.x
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
374494c
made shutdown timeout configurable, included integration tests and un…
Amberley-Sz 144982d
added in lib file to pass clippy checks
Amberley-Sz d83c01c
fixing clippy checks on overindentation and on `.next_back()` replaci…
Amberley-Sz 7272fea
updated the broken doc links
Amberley-Sz 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 hidden or 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 hidden or 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,62 @@ | ||
use std::{ | ||
io::{self, Write}, | ||
sync::atomic::{AtomicBool, AtomicU64, Ordering}, | ||
thread, | ||
time::{Duration, Instant}, | ||
}; | ||
use tracing_appender::non_blocking::NonBlockingBuilder; | ||
|
||
static BLOCK_IN_WORKER: AtomicBool = AtomicBool::new(false); | ||
static BLOCK_DURATION_SECS: AtomicU64 = AtomicU64::new(3); | ||
|
||
struct BlockingMemoryWriter { | ||
buffer: Vec<u8>, | ||
} | ||
|
||
impl BlockingMemoryWriter { | ||
fn new() -> Self { | ||
Self { buffer: Vec::new() } | ||
} | ||
} | ||
|
||
impl Write for BlockingMemoryWriter { | ||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
if BLOCK_IN_WORKER.load(Ordering::Relaxed) { | ||
let block_secs = BLOCK_DURATION_SECS.load(Ordering::Relaxed); | ||
thread::sleep(Duration::from_secs(block_secs)); | ||
} | ||
self.buffer.extend_from_slice(buf); | ||
Ok(buf.len()) | ||
} | ||
|
||
fn flush(&mut self) -> io::Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_shutdown_timeout_behavior() { | ||
let timeout = Duration::from_millis(300); | ||
let blocking_writer = BlockingMemoryWriter::new(); | ||
|
||
let (mut non_blocking, guard) = NonBlockingBuilder::default() | ||
.shutdown_timeout(timeout) | ||
.finish(blocking_writer); | ||
|
||
non_blocking.write_all(b"test data\n").unwrap(); | ||
|
||
thread::sleep(Duration::from_millis(50)); | ||
BLOCK_IN_WORKER.store(true, Ordering::Relaxed); | ||
non_blocking.write_all(b"blocking data\n").unwrap(); | ||
|
||
let start = Instant::now(); | ||
drop(guard); | ||
let elapsed = start.elapsed(); | ||
|
||
assert!( | ||
elapsed >= timeout, | ||
"Shutdown completed before timeout: {:?}, expected at least {:?}", | ||
elapsed, | ||
timeout | ||
); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
These links don't work, we should probably change this to:
Thanks!
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.
Changing to this would cause clippy warnings saying that link definitions are not shown in rendered documentation. Would changing to
be a better idea?
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.
Umm that's weird I don't get any clippy warnings. Which rust version do you use? Anyways I'd avoid your example because it doesn't actually link to the levels.
Uh oh!
There was an error while loading. Please reload this page.
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'm using
rustc 1.86.0
When I run
cargo clippy
it showsThere 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.
You didn't add my suggestion though.
Add:
instead of the existing doc-comment.
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 see the point now! Thanks for the suggestion, updated!