Compare commits
4 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f829f816ba | ||
|
|
f2ed92564c | ||
|
|
c817629db0 | ||
|
|
35e2a66628 |
2 changed files with 46 additions and 8 deletions
29
.devcontainer/devcontainer.json
Normal file
29
.devcontainer/devcontainer.json
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
{
|
||||||
|
"name": "Rust Workshop",
|
||||||
|
"image": "mcr.microsoft.com/devcontainers/rust:1-bullseye",
|
||||||
|
"features": {
|
||||||
|
"ghcr.io/devcontainers/features/git:1": {}
|
||||||
|
},
|
||||||
|
"customizations": {
|
||||||
|
"vscode": {
|
||||||
|
"extensions": [
|
||||||
|
"rust-lang.rust-analyzer",
|
||||||
|
"vadimcn.vscode-lldb",
|
||||||
|
"tamasfe.even-better-toml",
|
||||||
|
"humao.rest-client"
|
||||||
|
],
|
||||||
|
"settings": {
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"rust-analyzer.check.command": "clippy"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"postCreateCommand": "cargo build",
|
||||||
|
"forwardPorts": [7878],
|
||||||
|
"portsAttributes": {
|
||||||
|
"7878": {
|
||||||
|
"label": "HTTP Server",
|
||||||
|
"onAutoForward": "notify"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/main.rs
25
src/main.rs
|
|
@ -1,7 +1,7 @@
|
||||||
use log::info;
|
use log::info;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::net::{TcpListener, TcpStream};
|
use std::net::{TcpListener, TcpStream};
|
||||||
use std::sync::{mpsc, Arc, Mutex};
|
use std::sync::{Arc, Mutex, mpsc};
|
||||||
use std::{sync, thread};
|
use std::{sync, thread};
|
||||||
|
|
||||||
static GET: &'static [u8] = b"GET / HTTP/1.1\r\n";
|
static GET: &'static [u8] = b"GET / HTTP/1.1\r\n";
|
||||||
|
|
@ -12,9 +12,12 @@ struct Worker {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Worker {
|
impl Worker {
|
||||||
fn new(id: usize, receiver: mpsc::Receiver<Job>) -> Worker {
|
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
|
||||||
let thread = thread::spawn(|| {
|
let thread = thread::spawn(move || {
|
||||||
receiver;
|
loop {
|
||||||
|
let job = receiver.lock().unwrap().recv().unwrap();
|
||||||
|
job();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
Worker { id, thread }
|
Worker { id, thread }
|
||||||
}
|
}
|
||||||
|
|
@ -41,9 +44,17 @@ impl ThreadPool {
|
||||||
}
|
}
|
||||||
Self { workers, sender }
|
Self { workers, sender }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn execute<F>(&self, f: F)
|
||||||
|
where
|
||||||
|
F: FnOnce() + Send + 'static,
|
||||||
|
{
|
||||||
|
let job = Box::new(f);
|
||||||
|
self.sender.send(job).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Job {}
|
type Job = Box<dyn FnOnce() + Send + 'static>;
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
@ -51,9 +62,7 @@ fn main() -> std::io::Result<()> {
|
||||||
let pool = ThreadPool::new(4);
|
let pool = ThreadPool::new(4);
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
let stream = stream?;
|
let stream = stream?;
|
||||||
thread::spawn(|| {
|
pool.execute(|| handle_connection(stream).unwrap());
|
||||||
handle_connection(stream).unwrap();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue