Skip to content

Commit

Permalink
Read-write-lock on memory session allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
ostenbom committed May 23, 2023
1 parent 870360e commit dbb5e60
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
8 changes: 5 additions & 3 deletions linkup-cli/src/check.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use std::thread;

use std::time::{Duration, Instant};

use reqwest::blocking::Client;
use reqwest::StatusCode;
Expand Down Expand Up @@ -166,7 +165,10 @@ pub fn wait_till_ok(url: String) -> Result<(), CliError> {
let start = Instant::now();
loop {
if start.elapsed() > Duration::from_secs(15) {
return Err(CliError::StartLinkupTimeout(format!("{} took too long to load", url)));
return Err(CliError::StartLinkupTimeout(format!(
"{} took too long to load",
url
)));
}

let response = client.get(&url).send();
Expand Down
4 changes: 4 additions & 0 deletions linkup-cli/src/local_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ async fn linkup_request_handler(
.get_request_session(url.clone(), headers.clone())
.await;

if session_result.is_err() {
println!("Failed to get session: {:?}", session_result);
}

let (session_name, config) = match session_result {
Ok(result) => result,
Err(_) => {
Expand Down
1 change: 0 additions & 1 deletion linkup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ pub fn get_target_url(
}
})
.unwrap_or_else(|| domain.default_service.clone());
println!("target service: {:#?}", service_name);

if let Some(service) = config.services.get(&service_name) {
let mut new_path = path.to_string();
Expand Down
15 changes: 9 additions & 6 deletions linkup/src/memory_session_store.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use std::{collections::HashMap, sync::Mutex};
use std::{
collections::HashMap,
sync::{Mutex, RwLock},
};

use async_trait::async_trait;

use crate::{SessionError, StringStore};

pub struct MemoryStringStore {
store: Mutex<HashMap<String, String>>,
store: RwLock<HashMap<String, String>>,
}

impl MemoryStringStore {
pub fn new() -> Self {
MemoryStringStore {
store: Mutex::new(HashMap::new()),
store: RwLock::new(HashMap::new()),
}
}
}
Expand All @@ -25,14 +28,14 @@ impl Default for MemoryStringStore {
#[async_trait(?Send)]
impl StringStore for MemoryStringStore {
async fn get(&self, key: String) -> Result<Option<String>, SessionError> {
match self.store.lock() {
match self.store.read() {
Ok(l) => Ok(l.get(key.as_str()).cloned()),
Err(e) => Err(SessionError::GetError(e.to_string())),
}
}

async fn exists(&self, key: String) -> Result<bool, SessionError> {
let value = match self.store.lock() {
let value = match self.store.read() {
Ok(l) => Ok(l.get(&key).cloned()),
Err(e) => return Err(SessionError::GetError(e.to_string())),
}?;
Expand All @@ -44,7 +47,7 @@ impl StringStore for MemoryStringStore {
}

async fn put(&self, key: String, value: String) -> Result<(), SessionError> {
match self.store.lock() {
match self.store.write() {
Ok(mut l) => Ok(l.insert(key, value)),
Err(e) => Err(SessionError::PutError(e.to_string())),
}?;
Expand Down
6 changes: 3 additions & 3 deletions linkup/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use regex::Regex;
use serde::{Deserialize, Serialize};
use url::Url;

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Session {
pub session_token: String,
pub services: HashMap<String, Service>,
Expand All @@ -17,13 +17,13 @@ pub struct Session {
pub cache_routes: Option<Vec<Regex>>,
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Service {
pub origin: Url,
pub rewrites: Vec<Rewrite>,
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Rewrite {
pub source: Regex,
pub target: String,
Expand Down

0 comments on commit dbb5e60

Please sign in to comment.