Commit c5ba826ee4

c5ba826ee4948bb6618e91460c7b50ac2d58bfc1

parent: 48374d9746

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-07-13 15:33 UTC

room_buffer: Use a mutex to make sure that we don't send mutliple typing notifs.

Layout: unified · split

src/room_buffer.rs +13 −15
@@ -38,7 +38,7 @@ use async_trait::async_trait;
38use crate::server::{Connection, TYPING_NOTICE_TIMEOUT}; 38use crate::server::{Connection, TYPING_NOTICE_TIMEOUT};
39use std::cell::{Ref, RefCell, RefMut}; 39use std::cell::{Ref, RefCell, RefMut};
40use std::rc::Rc; 40use std::rc::Rc;
41use std::sync::atomic::{AtomicBool, Ordering}; 41use std::sync::Mutex;
42use std::time::Instant; 42use std::time::Instant;
43use weechat::buffer::{ 43use weechat::buffer::{
44 Buffer, BufferHandle, BufferInputCallbackAsync, BufferSettingsAsync, 44 Buffer, BufferHandle, BufferInputCallbackAsync, BufferSettingsAsync,
@@ -49,7 +49,7 @@ use weechat::Weechat;
49pub struct RoomBuffer { 49pub struct RoomBuffer {
50 own_user_id: Rc<UserId>, 50 own_user_id: Rc<UserId>,
51 room_id: Rc<RoomId>, 51 room_id: Rc<RoomId>,
52 typing_in_flight: Rc<AtomicBool>, 52 typing_in_flight: Mutex<()>,
53 inner: MatrixRoom, 53 inner: MatrixRoom,
54 buffer_handle: BufferHandle, 54 buffer_handle: BufferHandle,
55} 55}
@@ -127,7 +127,7 @@ impl RoomBuffer {
127 RoomBuffer { 127 RoomBuffer {
128 room_id: Rc::new(room_id), 128 room_id: Rc::new(room_id),
129 own_user_id: Rc::new(own_user_id.to_owned()), 129 own_user_id: Rc::new(own_user_id.to_owned()),
130 typing_in_flight: Rc::new(AtomicBool::from(false)), 130 typing_in_flight: Mutex::new(()),
131 inner: room, 131 inner: room,
132 buffer_handle, 132 buffer_handle,
133 } 133 }
@@ -221,9 +221,10 @@ impl RoomBuffer {
221 pub fn update_typing_notice(&self) { 221 pub fn update_typing_notice(&self) {
222 // We're in the process of sending out a typing notice, so don't make 222 // We're in the process of sending out a typing notice, so don't make
223 // the same request twice. 223 // the same request twice.
224 if self.typing_in_flight.load(Ordering::Relaxed) { 224 let guard = match self.typing_in_flight.try_lock() {
225 return; 225 Ok(guard) => guard,
226 } 226 Err(_) => return,
227 };
227 228
228 let buffer = self.weechat_buffer(); 229 let buffer = self.weechat_buffer();
229 let input = buffer.input(); 230 let input = buffer.input();
@@ -237,9 +238,8 @@ impl RoomBuffer {
237 let room_id = self.room_id.clone(); 238 let room_id = self.room_id.clone();
238 let user_id = self.own_user_id.clone(); 239 let user_id = self.own_user_id.clone();
239 let typing_notice_time = self.inner.typing_notice_time.clone(); 240 let typing_notice_time = self.inner.typing_notice_time.clone();
240 let typing_in_flight = self.typing_in_flight.clone();
241 241
242 let send = async move |typing: bool| { 242 let send = async move |typing: bool, _guard| {
243 let typing_time = typing_notice_time; 243 let typing_time = typing_notice_time;
244 244
245 if let Some(connection) = &*connection.borrow() { 245 if let Some(connection) = &*connection.borrow() {
@@ -264,7 +264,8 @@ impl RoomBuffer {
264 } 264 }
265 }; 265 };
266 266
267 typing_in_flight.store(false, Ordering::Relaxed); 267 // The `_guard` expires here, releasing the mutex taken at the beginning of
268 // `update_typing_notice`.
268 }; 269 };
269 270
270 let typing_time = self.inner.typing_notice_time.borrow_mut(); 271 let typing_time = self.inner.typing_notice_time.borrow_mut();
@@ -272,21 +273,18 @@ impl RoomBuffer {
272 if input.len() < 4 && typing_time.is_some() { 273 if input.len() < 4 && typing_time.is_some() {
273 // If we have an active typing notice and our input is short, e.g. 274 // If we have an active typing notice and our input is short, e.g.
274 // we removed the input set the typing notice to false. 275 // we removed the input set the typing notice to false.
275 self.typing_in_flight.store(true, Ordering::Release); 276 Weechat::spawn(send(false, guard));
276 Weechat::spawn(send(false));
277 } else if input.len() >= 4 { 277 } else if input.len() >= 4 {
278 if let Some(typing_time) = &*typing_time { 278 if let Some(typing_time) = &*typing_time {
279 // If we have some valid input, check if the typing notice 279 // If we have some valid input, check if the typing notice
280 // expired and send one out if it indeed expired. 280 // expired and send one out if it indeed expired.
281 if typing_time.elapsed() > TYPING_NOTICE_TIMEOUT { 281 if typing_time.elapsed() > TYPING_NOTICE_TIMEOUT {
282 self.typing_in_flight.store(true, Ordering::Release); 282 Weechat::spawn(send(true, guard));
283 Weechat::spawn(send(true));
284 } 283 }
285 } else { 284 } else {
286 // If we have some valid input and no active typing notice, send 285 // If we have some valid input and no active typing notice, send
287 // one out. 286 // one out.
288 self.typing_in_flight.store(true, Ordering::Release); 287 Weechat::spawn(send(true, guard));
289 Weechat::spawn(send(true));
290 } 288 }
291 } 289 }
292 } 290 }