Merge pull request #6 from msgmaxim/use_rwlock

Use RwLock for session versions
This commit is contained in:
Niels Andriesse 2021-06-03 11:06:52 +10:00 committed by GitHub
commit a4cd0aa456
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 7 deletions

View file

@ -1,9 +1,9 @@
use std::collections::HashMap;
use std::convert::TryInto;
use std::path::Path;
use std::sync::Mutex;
use log::{error, info, warn};
use parking_lot::RwLock;
use rand::{thread_rng, Rng};
use rusqlite::params;
use serde::{Deserialize, Serialize};
@ -32,7 +32,7 @@ pub const SESSION_VERSION_UPDATE_INTERVAL: i64 = 30 * 60;
lazy_static::lazy_static! {
pub static ref SESSION_VERSIONS: Mutex<HashMap<String, (i64, String)>> = Mutex::new(HashMap::new());
pub static ref SESSION_VERSIONS: RwLock<HashMap<String, (i64, String)>> = RwLock::new(HashMap::new());
}
// Rooms
@ -1012,7 +1012,7 @@ pub async fn get_url() -> Result<Response, Rejection> {
}
pub async fn get_session_version(platform: &str) -> Result<String, Rejection> {
let mut session_versions = SESSION_VERSIONS.lock().unwrap().clone();
let mut session_versions = SESSION_VERSIONS.read().clone();
let now = chrono::Utc::now().timestamp();
if let Some(version_info) = session_versions.get(platform) {
let last_updated = version_info.0;
@ -1029,7 +1029,7 @@ pub async fn get_session_version(platform: &str) -> Result<String, Rejection> {
let tag = release.tag_name;
let tuple = (now, tag.clone());
session_versions.insert(platform.to_string(), tuple);
*SESSION_VERSIONS.lock().unwrap() = session_versions.clone();
*SESSION_VERSIONS.write() = session_versions.clone();
return Ok(tag);
}

File diff suppressed because one or more lines are too long