“由对等方重置连接”使用ab对简单的Rust HTTP服务器进行基准测试时 [英] "Connection reset by peer" when benchmarking a simple Rust HTTP server with ab
问题描述
我正在尝试在Rust中编写一个非常简单的并发服务器来使用该语言的并发原语及其线程模型。这是我的代码:
I'm trying to write an extremely simple concurrent server in Rust to play with the language's concurrency primitives and its threading model. Here's 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) => {
// let count = count.clone();
thread::spawn(move || {
let _ = handle_client(stream);
// let mut count = count.lock().unwrap();
// *count += 1;
// println!("{:?}", *count);
});
}
Err(e) => {
println!("Error: {}", e);
}
}
}
drop(listener);
}
当我运行 ab -c 100 -n 100 http://127.0.0.1:1337/
如上所列运行的程序,我得到 apr_socket_recv:连接重置由peer(104)
几乎立即。为什么?
When I run ab -c 100 -n 100 http://127.0.0.1:1337/
with the program running as listed above, I get apr_socket_recv: Connection reset by peer (104)
almost immediately. Why?
当我添加试试!(stream.shutdown(Shutdown :: Both));
(已注释掉接近顶部,上面)我不再像之前那样得到 apr_socket_recv
错误,但是apachebench给了我结果,说明因异常而导致199个请求失败。为什么?我做错了什么?
When I add try!(stream.shutdown(Shutdown::Both));
(commented out near the top, above) I no longer get the apr_socket_recv
error like before, but apachebench gives me results that say 199 failed requests due to exceptions. Why? 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
推荐答案
我认为问题是你没有完全读取客户端发送的数据 ,因此客户永远不会有机会过渡到阅读回复。当它试图写入更多数据时,它注意到套接字已经关闭并失败。
I believe the problem is that you are not fully reading the data sent from the client, so the client never has a chance to transition to reading the response. When it tries to write more data, it notices the socket has been closed and fails.
我已经扩充了你的示例,以便在回复之前读取所有HTTP头,忽略任何请求机构。如果有错误,我正在努力处理漂亮的错误处理并且恐慌:
I've augmented your example to read all the HTTP headers before replying, ignoring any request body. I'm punting on pretty error handling and just panicking if there are errors:
use std::io::prelude::*;
use std::io::BufReader;
use std::net::{TcpListener, TcpStream};
use std::thread;
fn handle_client(mut stream: TcpStream) {
// Read all the headers
for header in BufReader::new(&mut stream).lines() {
let header = header.unwrap();
if header == "\r" { break }
}
// Write our response
stream.write_all(b"HTTP/1.0 200 OK\r\n\r\n").unwrap();
}
fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
thread::spawn(|| {
handle_client(stream);
});
}
}
这适用于 ab - c 50 -n 5000 http://127.0.0.1:8080/
:
Benchmarking 127.0.0.1 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software:
Server Hostname: 127.0.0.1
Server Port: 8080
Document Path: /
Document Length: 0 bytes
Concurrency Level: 50
Time taken for tests: 1.293 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 95000 bytes
HTML transferred: 0 bytes
Requests per second: 3868.22 [#/sec] (mean)
Time per request: 12.926 [ms] (mean)
Time per request: 0.259 [ms] (mean, across all concurrent requests)
Transfer rate: 71.77 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 6 1.7 6 14
Processing: 1 6 1.7 6 14
Waiting: 1 6 1.7 6 14
Total: 5 13 2.6 12 23
Percentage of the requests served within a certain time (ms)
50% 12
66% 13
75% 13
80% 14
90% 17
95% 19
98% 21
99% 22
100% 23 (longest request)
这篇关于“由对等方重置连接”使用ab对简单的Rust HTTP服务器进行基准测试时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!