Skip to content

Commit

Permalink
update with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
deepu105 committed Dec 23, 2020
1 parent fabbcfe commit 599daaf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
22 changes: 20 additions & 2 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is the code samples used for my blog series https://deepu.tech/concurrency-

The benchmarks were run using ApacheBench

For example
## Go

```shell
cd gows && go run main.go
Expand All @@ -13,11 +13,29 @@ cd gows && go run main.go
ab -c 100 -n 10000 http://localhost:8080/
```

or
## Rust

```shell
cd rustws_async && cargo run --release

# in a new terminal
ab -c 100 -n 10000 http://localhost:8080/
```

## JS

```shell
cd jsws && node main.js

# in a new terminal
ab -c 100 -n 10000 http://localhost:8080/
```

## TS

```shell
cd tsws && deno run --allow-all main.ts

# in a new terminal
ab -c 100 -n 10000 http://localhost:8080/
```
11 changes: 7 additions & 4 deletions rustws/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@ use std::thread;
use std::time::Duration;

fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); // bind listener
let pool = ThreadPool::new(100); // same number as max concurrent requests

let mut count = 0;
let mut count = 0; // count used to introduce delays

// listen to all incoming request streams
for stream in listener.incoming() {
let stream = stream.unwrap();
count = count + 1;
pool.execute(move || {
handle_connection(stream, count);
handle_connection(stream, count); // spawning each connection in a new thread
});
}
}

fn handle_connection(mut stream: TcpStream, count: i64) {
// Read the first 1024 bytes of data from the stream
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();

// add 2 second delay to every 10th request
if (count % 10) == 0 {
println!("Adding delay. Count: {}", count);
thread::sleep(Duration::from_secs(2));
Expand All @@ -40,6 +43,6 @@ Content-Type: text/html; charset=utf-8

let response = format!("{}\r\n\r\n{}", header, contents);

stream.write(response.as_bytes()).unwrap();
stream.write(response.as_bytes()).unwrap(); // write response
stream.flush().unwrap();
}
5 changes: 2 additions & 3 deletions rustws_async/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use std::time::Duration;

#[async_std::main]
async fn main() {
let mut count = 0;

let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap(); // set listen port
let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap(); // bind listener
let mut count = 0; // count used to introduce delays

loop {
count = count + 1;
Expand Down
28 changes: 20 additions & 8 deletions rustws_async_thread/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,51 @@ use std::net::TcpStream;
use std::thread;
use std::time::Duration;
extern crate futures;
use futures::executor::ThreadPool;
use futures::executor::ThreadPoolBuilder;

fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); // bind listener

let pool = ThreadPool::new().expect("couldn't create threadpool");
let mut count = 0;
let mut pool_builder = ThreadPoolBuilder::new();
pool_builder.pool_size(100);
let pool = pool_builder.create().expect("couldn't create threadpool");
let mut count = 0; // count used to introduce delays

// Listen for an incoming connection.
for stream in listener.incoming() {
let stream = stream.unwrap();
count = count + 1;
let count_n = Box::new(count);

// spawning each connection in a new thread asynchronously
pool.spawn_ok(async {
handle_connection(stream, count_n).await;
});
}
}

async fn handle_connection(mut stream: TcpStream, count: Box<i64>) {
// Read the first 1024 bytes of data from the stream
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();

// add 2 second delay to every 10th request
if (*count % 10) == 0 {
println!("Adding delay. Count: {}", count);
thread::sleep(Duration::from_secs(2));
}

let (status_line, filename) = ("HTTP/1.1 200 OK\r\n\r\n", "hello.html");
let contents = fs::read_to_string(filename).unwrap();
let header = "
HTTP/1.0 200 OK
Connection: keep-alive
Content-Length: 174
Content-Type: text/html; charset=utf-8
";

let response = format!("{}{}", status_line, contents);
let contents = fs::read_to_string("hello.html").unwrap();

stream.write(response.as_bytes()).unwrap();
let response = format!("{}\r\n\r\n{}", header, contents);

stream.write(response.as_bytes()).unwrap(); // write response
stream.flush().unwrap();
}

0 comments on commit 599daaf

Please sign in to comment.