Use 'RwLock' to make 'Client' 'Sync'.

Resolves #814.
This commit is contained in:
messense 2018-11-06 10:59:44 +08:00 committed by Sergio Benitez
parent 32e2ff4d6b
commit 64ba2056ef
2 changed files with 18 additions and 5 deletions

View File

@ -1,4 +1,4 @@
use std::cell::RefCell;
use std::sync::RwLock;
use std::borrow::Cow;
use Rocket;
@ -53,7 +53,7 @@ use error::LaunchError;
/// [`post`]: #method.post
pub struct Client {
rocket: Rocket,
crate cookies: Option<RefCell<CookieJar>>,
crate cookies: Option<RwLock<CookieJar>>,
}
impl Client {
@ -62,7 +62,7 @@ impl Client {
/// set to `None`.
fn _new(rocket: Rocket, tracked: bool) -> Result<Client, LaunchError> {
let cookies = match tracked {
true => Some(RefCell::new(CookieJar::new())),
true => Some(RwLock::new(CookieJar::new())),
false => None
};
@ -321,3 +321,15 @@ impl Client {
LocalRequest::new(self, method, uri.into())
}
}
#[cfg(test)]
mod test {
use super::Client;
fn assert_sync<T: Sync>() {}
#[test]
fn test_local_client_impl_sync() {
assert_sync::<Client>();
}
}

View File

@ -111,7 +111,8 @@ impl<'c> LocalRequest<'c> {
// Set up any cookies we know about.
if let Some(ref jar) = client.cookies {
for cookie in jar.borrow().iter() {
let cookies = jar.read().expect("LocalRequest::new() read lock");
for cookie in cookies.iter() {
request.cookies().add_original(cookie.clone().into_owned());
}
}
@ -408,7 +409,7 @@ impl<'c> LocalRequest<'c> {
// If the client is tracking cookies, updates the internal cookie jar
// with the changes reflected by `response`.
if let Some(ref jar) = client.cookies {
let mut jar = jar.borrow_mut();
let mut jar = jar.write().expect("LocalRequest::_dispatch() write lock");
let current_time = ::time::now();
for cookie in response.cookies() {
if let Some(expires) = cookie.expires() {