base_client: Move the session behind a lock.

master
Damir Jelić 2020-05-06 14:27:53 +02:00
parent 967544bab9
commit 3a30d53437
3 changed files with 14 additions and 11 deletions

View File

@ -300,9 +300,7 @@ impl AsyncClient {
/// Is the client logged in. /// Is the client logged in.
pub async fn logged_in(&self) -> bool { pub async fn logged_in(&self) -> bool {
// TODO turn this into a atomic bool so this method doesn't need to be self.base_client.read().await.logged_in().await
// async.
self.base_client.read().await.logged_in()
} }
/// The Homeserver of the client. /// The Homeserver of the client.
@ -806,8 +804,9 @@ impl AsyncClient {
let request_builder = if Request::METADATA.requires_authentication { let request_builder = if Request::METADATA.requires_authentication {
let client = self.base_client.read().await; let client = self.base_client.read().await;
let session = client.session.read().await;
if let Some(ref session) = client.session { if let Some(session) = session.as_ref() {
request_builder.bearer_auth(&session.access_token) request_builder.bearer_auth(&session.access_token)
} else { } else {
return Err(Error::AuthenticationRequired); return Err(Error::AuthenticationRequired);

View File

@ -66,7 +66,7 @@ pub type Token = String;
pub struct Client { pub struct Client {
/// The current client session containing our user id, device id and access /// The current client session containing our user id, device id and access
/// token. /// token.
pub session: Option<Session>, pub session: Arc<RwLock<Option<Session>>>,
/// The current sync token that should be used for the next sync call. /// The current sync token that should be used for the next sync call.
pub sync_token: Arc<RwLock<Option<Token>>>, pub sync_token: Arc<RwLock<Option<Token>>>,
/// A map of the rooms our user is joined in. /// A map of the rooms our user is joined in.
@ -118,7 +118,7 @@ impl Client {
}; };
Ok(Client { Ok(Client {
session, session: Arc::new(RwLock::new(session)),
sync_token: Arc::new(RwLock::new(None)), sync_token: Arc::new(RwLock::new(None)),
joined_rooms: Arc::new(RwLock::new(HashMap::new())), joined_rooms: Arc::new(RwLock::new(HashMap::new())),
ignored_users: Arc::new(RwLock::new(Vec::new())), ignored_users: Arc::new(RwLock::new(Vec::new())),
@ -132,8 +132,10 @@ impl Client {
} }
/// Is the client logged in. /// Is the client logged in.
pub fn logged_in(&self) -> bool { pub async fn logged_in(&self) -> bool {
self.session.is_some() // TODO turn this into a atomic bool so this method doesn't need to be
// async.
self.session.read().await.is_some()
} }
/// Add `EventEmitter` to `Client`. /// Add `EventEmitter` to `Client`.
@ -153,7 +155,7 @@ impl Client {
/// Returns `true` when a state store sync has successfully completed. /// Returns `true` when a state store sync has successfully completed.
pub(crate) async fn sync_with_state_store(&mut self) -> Result<bool> { pub(crate) async fn sync_with_state_store(&mut self) -> Result<bool> {
if let Some(store) = self.state_store.as_ref() { if let Some(store) = self.state_store.as_ref() {
if let Some(sess) = self.session.as_ref() { if let Some(sess) = self.session.read().await.as_ref() {
if let Some(client_state) = store.load_client_state(sess).await? { if let Some(client_state) = store.load_client_state(sess).await? {
let ClientState { let ClientState {
sync_token, sync_token,
@ -198,7 +200,7 @@ impl Client {
device_id: response.device_id.clone(), device_id: response.device_id.clone(),
user_id: response.user_id.clone(), user_id: response.user_id.clone(),
}; };
self.session = Some(session); *self.session.write().await = Some(session);
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
{ {
@ -236,6 +238,8 @@ impl Client {
room_id, room_id,
&self &self
.session .session
.read()
.await
.as_ref() .as_ref()
.expect("Receiving events while not being logged in") .expect("Receiving events while not being logged in")
.user_id, .user_id,

View File

@ -290,7 +290,7 @@ mod test {
let base_client = client.base_client.read().await; let base_client = client.base_client.read().await;
// assert the synced client and the logged in client are equal // assert the synced client and the logged in client are equal
assert_eq!(base_client.session, Some(session)); assert_eq!(*base_client.session.read().await, Some(session));
assert_eq!( assert_eq!(
base_client.sync_token().await, base_client.sync_token().await,
Some("s526_47314_0_7_1_1_1_11444_1".to_string()) Some("s526_47314_0_7_1_1_1_11444_1".to_string())