Crates.io | tokio-listener |
lib.rs | tokio-listener |
version | 0.4.3 |
source | src |
created_at | 2023-07-30 19:02:42.737868 |
updated_at | 2024-07-09 14:43:29.159936 |
description | Add flexibility in a way of accepting connections: unix sockets, socket activation, inetd mode to Tokio-based projects. |
homepage | |
repository | https://github.com/vi/tokio-listener |
max_upload_size | |
id | 930193 |
size | 162,611 |
Web service projects created using Hyper or Axum Rust frameworks typically allow users to specify TCP port and host to bind to in order to listen for incoming connetions. While it is a solid default choice, sometimes more flexibility is desired, especially on Linux.
tokio-listener
allows to add this flexibility by offering special abstract types ListenerAddress
and Listener
instead of typical SocketAddr
and TcpListener
, allowing adding this flexibility to projects in easy way.
.
or /
.@
.sd-listen
. You can also request a specific named or all sockets.clap
integration - custom address can be added as a Clap field (due to FromStr impl). Other options can be included by clap(flatten)
, which also brings short documentation to the CLI help message. Alternatively, the whole set of primary address and additional options can be brought in using ListenerAddressPositional
or ListenerAddressLFlag
helper types.serde
intergration - custom address type behaves like a string with respect to Serde. Other options can also be serialized or deserialized.axum 0.7
integration.sd-listen:*
address. Not enabled by default.TcpListener
.hyper
directly instead of Axum.See crate docs for API reference and some other examples.
Given this series of invocations:
target/debug/examples/clap_axum07 127.0.0.1:8080 $'Hello from usual mode\n'
target/debug/examples/clap_axum07 ./path_socket $'Hello from UNIX socket path mode\n'
target/debug/examples/clap_axum07 @abstract_socket $'Hello from UNIX socket abstract mode\n'
systemd-socket-activate -l 8081 target/debug/examples/clap_axum07 sd-listen $'Hello from pre-listened socket\n'
systemd-socket-activate --inetd -al 8082 target/debug/examples/clap_axum07 inetd $'Hello from inetd mode\n'
systemd-socket-activate -l 8083 -l 8084 --fdname=foo:bar -- target/debug/examples/clap_axum07 sd-listen:bar $'Hello from a named pre-listened socket\n'
systemd-socket-activate -l 8085 -l 8086 -- target/debug/examples/clap_axum07 sd-listen:* $'Hello from any of the two pre-listened sockets\n'
and this Caddyfile:
{
admin off
}
:4000
handle_path /tcp/* {
reverse_proxy 127.0.0.1:8080
}
handle_path /unix/* {
reverse_proxy unix/./path_socket
}
handle_path /abstract/* {
reverse_proxy unix/@abstract_socket
}
handle_path /sdlisten/* {
reverse_proxy 127.0.0.1:8081
}
handle_path /inetd/* {
reverse_proxy 127.0.0.1:8082
}
you can see that effectively the same service can be accessed in multiple ways:
$ curl http://127.0.0.1:4000/tcp/
Hello from usual mode
$ curl http://127.0.0.1:4000/unix/
Hello from UNIX socket path mode
$ curl http://127.0.0.1:4000/abstract/
Hello from UNIX socket abstract mode
$ curl http://127.0.0.1:4000/sdlisten/
Hello from a pre-listened socket
$ curl http://127.0.0.1:4000/inetd/
Hello from inetd
$ curl --unix ./path_socket http://q/
Hello from UNIX socket path mode
$ curl --abstract-unix abstract_socket http://q/
Hello from UNIX socket abstract mode
Demo application for tokio-listener
Usage: clap_axum [OPTIONS] <LISTEN_ADDRESS> <TEXT_TO_SERVE>
Arguments:
<LISTEN_ADDRESS>
Socket address to listen for incoming connections.
Various types of addresses are supported:
* TCP socket address and port, like 127.0.0.1:8080 or [::]:80
* UNIX socket path like /tmp/mysock or Linux abstract address like @abstract
* Special keyword "inetd" for serving one connection from stdin/stdout
* Special keyword "sd-listen" to accept connections from file descriptor 3 (e.g. systemd socket activation).
You can also specify a named descriptor after a colon or * to use all passed sockets (if this feature is enabled).
<TEXT_TO_SERVE>
Line of text to return as a body of incoming requests
Options:
--unix-listen-unlink
remove UNIX socket prior to binding to it
--unix-listen-chmod <UNIX_LISTEN_CHMOD>
change filesystem mode of the newly bound UNIX socket to `owner`, `group` or `everybody`
--unix-listen-uid <UNIX_LISTEN_UID>
change owner user of the newly bound UNIX socket to this numeric uid
--unix-listen-gid <UNIX_LISTEN_GID>
change owner group of the newly bound UNIX socket to this numeric uid
--sd-accept-ignore-environment
ignore environment variables like LISTEN_PID or LISTEN_FDS and unconditionally use file descritor `3` as a socket in sd-listen or sd-listen-unix modes
--tcp-keepalive <TCP_KEEPALIVE>
set SO_KEEPALIVE settings for each accepted TCP connection.
Value is a colon-separated triplet of time_ms:count:interval_ms, each of which is optional.
--tcp-reuse-port
Try to set SO_REUSEPORT, so that multiple processes can accept connections from the same port in a round-robin fashion
--recv-buffer-size <RECV_BUFFER_SIZE>
Set socket's SO_RCVBUF value
--send-buffer-size <SEND_BUFFER_SIZE>
Set socket's SO_SNDBUF value
--tcp-only-v6
Set socket's IPV6_V6ONLY to true, to avoid receiving IPv4 connections on IPv6 socket
--tcp-listen-backlog <TCP_LISTEN_BACKLOG>
Maximum number of pending unaccepted connections
-h, --help
Print help (see a summary with '-h')
All this can be brought in with just one #[clap(flatten)] addr: tokio_listener::ListenerAddressPositional
.