aboutsummaryrefslogtreecommitdiff
path: root/src/cache
diff options
context:
space:
mode:
authorMishio595 <[email protected]>2018-11-13 22:23:39 -0700
committerMishio595 <[email protected]>2018-11-13 22:23:39 -0700
commit1dad6996d8e9c983dc9e1689a93dbef5b0696c22 (patch)
tree1ee7226178cae76b20e6adcdc7e7cfcc3c477c32 /src/cache
parentFinish rebase (diff)
parentMake `Region`s `Japan`-variant lowercase. (diff)
downloadserenity-1dad6996d8e9c983dc9e1689a93dbef5b0696c22.tar.xz
serenity-1dad6996d8e9c983dc9e1689a93dbef5b0696c22.zip
Merge branch 'upstream'
Diffstat (limited to 'src/cache')
-rw-r--r--src/cache/mod.rs12
-rw-r--r--src/cache/settings.rs163
2 files changed, 124 insertions, 51 deletions
diff --git a/src/cache/mod.rs b/src/cache/mod.rs
index 41d66d4..ece7a57 100644
--- a/src/cache/mod.rs
+++ b/src/cache/mod.rs
@@ -52,7 +52,8 @@ use std::collections::{
};
use std::{
default::Default,
- sync::Arc
+ sync::Arc,
+ time::Duration,
};
mod cache_update;
@@ -798,6 +799,15 @@ impl Cache {
e.update(self)
}
+ /// Gets the duration it will try for when acquiring a write lock.
+ ///
+ /// Refer to the documentation for [`cache_lock_time`] for more information.
+ ///
+ /// [`cache_lock_time`]: struct.Settings.html#method.cache_lock_time
+ pub fn get_try_write_duration(&self) -> Option<Duration> {
+ self.settings.cache_lock_time
+ }
+
pub(crate) fn update_user_entry(&mut self, user: &User) {
match self.users.entry(user.id) {
Entry::Vacant(e) => {
diff --git a/src/cache/settings.rs b/src/cache/settings.rs
index e8e456b..975ddc6 100644
--- a/src/cache/settings.rs
+++ b/src/cache/settings.rs
@@ -1,50 +1,113 @@
-/// Settings for the cache.
-///
-/// # Examples
-///
-/// Create new settings, specifying the maximum number of messages:
-///
-/// ```rust
-/// use serenity::cache::Settings as CacheSettings;
-///
-/// let mut settings = CacheSettings::new();
-/// settings.max_messages(10);
-/// ```
-#[derive(Clone, Debug, Default)]
-pub struct Settings {
- /// The maximum number of messages to store in a channel's message cache.
- ///
- /// Defaults to 0.
- pub max_messages: usize,
- __nonexhaustive: (),
-}
-
-impl Settings {
- /// Creates new settings to be used with a cache.
- #[inline]
- pub fn new() -> Self {
- Self::default()
- }
-
- /// Sets the maximum number of messages to cache in a channel.
- ///
- /// Refer to [`max_messages`] for more information.
- ///
- /// # Examples
- ///
- /// Set the maximum number of messages to cache:
- ///
- /// ```rust
- /// use serenity::cache::Settings;
- ///
- /// let mut settings = Settings::new();
- /// settings.max_messages(10);
- /// ```
- ///
- /// [`max_messages`]: #structfield.max_messages
- pub fn max_messages(&mut self, max: usize) -> &mut Self {
- self.max_messages = max;
-
- self
- }
-}
+use std::time::Duration;
+
+/// Settings for the cache.
+///
+/// # Examples
+///
+/// Create new settings, specifying the maximum number of messages:
+///
+/// ```rust
+/// use serenity::cache::Settings as CacheSettings;
+///
+/// let mut settings = CacheSettings::new();
+/// settings.max_messages(10);
+/// ```
+#[derive(Clone, Debug)]
+pub struct Settings {
+ /// The maximum number of messages to store in a channel's message cache.
+ ///
+ /// Defaults to 0.
+ pub max_messages: usize,
+
+ /// The Duration cache updates will try to acquire write-locks for.
+ ///
+ /// Defaults to 10 milliseconds.
+ ///
+ /// **Note**:
+ /// If set to `None`, cache updates will acquire write-lock until available,
+ /// potentially deadlocking.
+ pub cache_lock_time: Option<Duration>,
+ __nonexhaustive: (),
+}
+
+impl Default for Settings {
+ fn default() -> Self {
+ Settings {
+ max_messages: usize::default(),
+ cache_lock_time: Some(Duration::from_millis(10)),
+ __nonexhaustive: (),
+ }
+ }
+}
+
+impl Settings {
+ /// Creates new settings to be used with a cache.
+ #[inline]
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ /// Sets the maximum number of messages to cache in a channel.
+ ///
+ /// Refer to [`max_messages`] for more information.
+ ///
+ /// # Examples
+ ///
+ /// Set the maximum number of messages to cache:
+ ///
+ /// ```rust
+ /// use serenity::cache::Settings;
+ ///
+ /// let mut settings = Settings::new();
+ /// settings.max_messages(10);
+ /// ```
+ ///
+ /// [`max_messages`]: #structfield.max_messages
+ pub fn max_messages(&mut self, max: usize) -> &mut Self {
+ self.max_messages = max;
+
+ self
+ }
+
+ /// Sets the duration that the cache will try to aquire a write lock.
+ ///
+ /// Refer to [`cache_lock_time`] for more information.
+ ///
+ /// **Note**:
+ /// Should be set before the client gets started, as it can not be
+ /// changed after the first read of the duration.
+ ///
+ /// # Examples
+ ///
+ /// Set the time that it will try to aquire a lock.
+ ///
+ /// ```rust,no_run
+ /// use std::time::Duration;
+ /// use std::env;
+ /// use serenity::prelude::*;
+ ///
+ /// struct Handler;
+ ///
+ /// impl EventHandler for Handler {}
+ ///
+ /// fn main() {
+ /// let token = env::var("DISCORD_TOKEN")
+ /// .expect("Expected a token in the environment");
+ /// serenity::CACHE
+ /// .write().settings_mut()
+ /// .cache_lock_time(Some(Duration::from_secs(1)));
+ /// let mut client = Client::new(&token, Handler).unwrap();
+ ///
+ /// if let Err(why) = client.start() {
+ /// println!("Client error: {:?}", why);
+ /// }
+ /// }
+ /// ```
+ ///
+ /// [`cache_lock_time`]: #structfield.cache_lock_time
+ pub fn cache_lock_time(&mut self, duration: Option<Duration>) -> &mut Self {
+ self.cache_lock_time = duration;
+
+ self
+ }
+}