Skip to content

Commit ae3f58b

Browse files
authored
Merge pull request #19 from psalm842/docker_epoll
Ch 04 - Solution for Mac users that want to run the epoll examples using docker
2 parents f2c0fd0 + 8ef149b commit ae3f58b

File tree

13 files changed

+122
-10
lines changed

13 files changed

+122
-10
lines changed

ch04/a-epoll/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM rust:1-slim-bookworm
2+
3+
WORKDIR /usr/a-epoll
4+
5+
COPY . .
6+
7+
RUN cargo build
8+
9+
CMD ["cargo", "run", "delayserver"]

ch04/a-epoll/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ additional comments that's not in the book.
55

66
You can run the example by simply writing `cargo run`
77

8+
If running on a Mac system (which only supports kqueue but not epoll), docker
9+
can be used to run the example by running the epoll_docker.sh script
10+
811
## Note
912

1013
There is one downside of having a local server on the same machine to mimic

ch04/a-epoll/docker-compose.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
services:
2+
delayserver:
3+
build: ../../delayserver/
4+
ports:
5+
- "8080:8080"
6+
networks:
7+
- delayserver_network
8+
9+
epoll:
10+
build: .
11+
depends_on:
12+
- delayserver
13+
networks:
14+
- delayserver_network
15+
16+
networks:
17+
delayserver_network: {}

ch04/a-epoll/epoll_docker.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
docker compose up --build

ch04/a-epoll/src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::{
1717
collections::HashSet,
1818
io::{self, Read, Result, Write},
1919
net::TcpStream,
20+
env
2021
};
2122

2223
use ffi::Event;
@@ -82,13 +83,21 @@ fn main() -> Result<()> {
8283
let n_events = 5;
8384

8485
let mut streams = vec![];
85-
let addr = "localhost:8080";
86+
87+
let args: Vec<String> = env::args().collect();
88+
let base_url;
89+
if args.len() > 1 {
90+
base_url = args[1].clone();
91+
} else {
92+
base_url = String::from("localhost");
93+
}
94+
let addr = format!("{}:8080", &base_url);
8695

8796
for i in 0..n_events {
8897
let delay = (n_events - i) * 1000;
8998
let url_path = format!("/{delay}/request-{i}");
9099
let request = get_req(&url_path);
91-
let mut stream = std::net::TcpStream::connect(addr)?;
100+
let mut stream = std::net::TcpStream::connect(&addr)?;
92101
stream.set_nonblocking(true)?;
93102

94103
stream.write_all(request.as_bytes())?;

ch04/b-epoll-mio/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM rust:1-slim-bookworm
2+
3+
WORKDIR /usr/a-epoll
4+
5+
COPY . .
6+
7+
RUN cargo build
8+
9+
CMD ["cargo", "run", "delayserver"]

ch04/b-epoll-mio/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
# a-epoll-mio
1+
# b-epoll-mio
22

33
This create contains the example code for chapter 4, but instead of using
44
our own queue, we use one created by [mio](https://github.com/tokio-rs/mio). Since we modelled our own code after
55
mio you only need to make a few very minor changes to get it working which I've
66
commented and marked out as clearly as I can.
77

8+
If running on a Mac system (which only supports kqueue but not epoll), docker
9+
can be used to run the example by running the epoll_mio_docker.sh script
10+
811
You can run the example by simply writing `cargo run`
912

1013
## Note

ch04/b-epoll-mio/docker-compose.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
services:
2+
delayserver:
3+
build: ../../delayserver/
4+
ports:
5+
- "8080:8080"
6+
networks:
7+
- delayserver_network
8+
9+
epoll-mio:
10+
build: .
11+
depends_on:
12+
- delayserver
13+
networks:
14+
- delayserver_network
15+
16+
networks:
17+
delayserver_network: {}

ch04/b-epoll-mio/epoll_mio_docker.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
docker compose up --build

ch04/b-epoll-mio/src/main.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
//! I don't consider this a bug with our code but a surprising behavior of the
2727
//! WSL/Windows network stack. Anyway, if you encounter this, the fix is simple:
2828
//!
29-
//! Change `let addr = "localhost:8080";` to `let addr = "127.0.0.1:8080";`.
29+
//! Change `base_url = String::from("localhost");` to `base_url = String::from("127.0.0.1");`.
3030
//!
3131
3232
// FIX #4 (import `HashSet``)
3333
use std::collections::HashSet;
34+
use std::env;
3435
use std::io::{self, Read, Result, Write};
3536

3637
use mio::event::Event;
@@ -89,13 +90,21 @@ fn main() -> Result<()> {
8990
let n_events = 5;
9091

9192
let mut streams = vec![];
92-
let addr = "localhost:8080";
93+
94+
let args: Vec<String> = env::args().collect();
95+
let base_url;
96+
if args.len() > 1 {
97+
base_url = args[1].clone();
98+
} else {
99+
base_url = String::from("localhost");
100+
}
101+
let addr = format!("{}:8080", &base_url);
93102

94103
for i in 0..n_events {
95104
let delay = (n_events - i) * 1000;
96105
let url_path = format!("/{delay}/request-{i}");
97106
let request = get_req(&url_path);
98-
let std_stream = std::net::TcpStream::connect(addr)?;
107+
let std_stream = std::net::TcpStream::connect(&addr)?;
99108
std_stream.set_nonblocking(true)?;
100109

101110
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

delayserver/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM rust:1-slim-bookworm
2+
3+
WORKDIR /usr/delayserver
4+
5+
COPY . .
6+
7+
RUN cargo build
8+
9+
CMD ["cargo", "run", "delayserver"]

delayserver/docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
delayserver:
3+
build: .
4+
ports:
5+
- 8080
6+
networks:
7+
- network1
8+
9+
networks:
10+
network1: {}

delayserver/src/main.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
use std::{time::Duration, sync::atomic::{AtomicUsize, Ordering}};
1+
use std::{time::Duration, sync::atomic::{AtomicUsize, Ordering}, env};
22
use actix_web::{Responder, get, HttpServer, App, web, rt::time::sleep};
33

44
const EXPLANATION: &str =
55
"USAGE:
66
Delay server works by issuing a http GET request in the format:
77
http://localhost:8080/[delay in ms]/[UrlEncoded message]
88
9-
On reception, it immediately reports the following to the console:
9+
If an argument is passed in when delayserver is started, that
10+
argument will be used as the url instead of 'localhost'
11+
12+
On reception, it immieiately reports the following to the console:
13+
1014
{Message #} - {delay in ms}: {message}
1115
1216
The server then delays the response for the requested time and echoes the message back to the caller.
@@ -27,12 +31,19 @@ async fn delay(path: web::Path<(u64, String)>) -> impl Responder {
2731

2832
#[actix_web::main]
2933
async fn main() -> std::io::Result<()> {
34+
let args: Vec<String> = env::args().collect();
35+
let url;
36+
if args.len() > 1 {
37+
url = args[1].clone();
38+
} else {
39+
url = String::from("localhost");
40+
}
3041
println!("{EXPLANATION}");
3142
HttpServer::new(|| {
3243
App::new()
3344
.service(delay)
3445
})
35-
.bind(("127.0.0.1", 8080))?
46+
.bind((url, 8080))?
3647
.run()
3748
.await
38-
}
49+
}

0 commit comments

Comments
 (0)