mirror of
https://github.com/OMGeeky/tarpc.git
synced 2025-12-29 07:40:14 +01:00
Rework the future Connect trait to only have one method, which takes an Options arg.
This commit is contained in:
@@ -24,7 +24,7 @@ use std::{cmp, thread};
|
||||
use std::sync::{Arc, mpsc};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::time::{Duration, Instant};
|
||||
use tarpc::future::{Connect};
|
||||
use tarpc::client::future::{Connect, Options};
|
||||
use tarpc::util::{FirstSocketAddr, Never};
|
||||
use tokio_core::reactor;
|
||||
|
||||
@@ -73,10 +73,10 @@ trait Microseconds {
|
||||
|
||||
impl Microseconds for Duration {
|
||||
fn microseconds(&self) -> i64 {
|
||||
chrono::Duration::from_std(*self)
|
||||
.unwrap()
|
||||
.num_microseconds()
|
||||
.unwrap()
|
||||
chrono::Duration::from_std(*self)
|
||||
.unwrap()
|
||||
.num_microseconds()
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,21 +101,25 @@ fn spawn_core() -> reactor::Remote {
|
||||
rx.recv().unwrap()
|
||||
}
|
||||
|
||||
fn run_once(clients: Vec<FutureClient>, concurrency: u32) -> impl Future<Item=(), Error=()> + 'static {
|
||||
fn run_once(clients: Vec<FutureClient>,
|
||||
concurrency: u32)
|
||||
-> impl Future<Item = (), Error = ()> + 'static {
|
||||
let start = Instant::now();
|
||||
let num_clients = clients.len();
|
||||
futures::stream::futures_unordered((0..concurrency as usize)
|
||||
.map(|iteration| (iteration + 1, iteration % num_clients))
|
||||
.map(|(iteration, client_idx)| {
|
||||
let client = &clients[client_idx];
|
||||
let start = Instant::now();
|
||||
debug!("Client {} reading (iteration {})...", client_idx, iteration);
|
||||
client.read(CHUNK_SIZE)
|
||||
.map(move |_| (client_idx, iteration, start))
|
||||
}))
|
||||
.map(|iteration| (iteration + 1, iteration % num_clients))
|
||||
.map(|(iteration, client_idx)| {
|
||||
let client = &clients[client_idx];
|
||||
let start = Instant::now();
|
||||
debug!("Client {} reading (iteration {})...", client_idx, iteration);
|
||||
client.read(CHUNK_SIZE)
|
||||
.map(move |_| (client_idx, iteration, start))
|
||||
}))
|
||||
.map(|(client_idx, iteration, start)| {
|
||||
let elapsed = start.elapsed();
|
||||
debug!("Client {} received reply (iteration {}).", client_idx, iteration);
|
||||
debug!("Client {} received reply (iteration {}).",
|
||||
client_idx,
|
||||
iteration);
|
||||
elapsed
|
||||
})
|
||||
.map_err(|e| panic!(e))
|
||||
@@ -128,31 +132,31 @@ fn run_once(clients: Vec<FutureClient>, concurrency: u32) -> impl Future<Item=()
|
||||
})
|
||||
.map(move |stats| {
|
||||
info!("{} requests => Mean={}µs, Min={}µs, Max={}µs, Total={}µs",
|
||||
stats.count,
|
||||
stats.sum.microseconds() as f64 / stats.count as f64,
|
||||
stats.min.unwrap().microseconds(),
|
||||
stats.max.unwrap().microseconds(),
|
||||
start.elapsed().microseconds());
|
||||
stats.count,
|
||||
stats.sum.microseconds() as f64 / stats.count as f64,
|
||||
stats.min.unwrap().microseconds(),
|
||||
stats.max.unwrap().microseconds(),
|
||||
start.elapsed().microseconds());
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = env_logger::init();
|
||||
let matches = App::new("Tarpc Concurrency")
|
||||
.about("Demonstrates making concurrent requests to a tarpc service.")
|
||||
.arg(Arg::with_name("concurrency")
|
||||
.short("c")
|
||||
.long("concurrency")
|
||||
.value_name("LEVEL")
|
||||
.help("Sets a custom concurrency level")
|
||||
.takes_value(true))
|
||||
.arg(Arg::with_name("clients")
|
||||
.short("n")
|
||||
.long("num_clients")
|
||||
.value_name("AMOUNT")
|
||||
.help("How many clients to distribute requests between")
|
||||
.takes_value(true))
|
||||
.get_matches();
|
||||
.about("Demonstrates making concurrent requests to a tarpc service.")
|
||||
.arg(Arg::with_name("concurrency")
|
||||
.short("c")
|
||||
.long("concurrency")
|
||||
.value_name("LEVEL")
|
||||
.help("Sets a custom concurrency level")
|
||||
.takes_value(true))
|
||||
.arg(Arg::with_name("clients")
|
||||
.short("n")
|
||||
.long("num_clients")
|
||||
.value_name("AMOUNT")
|
||||
.help("How many clients to distribute requests between")
|
||||
.takes_value(true))
|
||||
.get_matches();
|
||||
let concurrency = matches.value_of("concurrency")
|
||||
.map(&str::parse)
|
||||
.map(Result::unwrap)
|
||||
@@ -170,7 +174,7 @@ fn main() {
|
||||
.map(|i| (i, spawn_core()))
|
||||
.map(|(i, remote)| {
|
||||
info!("Client {} connecting...", i);
|
||||
FutureClient::connect_remotely(&addr, &remote)
|
||||
FutureClient::connect(addr, Options::default().remote(remote))
|
||||
.map_err(|e| panic!(e))
|
||||
})
|
||||
// Need an intermediate collection to connect the clients in parallel,
|
||||
|
||||
@@ -14,14 +14,14 @@ extern crate tokio_proto as tokio;
|
||||
|
||||
use futures::{BoxFuture, Future};
|
||||
use publisher::FutureServiceExt as PublisherExt;
|
||||
use subscriber::FutureServiceExt as SubscriberExt;
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use tarpc::future::Connect as Fc;
|
||||
use tarpc::sync::Connect as Sc;
|
||||
use subscriber::FutureServiceExt as SubscriberExt;
|
||||
use tarpc::client::future::{Connect as Fc, Options};
|
||||
use tarpc::client::sync::Connect as Sc;
|
||||
use tarpc::util::{FirstSocketAddr, Message, Never};
|
||||
|
||||
pub mod subscriber {
|
||||
@@ -57,12 +57,10 @@ impl subscriber::FutureService for Subscriber {
|
||||
|
||||
impl Subscriber {
|
||||
fn new(id: u32) -> SocketAddr {
|
||||
Subscriber {
|
||||
id: id,
|
||||
}
|
||||
.listen("localhost:0".first_socket_addr())
|
||||
.wait()
|
||||
.unwrap()
|
||||
Subscriber { id: id }
|
||||
.listen("localhost:0".first_socket_addr())
|
||||
.wait()
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,22 +86,21 @@ impl publisher::FutureService for Publisher {
|
||||
// Ignore failing subscribers.
|
||||
.map(move |client| client.receive(message.clone()).then(|_| Ok(())))
|
||||
.collect::<Vec<_>>())
|
||||
.map(|_| ())
|
||||
.boxed()
|
||||
.map(|_| ())
|
||||
.boxed()
|
||||
}
|
||||
|
||||
type SubscribeFut = BoxFuture<(), Message>;
|
||||
type SubscribeFut = Box<Future<Item = (), Error = Message>>;
|
||||
|
||||
fn subscribe(&self, id: u32, address: SocketAddr) -> Self::SubscribeFut {
|
||||
let clients = self.clients.clone();
|
||||
subscriber::FutureClient::connect(&address)
|
||||
Box::new(subscriber::FutureClient::connect(address, Options::default())
|
||||
.map(move |subscriber| {
|
||||
println!("Subscribing {}.", id);
|
||||
clients.lock().unwrap().insert(id, subscriber);
|
||||
()
|
||||
})
|
||||
.map_err(|e| e.to_string().into())
|
||||
.boxed()
|
||||
.map_err(|e| e.to_string().into()))
|
||||
}
|
||||
|
||||
type UnsubscribeFut = BoxFuture<(), Never>;
|
||||
|
||||
@@ -14,7 +14,7 @@ extern crate serde_derive;
|
||||
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use tarpc::sync::Connect;
|
||||
use tarpc::client::sync::Connect;
|
||||
|
||||
service! {
|
||||
rpc hello(name: String) -> String | NoNameGiven;
|
||||
|
||||
@@ -22,7 +22,7 @@ use bincode::serde::DeserializeError;
|
||||
use futures::{Future, IntoFuture};
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use tarpc::future::Connect;
|
||||
use tarpc::client::future::{Connect, Options};
|
||||
use tarpc::util::FirstSocketAddr;
|
||||
use tarpc::util::Never;
|
||||
use tokio_service::Service;
|
||||
@@ -31,9 +31,9 @@ use tokio_service::Service;
|
||||
struct HelloServer;
|
||||
|
||||
impl HelloServer {
|
||||
fn listen(addr: SocketAddr) -> impl Future<Item=SocketAddr, Error=io::Error> {
|
||||
fn listen(addr: SocketAddr) -> impl Future<Item = SocketAddr, Error = io::Error> {
|
||||
let (tx, rx) = futures::oneshot();
|
||||
tarpc::future::REMOTE.spawn(move |handle| {
|
||||
tarpc::REMOTE.spawn(move |handle| {
|
||||
Ok(tx.complete(tarpc::listen_with(addr, move || Ok(HelloServer), handle.clone())))
|
||||
});
|
||||
rx.map_err(|e| panic!(e)).and_then(|result| result)
|
||||
@@ -56,13 +56,13 @@ impl Service for HelloServer {
|
||||
pub struct FutureClient(tarpc::Client<String, String, Never>);
|
||||
|
||||
impl FutureClient {
|
||||
fn connect(addr: &SocketAddr) -> impl Future<Item = FutureClient, Error = io::Error> {
|
||||
tarpc::Client::connect_remotely(addr, &tarpc::future::REMOTE).map(FutureClient)
|
||||
fn connect(addr: SocketAddr) -> impl Future<Item = FutureClient, Error = io::Error> {
|
||||
tarpc::Client::connect(addr, Options::default()).map(FutureClient)
|
||||
}
|
||||
|
||||
pub fn hello(&self, name: String)
|
||||
-> impl Future<Item = String, Error = tarpc::Error<Never>> + 'static
|
||||
{
|
||||
pub fn hello(&self,
|
||||
name: String)
|
||||
-> impl Future<Item = String, Error = tarpc::Error<Never>> + 'static {
|
||||
self.0.call(name).then(|msg| msg.unwrap())
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,7 @@ fn main() {
|
||||
let _ = env_logger::init();
|
||||
let mut core = tokio_core::reactor::Core::new().unwrap();
|
||||
let addr = HelloServer::listen("localhost:10000".first_socket_addr()).wait().unwrap();
|
||||
let f = FutureClient::connect(&addr)
|
||||
let f = FutureClient::connect(addr)
|
||||
.map_err(tarpc::Error::from)
|
||||
.and_then(|client| {
|
||||
let resp1 = client.hello("Mom".to_string());
|
||||
|
||||
@@ -12,7 +12,7 @@ extern crate tarpc;
|
||||
extern crate tokio_core;
|
||||
|
||||
use futures::Future;
|
||||
use tarpc::future::Connect;
|
||||
use tarpc::client::future::{Connect, Options};
|
||||
use tarpc::util::{FirstSocketAddr, Never};
|
||||
use tokio_core::reactor;
|
||||
|
||||
@@ -34,11 +34,11 @@ impl FutureService for HelloServer {
|
||||
fn main() {
|
||||
let addr = "localhost:10000".first_socket_addr();
|
||||
let mut core = reactor::Core::new().unwrap();
|
||||
let handle = core.handle();
|
||||
HelloServer.listen_with(addr, core.handle()).unwrap();
|
||||
core.run(
|
||||
FutureClient::connect(&addr)
|
||||
core.run(FutureClient::connect(addr, Options::default().handle(handle))
|
||||
.map_err(tarpc::Error::from)
|
||||
.and_then(|client| client.hello("Mom".to_string()))
|
||||
.map(|resp| println!("{}", resp))
|
||||
).unwrap();
|
||||
.map(|resp| println!("{}", resp)))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ extern crate futures;
|
||||
#[macro_use]
|
||||
extern crate tarpc;
|
||||
|
||||
use tarpc::client::sync::Connect;
|
||||
use tarpc::util::Never;
|
||||
use tarpc::sync::Connect;
|
||||
|
||||
service! {
|
||||
rpc hello(name: String) -> String;
|
||||
|
||||
@@ -15,9 +15,9 @@ use add::{FutureService as AddFutureService, FutureServiceExt as AddExt};
|
||||
use double::{FutureService as DoubleFutureService, FutureServiceExt as DoubleExt};
|
||||
use futures::{BoxFuture, Future};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tarpc::client::future::{Connect as Fc, Options};
|
||||
use tarpc::client::sync::Connect as Sc;
|
||||
use tarpc::util::{FirstSocketAddr, Message, Never};
|
||||
use tarpc::future::Connect as Fc;
|
||||
use tarpc::sync::Connect as Sc;
|
||||
|
||||
pub mod add {
|
||||
service! {
|
||||
@@ -53,9 +53,7 @@ struct DoubleServer {
|
||||
|
||||
impl DoubleServer {
|
||||
fn new(client: add::FutureClient) -> Self {
|
||||
DoubleServer {
|
||||
client: Arc::new(Mutex::new(client)),
|
||||
}
|
||||
DoubleServer { client: Arc::new(Mutex::new(client)) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +73,7 @@ impl DoubleFutureService for DoubleServer {
|
||||
fn main() {
|
||||
let _ = env_logger::init();
|
||||
let add_addr = AddServer.listen("localhost:0".first_socket_addr()).wait().unwrap();
|
||||
let add_client = add::FutureClient::connect(&add_addr).wait().unwrap();
|
||||
let add_client = add::FutureClient::connect(add_addr, Options::default()).wait().unwrap();
|
||||
|
||||
let double = DoubleServer::new(add_client);
|
||||
let double_addr = double.listen("localhost:0".first_socket_addr()).wait().unwrap();
|
||||
|
||||
@@ -13,13 +13,13 @@ extern crate tarpc;
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
|
||||
use std::sync::Arc;
|
||||
use futures::Future;
|
||||
use std::io::{Read, Write, stdout};
|
||||
use std::net;
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time;
|
||||
use std::io::{Read, Write, stdout};
|
||||
use futures::Future;
|
||||
use tarpc::sync::Connect;
|
||||
use tarpc::client::sync::Connect;
|
||||
use tarpc::util::{FirstSocketAddr, Never};
|
||||
|
||||
lazy_static! {
|
||||
|
||||
@@ -17,8 +17,8 @@ extern crate futures;
|
||||
use bar::FutureServiceExt as BarExt;
|
||||
use baz::FutureServiceExt as BazExt;
|
||||
use futures::Future;
|
||||
use tarpc::client::sync::Connect;
|
||||
use tarpc::util::{FirstSocketAddr, Never};
|
||||
use tarpc::sync::Connect;
|
||||
|
||||
mod bar {
|
||||
service! {
|
||||
|
||||
Reference in New Issue
Block a user