Skip to content

Commit

Permalink
Keep connections open (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
erthalion authored Aug 2, 2024
1 parent 5f620d2 commit 73912ca
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/worker/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
fmt::Display,
io::{prelude::*, BufReader},
net::TcpListener,
thread,
};

use crate::{BaseConfig, Worker, WorkerError, Workload, WorkloadConfig};
Expand Down Expand Up @@ -51,7 +52,16 @@ impl NetworkWorker {

for stream in listener.incoming() {
let mut stream = stream.unwrap();
loop {

// As a simplest solution to keep a connection open, spawn a
// thread. It's not the best one though, as we waste resources.
// For the purpose of only keeping connections open we could e.g.
// spawn only two threads, where the first one receives connections
// and adds streams into the list of active, and the second iterates
// through streams and replies. This way the connections will have
// high latency, but for the purpose of networking workload it
// doesn't matter.
thread::spawn(move || loop {
let mut buf_reader = BufReader::new(&stream);
let mut buffer = String::new();

Expand All @@ -67,7 +77,6 @@ impl NetworkWorker {
match stream.write_all(response.as_bytes()) {
Ok(_) => {
// Response is sent, handle the next one
break;
}
Err(e) => {
trace!("ERROR: sending response, {}", e);
Expand All @@ -79,7 +88,7 @@ impl NetworkWorker {
trace!("ERROR: reading a line, {}", e)
}
}
}
});
}

Ok(())
Expand Down

0 comments on commit 73912ca

Please sign in to comment.