Crates.io | tcp-connect |
lib.rs | tcp-connect |
version | 0.1.0 |
source | src |
created_at | 2024-09-16 20:25:57.560551 |
updated_at | 2024-09-16 20:25:57.560551 |
description | TCP connect is a replacment of the `std::net::TcpStream::connect` with DNS caching |
homepage | |
repository | https://github.com/pfreixes/tcp-connect |
max_upload_size | |
id | 1376837 |
size | 34,160 |
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");
}