comparison src/lib.rs @ 29:efc69e99db70

Set socket to nonblocking before passing it to tokio.
author Paul Fisher <paul@pfish.zone>
date Sat, 19 Apr 2025 01:41:25 -0400
parents cfef4593e207
children
comparison
equal deleted inserted replaced
28:cfef4593e207 29:efc69e99db70
368 368
369 /// Creates `tokio::net::TcpListener` 369 /// Creates `tokio::net::TcpListener`
370 /// 370 ///
371 /// To be specific, it binds the socket or converts systemd socket to `tokio` 1.0 socket. 371 /// To be specific, it binds the socket or converts systemd socket to `tokio` 1.0 socket.
372 /// 372 ///
373 /// This method either `binds` the socket, if the address was provided or uses systemd socket 373 /// This method either `bind`s the socket, if the address was provided or uses systemd socket
374 /// if the socket name was provided. 374 /// if the socket name was provided.
375 #[cfg(feature = "tokio")] 375 #[cfg(feature = "tokio")]
376 pub async fn bind_tokio(self) -> Result<tokio::net::TcpListener, TokioBindError> { 376 pub async fn bind_tokio(self) -> Result<tokio::net::TcpListener, TokioBindError> {
377 match self.0 { 377 match self.0 {
378 SocketAddrInner::Ordinary(addr) => match tokio::net::TcpListener::bind(addr).await { 378 SocketAddrInner::Ordinary(addr) => match tokio::net::TcpListener::bind(addr).await {
389 )), 389 )),
390 } 390 }
391 } 391 }
392 SocketAddrInner::Systemd(socket_name) => { 392 SocketAddrInner::Systemd(socket_name) => {
393 let (socket, addr) = Self::get_systemd(socket_name, true)?; 393 let (socket, addr) = Self::get_systemd(socket_name, true)?;
394 socket 394 Self::wrap_tokio(socket)
395 .try_into()
396 .map_err(|error| TokioConversionError { addr, error }.into()) 395 .map_err(|error| TokioConversionError { addr, error }.into())
397 } 396 }
398 SocketAddrInner::SystemdNoPrefix(socket_name) => { 397 SocketAddrInner::SystemdNoPrefix(socket_name) => {
399 let (socket, addr) = Self::get_systemd(socket_name, false)?; 398 let (socket, addr) = Self::get_systemd(socket_name, false)?;
400 socket 399 Self::wrap_tokio(socket)
401 .try_into()
402 .map_err(|error| TokioConversionError { addr, error }.into()) 400 .map_err(|error| TokioConversionError { addr, error }.into())
403 } 401 }
404 } 402 }
403 }
404
405 #[cfg(feature = "tokio")]
406 fn wrap_tokio(socket: std::net::TcpListener) -> Result<tokio::net::TcpListener, std::io::Error> {
407 socket.set_nonblocking(true)?;
408 socket.try_into()
405 } 409 }
406 410
407 /// Creates `tokio::net::TcpListener` 411 /// Creates `tokio::net::TcpListener`
408 /// 412 ///
409 /// To be specific, it binds the socket or converts systemd socket to `tokio` 0.2 socket. 413 /// To be specific, it binds the socket or converts systemd socket to `tokio` 0.2 socket.