neitomic / doh-server

An implementation of DNS server which supports DNS over HTTPS
1 stars 1 forks source link

timeout for queries to dns servers #16

Open sonhmai opened 4 days ago

sonhmai commented 4 days ago

probably need timeout in queries to dns servers to avoid waiting for too long when packet is lost, etc.

    let mut req_buffer = BytePacketBuffer::new();
    packet.write(&mut req_buffer)?;

    socket.send_to(&req_buffer.buf[0..req_buffer.pos], server).await?;

    let mut res_buffer = BytePacketBuffer::new();
    socket.recv_from(&mut res_buffer.buf).await?;
sonhmai commented 4 days ago

🚀 🚀 🚀

throughput increased and is more consistent after adding timeout

After adding timeout example

Benchmarking A records
Running 30s test @ https://localhost/dns-query
  4 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     7.57ms    8.09ms 238.21ms   97.36%
    Req/Sec   268.23     71.78   410.00     79.23%
  29433 requests in 30.03s, 9.39MB read
  Socket errors: connect 0, read 32, write 0, timeout 0
Requests/sec:    980.20
Transfer/sec:    320.34KB
------------------------------
Total Requests: 29433
Avg. Latency: 7.566 ms
Max Latency: 238.214 ms
50th percentile: 6.145 ms
90th percentile: 11.342 ms
99th percentile: 24.943 ms
------------------------------
Benchmarking AAAA records
Running 30s test @ https://localhost/dns-query
  4 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     5.45ms    2.77ms  48.28ms   70.09%
    Req/Sec   363.82     47.90   485.00     75.50%
  43579 requests in 30.09s, 13.89MB read
  Socket errors: connect 0, read 4, write 0, timeout 0
Requests/sec:   1448.14
Transfer/sec:    472.69KB
------------------------------
Total Requests: 43579
Avg. Latency: 5.446 ms
Max Latency: 48.276 ms
50th percentile: 5.608 ms
90th percentile: 8.539 ms
99th percentile: 13.146 ms
------------------------------

Before adding timeout example

Benchmarking A records
Running 30s test @ https://localhost/dns-query
  4 threads and 10 connections
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    12.62ms   20.57ms 122.74ms   89.61%
    Req/Sec    26.16     25.31    80.00     59.38%
  176 requests in 30.09s, 50.76KB read
  Socket errors: connect 0, read 0, write 0, timeout 22
  Non-2xx or 3xx responses: 22
Requests/sec:      5.85
Transfer/sec:      1.69KB
------------------------------
Total Requests: 176
Avg. Latency: 12.618 ms
Max Latency: 122.741 ms
50th percentile: 5.741 ms
90th percentile: 36.813 ms
99th percentile: 119.865 ms
------------------------------
Benchmarking AAAA records
Running 30s test @ https://localhost/dns-query
  4 threads and 10 connections
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
Error: 408
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     9.01ms    8.70ms 157.28ms   89.66%
    Req/Sec   218.01     92.21   560.00     75.78%
  19697 requests in 30.10s, 6.25MB read
  Socket errors: connect 0, read 0, write 0, timeout 7
  Non-2xx or 3xx responses: 7
Requests/sec:    654.33
Transfer/sec:    212.67KB
------------------------------
Total Requests: 19697
Avg. Latency: 9.013 ms
Max Latency: 157.278 ms
50th percentile: 7.248 ms
90th percentile: 17.812 ms
99th percentile: 41.075 ms
------------------------------
sonhmai commented 4 days ago

interesting fact

interesting quote from no way to set read/write timeout for tokio's TcpStream #510

read / write timeouts are a synchronous socket concept.

btw if timeout is not set in the stdlib socket, read or write can block indefinitely. This is potentially for making the server hanged while using stdlib as well.

neitomic commented 4 days ago

Perfect