Compare commits
8 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f829f816ba | ||
|
|
f2ed92564c | ||
|
|
c817629db0 | ||
|
|
35e2a66628 | ||
|
|
18923b0952 | ||
|
|
e4211e27bf | ||
|
|
bbf889e72e | ||
|
|
24f0ca7de1 |
2 changed files with 68 additions and 11 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
46
src/main.rs
46
src/main.rs
|
|
@ -1,12 +1,31 @@
|
||||||
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::thread;
|
use std::sync::{Arc, Mutex, mpsc};
|
||||||
|
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";
|
||||||
|
|
||||||
|
struct Worker {
|
||||||
|
id: usize,
|
||||||
|
thread: thread::JoinHandle<()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Worker {
|
||||||
|
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
|
||||||
|
let thread = thread::spawn(move || {
|
||||||
|
loop {
|
||||||
|
let job = receiver.lock().unwrap().recv().unwrap();
|
||||||
|
job();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Worker { id, thread }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ThreadPool {
|
pub struct ThreadPool {
|
||||||
threads: Vec<thread::JoinHandle<()>>,
|
workers: Vec<Worker>,
|
||||||
|
sender: mpsc::Sender<Job>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ThreadPool {
|
impl ThreadPool {
|
||||||
|
|
@ -17,14 +36,25 @@ impl ThreadPool {
|
||||||
/// The `new` function panics if the size is zero
|
/// The `new` function panics if the size is zero
|
||||||
pub fn new(size: usize) -> Self {
|
pub fn new(size: usize) -> Self {
|
||||||
assert!(size > 0);
|
assert!(size > 0);
|
||||||
let threads = Vec::with_capacity(size);
|
let mut workers = Vec::with_capacity(size);
|
||||||
for _ in 0..size {
|
let (sender, receiver) = mpsc::channel();
|
||||||
|
let receiver = Arc::new(Mutex::new(receiver));
|
||||||
|
for id in 0..size {
|
||||||
|
workers.push(Worker::new(id, Arc::clone(&receiver)))
|
||||||
}
|
}
|
||||||
Self {threads}
|
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();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Job = Box<dyn FnOnce() + Send + 'static>;
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
@ -32,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