Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add net feature for compiling to embedded targets. #1579

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions redis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ pin-project-lite = { version = "0.2", optional = true }
tokio-util = { version = "0.7", optional = true }
tokio = { version = "1", features = [
"rt",
"net",
"time",
"sync",
], optional = true }
socket2 = { version = "0.5", features = ["all"] }
socket2 = { version = "0.5", features = ["all"], optional = true }

# Only needed for the connection manager
arc-swap = { version = "1.7.1" }
Expand Down Expand Up @@ -108,14 +107,16 @@ hashbrown = { version = "0.15", optional = true }
lru = { version = "0.13", optional = true }

[features]
default = ["acl", "streams", "geospatial", "script", "keep-alive"]
default = ["acl", "streams", "geospatial", "script", "keep-alive", "net"]
net = ["dep:socket2", "tokio/net"]
acl = []
geospatial = []
json = ["dep:serde", "serde/derive", "dep:serde_json"]
cluster = ["dep:crc16", "dep:rand"]
script = ["dep:sha1_smol"]
tls-native-tls = ["dep:native-tls"]
tls-native-tls = ["net", "dep:native-tls"]
tls-rustls = [
"net",
"dep:rustls",
"rustls/std",
"rustls/ring",
Expand Down
22 changes: 21 additions & 1 deletion redis/src/aio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
//! Adds async IO support to redis.
use crate::cmd::Cmd;
#[cfg(feature = "net")]
use crate::connection::{
check_connection_setup, connection_setup_pipeline, AuthResult, ConnectionSetupComponents,
RedisConnectionInfo,
};
use crate::types::{RedisFuture, RedisResult, Value};
#[cfg(feature = "net")]
use crate::types::RedisResult;
use crate::types::{RedisFuture, Value};
use crate::PushInfo;
use ::tokio::io::{AsyncRead, AsyncWrite};
#[cfg(feature = "net")]
use futures_util::Future;
#[cfg(feature = "net")]
use std::net::SocketAddr;
#[cfg(unix)]
use std::path::Path;
#[cfg(feature = "net")]
use std::pin::Pin;

/// Enables the async_std compatibility
Expand All @@ -26,12 +32,16 @@ use crate::connection::TlsConnParams;
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-comp")))]
pub mod tokio;

#[cfg(feature = "net")]
mod pubsub;
#[cfg(feature = "net")]
pub use pubsub::{PubSub, PubSubSink, PubSubStream};

/// Represents the ability of connecting via TCP or via Unix socket
#[cfg(feature = "net")]
pub(crate) trait RedisRuntime: AsyncStream + Send + Sync + Sized + 'static {
/// Performs a TCP connection
#[cfg(feature = "net")]
async fn connect_tcp(
socket_addr: SocketAddr,
tcp_settings: &crate::io::tcp::TcpSettings,
Expand Down Expand Up @@ -90,6 +100,7 @@ pub trait ConnectionLike {
fn get_db(&self) -> i64;
}

#[cfg(feature = "net")]
async fn execute_connection_pipeline(
rv: &mut impl ConnectionLike,
(pipeline, instructions): (crate::Pipeline, ConnectionSetupComponents),
Expand All @@ -104,6 +115,7 @@ async fn execute_connection_pipeline(
}

// Initial setup for every connection.
#[cfg(feature = "net")]
async fn setup_connection(
connection_info: &RedisConnectionInfo,
con: &mut impl ConnectionLike,
Expand Down Expand Up @@ -136,18 +148,25 @@ async fn setup_connection(
Ok(())
}

#[cfg(feature = "net")]
mod connection;
#[cfg(feature = "net")]
pub use connection::*;
#[cfg(feature = "net")]
mod multiplexed_connection;
#[cfg(feature = "net")]
pub use multiplexed_connection::*;
#[cfg(feature = "connection-manager")]
mod connection_manager;
#[cfg(feature = "connection-manager")]
#[cfg_attr(docsrs, doc(cfg(feature = "connection-manager")))]
pub use connection_manager::*;
#[cfg(feature = "net")]
mod runtime;
#[cfg(feature = "net")]
pub(super) use runtime::*;

#[cfg(feature = "net")]
macro_rules! check_resp3 {
($protocol: expr) => {
use crate::types::ProtocolVersion;
Expand All @@ -170,6 +189,7 @@ macro_rules! check_resp3 {
};
}

#[cfg(feature = "net")]
pub(crate) use check_resp3;

/// An error showing that the receiver
Expand Down
30 changes: 19 additions & 11 deletions redis/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#[cfg(feature = "net")]
use std::time::Duration;

#[cfg(feature = "aio")]
#[cfg(all(feature = "aio", feature = "net"))]
use crate::aio::AsyncPushSender;
#[cfg(feature = "aio")]
#[cfg(feature = "net")]
use crate::connection::{connect, Connection, ConnectionInfo, ConnectionLike, IntoConnectionInfo};
#[cfg(all(feature = "aio", feature = "net"))]
use crate::io::tcp::TcpSettings;
use crate::{
connection::{connect, Connection, ConnectionInfo, ConnectionLike, IntoConnectionInfo},
types::{RedisResult, Value},
};
#[cfg(feature = "aio")]
#[cfg(feature = "net")]
use crate::types::{RedisResult, Value};
#[cfg(all(feature = "aio", feature = "net"))]
use std::pin::Pin;

#[cfg(feature = "tls-rustls")]
Expand All @@ -20,6 +21,7 @@ use crate::caching::CacheConfig;
use crate::caching::CacheManager;

/// The client type.
#[cfg(feature = "net")]
#[derive(Debug, Clone)]
pub struct Client {
pub(crate) connection_info: ConnectionInfo,
Expand All @@ -41,6 +43,7 @@ pub struct Client {
/// let client = redis::Client::open("redis://127.0.0.1/").unwrap();
/// let con = client.get_connection().unwrap();
/// ```
#[cfg(feature = "net")]
impl Client {
/// Connects to a redis server and returns a client. This does not
/// actually open a connection yet but it does perform some basic
Expand Down Expand Up @@ -172,7 +175,7 @@ pub(crate) enum Cache {
}

/// Options for creation of async connection
#[cfg(feature = "aio")]
#[cfg(all(feature = "aio", feature = "net"))]
#[derive(Clone, Default)]
pub struct AsyncConnectionConfig {
/// Maximum time to wait for a response from the server
Expand All @@ -185,7 +188,7 @@ pub struct AsyncConnectionConfig {
pub(crate) tcp_settings: TcpSettings,
}

#[cfg(feature = "aio")]
#[cfg(all(feature = "aio", feature = "net"))]
impl AsyncConnectionConfig {
/// Creates a new instance of the options with nothing set
pub fn new() -> Self {
Expand Down Expand Up @@ -265,7 +268,7 @@ impl AsyncConnectionConfig {

/// To enable async support you need to chose one of the supported runtimes and active its
/// corresponding feature: `tokio-comp` or `async-std-comp`
#[cfg(feature = "aio")]
#[cfg(all(feature = "aio", feature = "net"))]
#[cfg_attr(docsrs, doc(cfg(feature = "aio")))]
impl Client {
/// Returns an async connection from the client.
Expand Down Expand Up @@ -786,6 +789,7 @@ impl Client {
crate::aio::ConnectionManager::new_with_config(self.clone(), config).await
}

#[cfg(feature = "net")]
async fn get_multiplexed_async_connection_inner<T>(
&self,
config: &AsyncConnectionConfig,
Expand All @@ -801,6 +805,7 @@ impl Client {
Ok(connection)
}

#[cfg(feature = "net")]
async fn create_multiplexed_async_connection_inner<T>(
&self,
config: &AsyncConnectionConfig,
Expand All @@ -822,6 +827,7 @@ impl Client {
.await
}

#[cfg(feature = "net")]
async fn get_simple_async_connection_dynamically(
&self,
tcp_settings: &TcpSettings,
Expand All @@ -841,6 +847,7 @@ impl Client {
}
}

#[cfg(feature = "net")]
async fn get_simple_async_connection<T>(
&self,
tcp_settings: &TcpSettings,
Expand Down Expand Up @@ -882,9 +889,10 @@ impl Client {
}
}

#[cfg(feature = "aio")]
#[cfg(all(feature = "aio", feature = "net"))]
use crate::aio::Runtime;

#[cfg(feature = "net")]
impl ConnectionLike for Client {
fn req_packed_command(&mut self, cmd: &[u8]) -> RedisResult<Value> {
self.get_connection()?.req_packed_command(cmd)
Expand Down
7 changes: 6 additions & 1 deletion redis/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::cmd::{cmd, Cmd, Iter};
use crate::connection::{Connection, ConnectionLike, Msg};
#[cfg(feature = "net")]
use crate::connection::Connection;
use crate::connection::{ConnectionLike, Msg};
use crate::pipeline::Pipeline;
use crate::types::{
ExistenceCheck, ExpireOption, Expiry, FromRedisValue, NumericBehavior, RedisResult, RedisWrite,
Expand Down Expand Up @@ -30,6 +32,7 @@ use crate::streams;

#[cfg(feature = "acl")]
use crate::acl;
#[cfg(feature = "net")]
use crate::RedisConnectionInfo;

#[cfg(any(feature = "cluster", feature = "cache-aio"))]
Expand Down Expand Up @@ -2331,6 +2334,7 @@ impl<T> Commands for T where T: ConnectionLike {}
#[cfg(feature = "aio")]
impl<T> AsyncCommands for T where T: crate::aio::ConnectionLike + Send + Sync + Sized {}

#[cfg(feature = "net")]
impl PubSubCommands for Connection {
fn subscribe<C, F, U>(&mut self, channels: C, mut func: F) -> RedisResult<U>
where
Expand Down Expand Up @@ -2683,6 +2687,7 @@ impl ToRedisArgs for FlushAllOptions {
pub type FlushDbOptions = FlushAllOptions;

/// Creates HELLO command for RESP3 with RedisConnectionInfo
#[cfg(feature = "net")]
pub fn resp3_hello(connection_info: &RedisConnectionInfo) -> Cmd {
let mut hello_cmd = cmd("HELLO");
hello_cmd.arg("3");
Expand Down
Loading
Loading