#listen #tcp #bind #tokio #resources #network-programming

tk-listen

A set of helper futures allowing to listen TCP (or unix) socket with resource limits and proper error handling

4 releases

Uses old Rust 2015

0.2.1 Dec 15, 2018
0.2.0 May 24, 2018
0.1.1 Oct 19, 2017
0.1.0 Mar 16, 2017

#675 in Asynchronous

Download history 462/week @ 2024-03-14 644/week @ 2024-03-21 776/week @ 2024-03-28 525/week @ 2024-04-04 661/week @ 2024-04-11 614/week @ 2024-04-18 591/week @ 2024-04-25 545/week @ 2024-05-02 690/week @ 2024-05-09 732/week @ 2024-05-16 767/week @ 2024-05-23 601/week @ 2024-05-30 589/week @ 2024-06-06 512/week @ 2024-06-13 617/week @ 2024-06-20 444/week @ 2024-06-27

2,279 downloads per month
Used in 25 crates (10 directly)

MIT/Apache

23KB
272 lines

Tokio Listen Helpers

Status: Beta

Documentation | Github | Crate

A library that allows to listen network sockets with proper resource limits and error handling.

Basic challenges:

  • Some connection accept errors (like "connection reset") must be ignored, some (like "too many files open") may consume 100% CPU when ignored. You need to know what to do with them every time
  • Server must accept connections up to a certain limit to avoid DoS attacks
  • Shutting down listener and update the set of addresses listened should be obvious to implement

Example

Here is the basic example:


let TIME_TO WAIT_ON_ERROR = Duration::from_millis(100);
let MAX_SIMULTANEOUS_CONNECTIONS = 1000;

let mut lp = Core::new().unwrap();
let listener = TcpListener::bind(&addr, &lp.handle()).unwrap();
lp.run(
    listener.incoming()
    .sleep_on_error(TIME_TO_WAIT_ON_ERROR, &h2)
    .map(move |(mut socket, _addr)| {
         // Your future is here:
         Proto::new(socket)
         // Errors should not pass silently
         // common idea is to log them
         .map_err(|e| error!("Protocol error: {}", e))
    })
    .listen(MAX_SIMULTANEOUS_CONNECTIONS)
).unwrap(); // stream doesn't end in this case

More in docs and examples

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~3.5MB
~55K SLoC