-
Notifications
You must be signed in to change notification settings - Fork 88
Implement unsolicited message handling while IDLEing #130
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -4,9 +4,11 @@ | |
use client::Session; | ||
use error::{Error, Result}; | ||
use native_tls::TlsStream; | ||
use parse; | ||
use std::io::{self, Read, Write}; | ||
use std::net::TcpStream; | ||
use std::time::Duration; | ||
use unsolicited_responses::UnsolicitedResponseSender; | ||
|
||
/// `Handle` allows a client to block waiting for changes to the remote mailbox. | ||
/// | ||
|
@@ -26,7 +28,8 @@ use std::time::Duration; | |
#[derive(Debug)] | ||
pub struct Handle<'a, T: Read + Write + 'a> { | ||
session: &'a mut Session<T>, | ||
keepalive: Duration, | ||
unsolicited_responses_tx: UnsolicitedResponseSender, | ||
keepalive: Option<Duration>, | ||
done: bool, | ||
} | ||
|
||
|
@@ -45,10 +48,14 @@ pub trait SetReadTimeout { | |
} | ||
|
||
impl<'a, T: Read + Write + 'a> Handle<'a, T> { | ||
pub(crate) fn make(session: &'a mut Session<T>) -> Result<Self> { | ||
pub(crate) fn make( | ||
session: &'a mut Session<T>, | ||
unsolicited_responses_tx: UnsolicitedResponseSender, | ||
) -> Result<Self> { | ||
let mut h = Handle { | ||
session, | ||
keepalive: Duration::from_secs(29 * 60), | ||
unsolicited_responses_tx, | ||
keepalive: None, | ||
done: false, | ||
}; | ||
h.init()?; | ||
|
@@ -77,31 +84,53 @@ impl<'a, T: Read + Write + 'a> Handle<'a, T> { | |
unreachable!(); | ||
} | ||
|
||
fn terminate(&mut self) -> Result<()> { | ||
fn terminate(&mut self, consume_response: bool) -> Result<()> { | ||
if !self.done { | ||
self.done = true; | ||
self.session.write_line(b"DONE")?; | ||
self.session.read_response().map(|_| ()) | ||
} else { | ||
Ok(()) | ||
if consume_response { | ||
return self.session.read_response().map(|_| ()); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Internal helper that doesn't consume self. | ||
/// | ||
/// This is necessary so that we can keep using the inner `Session` in `wait_keepalive`. | ||
fn wait_inner(&mut self) -> Result<()> { | ||
let mut v = Vec::new(); | ||
match self.session.readline(&mut v).map(|_| ()) { | ||
Err(Error::Io(ref e)) | ||
if e.kind() == io::ErrorKind::TimedOut || e.kind() == io::ErrorKind::WouldBlock => | ||
{ | ||
// we need to refresh the IDLE connection | ||
self.terminate()?; | ||
self.init()?; | ||
self.wait_inner() | ||
let mut buffer = Vec::new(); | ||
loop { | ||
match self.session.readline(&mut buffer).map(|_| ()) { | ||
Err(Error::Io(ref e)) | ||
if e.kind() == io::ErrorKind::TimedOut | ||
|| e.kind() == io::ErrorKind::WouldBlock => | ||
{ | ||
if self.keepalive.is_some() { | ||
// we need to refresh the IDLE connection | ||
self.terminate(true)?; | ||
self.init()?; | ||
} | ||
} | ||
Err(err) => return Err(err), | ||
Ok(_) => { | ||
let _ = parse::parse_idle(&buffer, &mut self.unsolicited_responses_tx)?; | ||
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. Specifically, shouldn't this use the return value from |
||
self.terminate(false)?; | ||
buffer.truncate(0); | ||
|
||
// Unsolicited responses coming in from the server are not multi-line, | ||
// therefore, we don't need to worry about the remaining bytes since | ||
// they should always be terminated at a LF. | ||
loop { | ||
let _ = self.session.readline(&mut buffer)?; | ||
let found_ok = | ||
parse::parse_idle(&buffer, &mut self.unsolicited_responses_tx)?; | ||
if found_ok { | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
} | ||
r => r, | ||
} | ||
} | ||
|
||
|
@@ -114,28 +143,31 @@ impl<'a, T: Read + Write + 'a> Handle<'a, T> { | |
impl<'a, T: SetReadTimeout + Read + Write + 'a> Handle<'a, T> { | ||
/// Set the keep-alive interval to use when `wait_keepalive` is called. | ||
/// | ||
/// The interval defaults to 29 minutes as dictated by RFC 2177. | ||
/// The interval defaults to 29 minutes as advised by RFC 2177. | ||
pub fn set_keepalive(&mut self, interval: Duration) { | ||
self.keepalive = interval; | ||
self.keepalive = Some(interval); | ||
} | ||
|
||
/// Block until the selected mailbox changes. | ||
/// | ||
/// This method differs from [`Handle::wait`] in that it will periodically refresh the IDLE | ||
/// connection, to prevent the server from timing out our connection. The keepalive interval is | ||
/// set to 29 minutes by default, as dictated by RFC 2177, but can be changed using | ||
/// set to 29 minutes by default, as advised by RFC 2177, but can be changed using | ||
/// [`Handle::set_keepalive`]. | ||
/// | ||
/// This is the recommended method to use for waiting. | ||
pub fn wait_keepalive(self) -> Result<()> { | ||
pub fn wait_keepalive(mut self) -> Result<()> { | ||
// The server MAY consider a client inactive if it has an IDLE command | ||
// running, and if such a server has an inactivity timeout it MAY log | ||
// the client off implicitly at the end of its timeout period. Because | ||
// of that, clients using IDLE are advised to terminate the IDLE and | ||
// re-issue it at least every 29 minutes to avoid being logged off. | ||
// This still allows a client to receive immediate mailbox updates even | ||
// though it need only "poll" at half hour intervals. | ||
let keepalive = self.keepalive; | ||
let keepalive = self | ||
.keepalive | ||
.get_or_insert_with(|| Duration::from_secs(29 * 60)) | ||
.clone(); | ||
self.wait_timeout(keepalive) | ||
} | ||
|
||
|
@@ -154,7 +186,8 @@ impl<'a, T: SetReadTimeout + Read + Write + 'a> Handle<'a, T> { | |
impl<'a, T: Read + Write + 'a> Drop for Handle<'a, T> { | ||
fn drop(&mut self) { | ||
// we don't want to panic here if we can't terminate the Idle | ||
let _ = self.terminate().is_ok(); | ||
// If we sent done, then we should suck up the OK. | ||
let _ = self.terminate(true).is_ok(); | ||
} | ||
} | ||
|
||
|
@@ -164,8 +197,8 @@ impl<'a> SetReadTimeout for TcpStream { | |
} | ||
} | ||
|
||
impl<'a> SetReadTimeout for TlsStream<TcpStream> { | ||
impl<'a, T: SetReadTimeout + Read + Write + 'a> SetReadTimeout for TlsStream<T> { | ||
fn set_read_timeout(&mut self, timeout: Option<Duration>) -> Result<()> { | ||
self.get_ref().set_read_timeout(timeout).map_err(Error::Io) | ||
self.get_mut().set_read_timeout(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
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 don't understand this clause at all. Why do we need this nested loop construct? And why do we need to terminate the IDLE for each iteration?