#tcp-connection #dns-resolution #tcp-stream #ip-address #cache #connect #time

tcp-connect

TCP connect is a replacment of the std::net::TcpStream::connect with DNS caching

1 unstable release

0.1.0 Sep 16, 2024

#18 in #tcp-stream

24 downloads per month

MIT license

33KB
686 lines

TCP connect is a ~ drop in replacment of the std::net::TcpStream::connect with DNS caching.

TCPConnect allows you to cache the DNS resolution result for a configured TTL, once cached subsequent calls will pick a random IP adddress from the original list of IP addresses returned, with the goal of distributing connections among all IP addresses.

During cache miss stampede events TCPConnect will make sure that only one concurrent DNS resolution is allowed per host, queing other calls for the same host. This would reduce drastically the number of DNS resolutions.

In the example below, a TCPConnect is built and used to further connect to any host using a DNS TTL of 60 seconds and a maximum stale time of 1 second.

use std::time::Duration;
use tcp_connect::*;
use std::io::Write;

fn main() {
   let tcp_connect = TCPConnect::builder()
       .dns_ttl(Duration::from_secs(60))
       .dns_max_stale_time(Duration::from_secs(1))
       .build();

   tcp_connect.connect("localhost:80");
}

Dependencies

~310KB