I am trying to write an extremely simple parallel server in Rust to play with the concurrency primitives and its thread model. Here is my code:
use std::io::prelude::*; use std::io::Result; use std::net::{TcpListener, TcpStream, Shutdown}; use std::sync::{Arc, Mutex}; use std::thread; fn handle_client(mut stream: TcpStream) -> Result<()> { try!(stream.write(b"HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nPong!\r\n")); // try!(stream.shutdown(Shutdown::Both)); Ok(()) } fn main() { let listener = TcpListener::bind("127.0.0.1:1337").unwrap(); // let count = Arc::new(Mutex::new(0)); for stream in listener.incoming() { match stream { Ok(stream) => {
When I run ab -c 100 -n 100 http://127.0.0.1:1337/ with the program running, as indicated above, I get apr_socket_recv: Connection reset by peer (104) almost immediately. Why?
When I add try!(stream.shutdown(Shutdown::Both)); (commented out near the top, above), I no longer get the apr_socket_recv error as before, but apachebench gives me results that tell 199 failed requests due to exceptions. What for? What am I doing wrong?
Concurrency Level: 100 Time taken for tests: 0.008 seconds Complete requests: 100 Failed requests: 199 (Connect: 0, Receive: 0, Length: 0, Exceptions: 199) Total transferred: 500 bytes
source share