Skip to content

Commit

Permalink
Create new thread per request to support parallel requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Deeds67 committed Mar 28, 2024
1 parent ae96665 commit 7f37a8e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/action_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{key_value_repository::RedisDatabase, resp::RespType};

#[derive(Clone)]
pub struct RedisActionHandler {
db: RedisDatabase,
}
Expand All @@ -9,7 +10,7 @@ impl RedisActionHandler {
RedisActionHandler { db }
}

pub fn handle(&mut self, resp: RespType) -> RespType {
pub fn handle(&self, resp: RespType) -> RespType {
let action = RedisAction::from(resp);
match action {
RedisAction::Set(key, value) => {
Expand Down
1 change: 1 addition & 0 deletions src/key_value_repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use dashmap::DashMap;

#[derive(Clone)]
pub struct RedisDatabase {
data: DashMap<String, String>,
}
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ mod server;
mod action_handler;
mod key_value_repository;
mod resp;
use std::sync::Arc;

fn main() {
let repository = key_value_repository::RedisDatabase::new();
let mut action_handler = action_handler::RedisActionHandler::new(repository);
let action_handler = Arc::new(action_handler::RedisActionHandler::new(repository));

let port: &str = "6388";
println!("Starting tcp stream on port {}", port);
server::start_tcp_stream(port, &mut action_handler);
server::start_tcp_stream(port, action_handler);
}
11 changes: 8 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::io::BufReader;
use std::net::{TcpListener, TcpStream};
use std::thread;
use std::sync::Arc;

use crate::action_handler::{self};
use crate::resp::{RespType, RespDeserializer, RespSerializer};


fn handle_client(mut stream: TcpStream, action_handler: &mut action_handler::RedisActionHandler) {
fn handle_client(mut stream: TcpStream, action_handler: Arc<action_handler::RedisActionHandler>) {
println!("Incoming connection from: {}", stream.peer_addr().unwrap());
let buf_reader = BufReader::new(&mut stream);
let mut parser = RespDeserializer::new(buf_reader);
Expand All @@ -23,12 +25,15 @@ fn handle_client(mut stream: TcpStream, action_handler: &mut action_handler::Red
}
}

pub fn start_tcp_stream(port: &str, action_handler: &mut action_handler::RedisActionHandler) {
pub fn start_tcp_stream(port: &str, action_handler: Arc<action_handler::RedisActionHandler>) {
let listener = TcpListener::bind(format!("127.0.0.1:{}", port)).expect("Could not bind");
for stream in listener.incoming() {
match stream {
Ok(stream) => {
handle_client(stream, action_handler);
let handler = Arc::clone(&action_handler);
thread::spawn(move || {
handle_client(stream, handler);
});
}
Err(e) => {
eprintln!("Failed: {}", e)
Expand Down

0 comments on commit 7f37a8e

Please sign in to comment.