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

Allow running SharedConnection with any other runtime #229

Merged
merged 3 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 20 additions & 15 deletions src/aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ use std::net::ToSocketAddrs;
use tokio_uds::UnixStream;

use tokio_codec::{Decoder, Framed};
use tokio_executor;
use tokio_io::{self, AsyncWrite};
use tokio_tcp::TcpStream;

use futures::future::Either;
use futures::future::{Either, Executor};
use futures::{future, Async, AsyncSink, Future, Poll, Sink, StartSend, Stream};
use tokio_sync::{mpsc, oneshot};

Expand Down Expand Up @@ -431,18 +430,21 @@ where
T::Error: Send,
T::Error: ::std::fmt::Debug,
{
fn new(sink_stream: T) -> Self {
fn new<E>(sink_stream: T, executor: E) -> Self
where
E: Executor<Box<dyn Future<Item = (), Error = ()> + Send>>,
{
const BUFFER_SIZE: usize = 50;
let (sender, receiver) = mpsc::channel(BUFFER_SIZE);
tokio_executor::spawn(
receiver
.map_err(|_| ())
.forward(PipelineSink {
sink_stream,
in_flight: VecDeque::new(),
})
.map(|_| ()),
);
let f = receiver
.map_err(|_| ())
.forward(PipelineSink {
sink_stream,
in_flight: VecDeque::new(),
})
.map(|_| ());

executor.execute(Box::new(f));
Pipeline(sender)
}

Expand Down Expand Up @@ -498,17 +500,20 @@ pub struct SharedConnection {
}

impl SharedConnection {
pub fn new(con: Connection) -> impl Future<Item = Self, Error = RedisError> {
pub fn new<E>(con: Connection, executor: E) -> impl Future<Item = Self, Error = RedisError>
where
E: Executor<Box<dyn Future<Item = (), Error = ()> + Send>>,
{
future::lazy(|| {
let pipeline = match con.con {
ActualConnection::Tcp(tcp) => {
let codec = ValueCodec::default().framed(tcp.into_inner());
ActualPipeline::Tcp(Pipeline::new(codec))
ActualPipeline::Tcp(Pipeline::new(codec, executor))
}
#[cfg(unix)]
ActualConnection::Unix(unix) => {
let codec = ValueCodec::default().framed(unix.into_inner());
ActualPipeline::Unix(Pipeline::new(codec))
ActualPipeline::Unix(Pipeline::new(codec, executor))
}
};
Ok(SharedConnection {
Expand Down
15 changes: 14 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use futures::Future;

use connection::{connect, Connection, ConnectionInfo, ConnectionLike, IntoConnectionInfo};
use futures::future::Executor;
use types::{RedisError, RedisResult, Value};

/// The client type.
Expand Down Expand Up @@ -53,8 +54,20 @@ impl Client {
pub fn get_shared_async_connection(
&self,
) -> impl Future<Item = ::aio::SharedConnection, Error = RedisError> {
let executor = tokio_executor::DefaultExecutor::current();
self.get_async_connection()
.and_then(move |con| ::aio::SharedConnection::new(con))
.and_then(move |con| ::aio::SharedConnection::new(con, executor))
}

pub fn get_shared_async_connection_with_executor<E>(
&self,
executor: E,
) -> impl Future<Item = ::aio::SharedConnection, Error = RedisError>
where
E: Executor<Box<dyn Future<Item = (), Error = ()> + Send>>,
{
self.get_async_connection()
.and_then(move |con| ::aio::SharedConnection::new(con, executor))
}
}

Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ extern crate sha1;
extern crate url;
#[macro_use]
extern crate futures;
extern crate tokio_executor;
#[macro_use]
extern crate tokio_io;
extern crate tokio_codec;
Expand Down