rust-tokio

How to bind() on TCP client side in rust/tokio?

扶醉桌前 提交于 2020-08-08 06:04:38
问题 I have a need to make sure the client side of the TCP connection goes through a particular (IP) interface. The standard way would be to bind() the socket to the IP:0 , before the connect() . I started looking at tokio::net::TcpStream::connect() and friends, which doesn't seem to have a way to do this. I took a step back and looked at std::net::TcpStream , which doesn't have it either. Am I missing something, or do I need to go through some lower level APIs? 回答1: The best way of doing this at

How to bind() on TCP client side in rust/tokio?

那年仲夏 提交于 2020-08-08 06:03:23
问题 I have a need to make sure the client side of the TCP connection goes through a particular (IP) interface. The standard way would be to bind() the socket to the IP:0 , before the connect() . I started looking at tokio::net::TcpStream::connect() and friends, which doesn't seem to have a way to do this. I took a step back and looked at std::net::TcpStream , which doesn't have it either. Am I missing something, or do I need to go through some lower level APIs? 回答1: The best way of doing this at

What is the best way to convert an AsyncRead to a TryStream of bytes?

廉价感情. 提交于 2020-08-02 04:18:26
问题 I have an AsyncRead and want to convert it to a Stream<Item = tokio::io::Result<Bytes>> with tokio 0.2 and futures 0.3. The best I've been able to do is something like: use bytes::Bytes; // 0.4.12 use futures::stream::{Stream, TryStreamExt};; // 0.3.1 use tokio::{fs::File, io::Result}; // 0.2.4 use tokio_util::{BytesCodec, FramedRead}; // 0.2.0 #[tokio::main] async fn main() -> Result<()> { let file = File::open("some_file.txt").await?; let stream = FramedRead::new(file, BytesCodec::new())

How to create a dedicated threadpool for CPU-intensive work in Tokio?

馋奶兔 提交于 2020-07-28 04:20:09
问题 I have a Rust async server based on the Tokio runtime. It has to process a mix of latency-sensitive I/O-bound requests, and heavy CPU-bound requests. I don't want to let the CPU-bound tasks monopolize the Tokio runtime and starve the I/O bound tasks, so I'd like to offload the CPU-bound tasks to a dedicated, isolated threadpool (isolation is the key here, so spawn_blocking / block_in_place on one shared threadpool are insufficient). How can I create such a threadpool in Tokio? A naive

How to create a dedicated threadpool for CPU-intensive work in Tokio?

蓝咒 提交于 2020-07-28 04:20:09
问题 I have a Rust async server based on the Tokio runtime. It has to process a mix of latency-sensitive I/O-bound requests, and heavy CPU-bound requests. I don't want to let the CPU-bound tasks monopolize the Tokio runtime and starve the I/O bound tasks, so I'd like to offload the CPU-bound tasks to a dedicated, isolated threadpool (isolation is the key here, so spawn_blocking / block_in_place on one shared threadpool are insufficient). How can I create such a threadpool in Tokio? A naive

How to store an invariant type variable in Rust

人盡茶涼 提交于 2020-07-10 06:47:08
问题 I would like to parse the type of each value in a row of data from tokio-postgresql Here is an example of getting a single value for a row of data from postgresql: ... let rows = client .query("select * from ExampleTable;") .await?; // This is how you read a string if you know the first column is a string type. let thisValue: &str = rows[0].get(0); In this example, it is known at design-time that the type in the first column is a string, and therefore the type for thisValue is &str . I would

How to store an invariant type variable in Rust

时光毁灭记忆、已成空白 提交于 2020-07-10 06:46:27
问题 I would like to parse the type of each value in a row of data from tokio-postgresql Here is an example of getting a single value for a row of data from postgresql: ... let rows = client .query("select * from ExampleTable;") .await?; // This is how you read a string if you know the first column is a string type. let thisValue: &str = rows[0].get(0); In this example, it is known at design-time that the type in the first column is a string, and therefore the type for thisValue is &str . I would

How do I spawn many cancellable timers using Tokio?

隐身守侯 提交于 2020-07-09 03:17:30
问题 How do I use Tokio to implement a fixed number of timers that are regularly reset and canceled across threads? When a timer expires, a callback will be executed. An API similar to that of Go's time.AfterFunc is essentially what I desire: package main import ( "fmt" "time" ) func main() { t := time.AfterFunc(time.Hour, func() { // happens every 2 seconds with 1 second delay fmt.Println("fired") }) for { t.Reset(time.Second) time.Sleep(time.Second * 2) } } The only crate I've found that