Skip to content

Commit

Permalink
Add ability to spawn new thread per connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ogzhanolguncu committed Sep 9, 2023
1 parent a641826 commit 5e488e5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.vscode
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod store;
#[macro_use(concat_string)]
extern crate concat_string;
use std::net::{SocketAddrV4, TcpListener};
use std::sync::Arc;
use std::thread;

use connection_manager::client_handler::handle_stream;
use resp::deserialize::deserialize;
Expand All @@ -16,13 +18,15 @@ const PORT: u16 = 6379; //Redis PORT

fn main() {
let listener = TcpListener::bind(SocketAddrV4::new(ADDR, PORT)).unwrap();
let cache = Cache::new();
let cache = Arc::new(Cache::new());

for stream in listener.incoming() {
match stream {
Ok(stream) => {
println!("New connection: {}", stream.peer_addr().unwrap());
handle_stream(stream, &cache)

let cache_clone = cache.clone();
thread::spawn(move || handle_stream(stream, &cache_clone));
}
Err(err) => println!("Connection failed due to {:?}", err),
}
Expand Down
57 changes: 28 additions & 29 deletions src/store/db.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

#[derive(Debug, Clone)]
pub struct Cache {
data: Arc<RwLock<HashMap<String, String>>>,
}
#[derive(Debug, Clone)]
pub struct Cache {
data: Arc<RwLock<HashMap<String, String>>>,
}

impl Cache {
// Initialize a new database
pub fn new() -> Self {
Self {
data: Arc::new(RwLock::new(HashMap::new())),
impl Cache {
pub fn new() -> Self {
Self {
data: Arc::new(RwLock::new(HashMap::new())),
}
}
}

pub fn set(&self, key: String, value: String) {
let mut data = self.data.write().unwrap();
data.insert(key, value);
}
pub fn set(&self, key: String, value: String) {
let mut data = self.data.write().unwrap();
data.insert(key, value);
}

pub fn get(&self, key: &str) -> Option<String> {
let data = self.data.read().unwrap();
data.get(key).cloned()
pub fn get(&self, key: &str) -> Option<String> {
let data = self.data.read().unwrap();
data.get(key).cloned()
}
}
}

#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn should_initialize_and_get_set() {
let cache = Cache::new();
cache.set(String::from("name"), String::from("The Wizard of Oz"));
assert_eq!("The Wizard of Oz", cache.get("name").unwrap());
#[test]
fn should_initialize_and_get_set() {
let cache = Cache::new();
cache.set(String::from("name"), String::from("The Wizard of Oz"));
assert_eq!("The Wizard of Oz", cache.get("name").unwrap());
}
}
}

0 comments on commit 5e488e5

Please sign in to comment.