mirror of
https://github.com/OMGeeky/tarpc.git
synced 2026-01-10 13:34:03 +01:00
Format code with rustfmt
This commit is contained in:
@@ -21,7 +21,8 @@ use syn::{
|
||||
punctuated::Punctuated,
|
||||
spanned::Spanned,
|
||||
token::Comma,
|
||||
ArgCaptured, Attribute, FnArg, Ident, Lit, LitBool, MetaNameValue, Pat, ReturnType, Token, Visibility,
|
||||
ArgCaptured, Attribute, FnArg, Ident, Lit, LitBool, MetaNameValue, Pat, ReturnType, Token,
|
||||
Visibility,
|
||||
};
|
||||
|
||||
struct Service {
|
||||
@@ -54,14 +55,17 @@ impl Parse for Service {
|
||||
if rpc.ident == "new" {
|
||||
return Err(syn::Error::new(
|
||||
rpc.ident.span(),
|
||||
format!("method name conflicts with generated fn `{}Client::new`", ident)
|
||||
))
|
||||
format!(
|
||||
"method name conflicts with generated fn `{}Client::new`",
|
||||
ident
|
||||
),
|
||||
));
|
||||
}
|
||||
if rpc.ident == "serve" {
|
||||
return Err(syn::Error::new(
|
||||
rpc.ident.span(),
|
||||
format!("method name conflicts with generated fn `{}::serve`", ident)
|
||||
))
|
||||
format!("method name conflicts with generated fn `{}::serve`", ident),
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(Service {
|
||||
@@ -133,29 +137,29 @@ struct DeriveSerde(bool);
|
||||
impl Parse for DeriveSerde {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
if input.is_empty() {
|
||||
return Ok(DeriveSerde(cfg!(feature = "serde1")))
|
||||
return Ok(DeriveSerde(cfg!(feature = "serde1")));
|
||||
}
|
||||
match input.parse::<MetaNameValue>()? {
|
||||
MetaNameValue { ref ident, ref lit, .. } if ident == "derive_serde" => {
|
||||
match lit {
|
||||
Lit::Bool(LitBool{value: true, ..}) if cfg!(feature = "serde1") => Ok(DeriveSerde(true)),
|
||||
Lit::Bool(LitBool{value: true, ..}) => Err(syn::Error::new(
|
||||
lit.span(),
|
||||
"To enable serde, first enable the `serde1` feature of tarpc",
|
||||
)),
|
||||
Lit::Bool(LitBool{value: false, ..}) => Ok(DeriveSerde(false)),
|
||||
lit => Err(syn::Error::new(
|
||||
lit.span(),
|
||||
"`derive_serde` expects a value of type `bool`",
|
||||
)),
|
||||
MetaNameValue {
|
||||
ref ident, ref lit, ..
|
||||
} if ident == "derive_serde" => match lit {
|
||||
Lit::Bool(LitBool { value: true, .. }) if cfg!(feature = "serde1") => {
|
||||
Ok(DeriveSerde(true))
|
||||
}
|
||||
}
|
||||
MetaNameValue { ident, .. } => {
|
||||
Err(syn::Error::new(
|
||||
ident.span(),
|
||||
"tarpc::service only supports one meta item, `derive_serde = {bool}`",
|
||||
))
|
||||
}
|
||||
Lit::Bool(LitBool { value: true, .. }) => Err(syn::Error::new(
|
||||
lit.span(),
|
||||
"To enable serde, first enable the `serde1` feature of tarpc",
|
||||
)),
|
||||
Lit::Bool(LitBool { value: false, .. }) => Ok(DeriveSerde(false)),
|
||||
lit => Err(syn::Error::new(
|
||||
lit.span(),
|
||||
"`derive_serde` expects a value of type `bool`",
|
||||
)),
|
||||
},
|
||||
MetaNameValue { ident, .. } => Err(syn::Error::new(
|
||||
ident.span(),
|
||||
"tarpc::service only supports one meta item, `derive_serde = {bool}`",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ use std::{
|
||||
Arc,
|
||||
},
|
||||
};
|
||||
use trace::SpanId;
|
||||
use tokio_timer::{timeout, Timeout};
|
||||
use trace::SpanId;
|
||||
|
||||
use super::{Config, NewClient};
|
||||
|
||||
@@ -187,12 +187,10 @@ impl<Resp> Future for DispatchResponse<Resp> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(timeout::Elapsed{..}) => Err(
|
||||
io::Error::new(
|
||||
io::ErrorKind::TimedOut,
|
||||
"Client dropped expired request.".to_string(),
|
||||
)
|
||||
),
|
||||
Err(timeout::Elapsed { .. }) => Err(io::Error::new(
|
||||
io::ErrorKind::TimedOut,
|
||||
"Client dropped expired request.".to_string(),
|
||||
)),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -735,8 +733,8 @@ mod tests {
|
||||
use futures_test::task::noop_waker_ref;
|
||||
use std::time::Duration;
|
||||
use std::{pin::Pin, sync::atomic::AtomicU64, sync::Arc};
|
||||
use tokio_timer::Timeout;
|
||||
use tokio::runtime::current_thread;
|
||||
use tokio_timer::Timeout;
|
||||
|
||||
#[test]
|
||||
fn dispatch_response_cancels_on_timeout() {
|
||||
@@ -753,14 +751,11 @@ mod tests {
|
||||
{
|
||||
pin_utils::pin_mut!(resp);
|
||||
let timer = tokio_timer::Timer::default();
|
||||
tokio_timer::with_default(
|
||||
&timer.handle(),
|
||||
|| {
|
||||
let _ = resp
|
||||
.as_mut()
|
||||
.poll(&mut Context::from_waker(&noop_waker_ref()));
|
||||
},
|
||||
);
|
||||
tokio_timer::with_default(&timer.handle(), || {
|
||||
let _ = resp
|
||||
.as_mut()
|
||||
.poll(&mut Context::from_waker(&noop_waker_ref()));
|
||||
});
|
||||
// End of block should cause resp.drop() to run, which should send a cancel message.
|
||||
}
|
||||
assert!(canceled_requests.0.try_next().unwrap() == Some(3));
|
||||
|
||||
@@ -33,9 +33,7 @@ pub(crate) mod util;
|
||||
|
||||
pub use crate::{client::Client, server::Server, transport::Transport};
|
||||
|
||||
use futures::{
|
||||
task::Poll,
|
||||
};
|
||||
use futures::task::Poll;
|
||||
use std::{io, time::SystemTime};
|
||||
|
||||
/// A message from a client to a server.
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
//! Provides a server that concurrently handles many connections sending multiplexed requests.
|
||||
|
||||
use crate::{
|
||||
context, util::Compact, util::TimeUntil, ClientMessage, PollIo, Request,
|
||||
Response, ServerError, Transport,
|
||||
context, util::Compact, util::TimeUntil, ClientMessage, PollIo, Request, Response, ServerError,
|
||||
Transport,
|
||||
};
|
||||
use fnv::FnvHashMap;
|
||||
use futures::{
|
||||
@@ -22,15 +22,8 @@ use futures::{
|
||||
use humantime::format_rfc3339;
|
||||
use log::{debug, trace};
|
||||
use pin_utils::{unsafe_pinned, unsafe_unpinned};
|
||||
use std::{
|
||||
fmt,
|
||||
hash::Hash,
|
||||
io,
|
||||
marker::PhantomData,
|
||||
pin::Pin,
|
||||
time::SystemTime,
|
||||
};
|
||||
use tokio_timer::{Timeout, timeout};
|
||||
use std::{fmt, hash::Hash, io, marker::PhantomData, pin::Pin, time::SystemTime};
|
||||
use tokio_timer::{timeout, Timeout};
|
||||
|
||||
mod filter;
|
||||
#[cfg(test)]
|
||||
@@ -575,7 +568,7 @@ where
|
||||
request_id: self.request_id,
|
||||
message: match result {
|
||||
Ok(message) => Ok(message),
|
||||
Err(timeout::Elapsed{..}) => {
|
||||
Err(timeout::Elapsed { .. }) => {
|
||||
debug!(
|
||||
"[{}] Response did not complete before deadline of {}s.",
|
||||
self.ctx.trace_id(),
|
||||
|
||||
@@ -12,10 +12,6 @@ use futures::{
|
||||
Future,
|
||||
};
|
||||
use publisher::Publisher as _;
|
||||
use tarpc::{
|
||||
client, context,
|
||||
server::{self, Handler},
|
||||
};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io,
|
||||
@@ -25,6 +21,10 @@ use std::{
|
||||
time::Duration,
|
||||
};
|
||||
use subscriber::Subscriber as _;
|
||||
use tarpc::{
|
||||
client, context,
|
||||
server::{self, Handler},
|
||||
};
|
||||
|
||||
pub mod subscriber {
|
||||
#[tarpc::service]
|
||||
|
||||
@@ -10,11 +10,11 @@ use futures::{
|
||||
future::{self, Ready},
|
||||
prelude::*,
|
||||
};
|
||||
use std::io;
|
||||
use tarpc::{
|
||||
client, context,
|
||||
server::{BaseChannel, Channel},
|
||||
};
|
||||
use std::io;
|
||||
|
||||
/// This is the service definition. It looks a lot like a trait definition.
|
||||
/// It defines one RPC, hello, which takes one arg, name, and returns a String.
|
||||
|
||||
@@ -11,11 +11,11 @@ use futures::{
|
||||
future::{self, Ready},
|
||||
prelude::*,
|
||||
};
|
||||
use std::io;
|
||||
use tarpc::{
|
||||
client, context,
|
||||
server::{Handler, Server},
|
||||
};
|
||||
use std::io;
|
||||
|
||||
pub mod add {
|
||||
#[tarpc::service]
|
||||
|
||||
@@ -6,26 +6,26 @@
|
||||
|
||||
//! [](https://crates.io/crates/tarpc)
|
||||
//! [](https://gitter.im/tarpc/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
//!
|
||||
//!
|
||||
//! *Disclaimer*: This is not an official Google product.
|
||||
//!
|
||||
//!
|
||||
//! tarpc is an RPC framework for rust with a focus on ease of use. Defining a
|
||||
//! service can be done in just a few lines of code, and most of the boilerplate of
|
||||
//! writing a server is taken care of for you.
|
||||
//!
|
||||
//!
|
||||
//! [Documentation](https://docs.rs/crate/tarpc/)
|
||||
//!
|
||||
//!
|
||||
//! ## What is an RPC framework?
|
||||
//! "RPC" stands for "Remote Procedure Call," a function call where the work of
|
||||
//! producing the return value is being done somewhere else. When an rpc function is
|
||||
//! invoked, behind the scenes the function contacts some other process somewhere
|
||||
//! and asks them to evaluate the function instead. The original function then
|
||||
//! returns the value produced by the other process.
|
||||
//!
|
||||
//!
|
||||
//! RPC frameworks are a fundamental building block of most microservices-oriented
|
||||
//! architectures. Two well-known ones are [gRPC](http://www.grpc.io) and
|
||||
//! [Cap'n Proto](https://capnproto.org/).
|
||||
//!
|
||||
//!
|
||||
//! tarpc differentiates itself from other RPC frameworks by defining the schema in code,
|
||||
//! rather than in a separate language such as .proto. This means there's no separate compilation
|
||||
//! process, and no context switching between different languages.
|
||||
@@ -45,38 +45,38 @@
|
||||
//! - Serde serialization: enabling the `serde1` Cargo feature will make service requests and
|
||||
//! responses `Serialize + Deserialize`. It's entirely optional, though: in-memory transports can
|
||||
//! be used, as well, so the price of eerialization doesn't have to be paid when it's not needed.
|
||||
//!
|
||||
//!
|
||||
//! ## Usage
|
||||
//! Add to your `Cargo.toml` dependencies:
|
||||
//!
|
||||
//!
|
||||
//! ```toml
|
||||
//! tarpc = "0.18.0"
|
||||
//! ```
|
||||
//!
|
||||
//!
|
||||
//! The `tarpc::service` attribute expands to a collection of items that form an rpc service.
|
||||
//! These generated types make it easy and ergonomic to write servers with less boilerplate.
|
||||
//! Simply implement the generated service trait, and you're off to the races!
|
||||
//!
|
||||
//!
|
||||
//! ## Example
|
||||
//!
|
||||
//!
|
||||
//! For this example, in addition to tarpc, also add two other dependencies to
|
||||
//! your `Cargo.toml`:
|
||||
//!
|
||||
//!
|
||||
//! ```toml
|
||||
//! futures-preview = "0.3.0-alpha.17"
|
||||
//! tokio = "0.2.0-alpha.1"
|
||||
//! ```
|
||||
//!
|
||||
//!
|
||||
//! In the following example, we use an in-process channel for communication between
|
||||
//! client and server. In real code, you will likely communicate over the network.
|
||||
//! For a more real-world example, see [example-service](example-service).
|
||||
//!
|
||||
//!
|
||||
//! First, let's set up the dependencies and service definition.
|
||||
//!
|
||||
//!
|
||||
//! ```rust
|
||||
//! #![feature(async_await)]
|
||||
//! # extern crate futures;
|
||||
//!
|
||||
//!
|
||||
//! use futures::{
|
||||
//! future::{self, Ready},
|
||||
//! prelude::*,
|
||||
@@ -86,7 +86,7 @@
|
||||
//! server::{self, Handler},
|
||||
//! };
|
||||
//! use std::io;
|
||||
//!
|
||||
//!
|
||||
//! // This is the service definition. It looks a lot like a trait definition.
|
||||
//! // It defines one RPC, hello, which takes one arg, name, and returns a String.
|
||||
//! #[tarpc::service]
|
||||
@@ -95,10 +95,10 @@
|
||||
//! async fn hello(name: String) -> String;
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//!
|
||||
//! This service definition generates a trait called `World`. Next we need to
|
||||
//! implement it for our Server struct.
|
||||
//!
|
||||
//!
|
||||
//! ```rust
|
||||
//! # #![feature(async_await)]
|
||||
//! # extern crate futures;
|
||||
@@ -122,26 +122,26 @@
|
||||
//! // and is used to start the server.
|
||||
//! #[derive(Clone)]
|
||||
//! struct HelloServer;
|
||||
//!
|
||||
//!
|
||||
//! impl World for HelloServer {
|
||||
//! // Each defined rpc generates two items in the trait, a fn that serves the RPC, and
|
||||
//! // an associated type representing the future output by the fn.
|
||||
//!
|
||||
//!
|
||||
//! type HelloFut = Ready<String>;
|
||||
//!
|
||||
//!
|
||||
//! fn hello(self, _: context::Context, name: String) -> Self::HelloFut {
|
||||
//! future::ready(format!("Hello, {}!", name))
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//!
|
||||
//! Lastly let's write our `main` that will start the server. While this example uses an
|
||||
//! [in-process
|
||||
//! channel](https://docs.rs/tarpc/0.18.0/tarpc/transport/channel/struct.UnboundedChannel.html),
|
||||
//! tarpc also ships a
|
||||
//! [transport](https://docs.rs/tarpc-bincode-transport/0.7.0/tarpc_bincode_transport/)
|
||||
//! that uses bincode over TCP.
|
||||
//!
|
||||
//!
|
||||
//! ```rust
|
||||
//! # #![feature(async_await)]
|
||||
//! # extern crate futures;
|
||||
@@ -176,32 +176,32 @@
|
||||
//! #[tokio::main]
|
||||
//! async fn main() -> io::Result<()> {
|
||||
//! let (client_transport, server_transport) = tarpc::transport::channel::unbounded();
|
||||
//!
|
||||
//!
|
||||
//! let server = server::new(server::Config::default())
|
||||
//! // incoming() takes a stream of transports such as would be returned by
|
||||
//! // TcpListener::incoming (but a stream instead of an iterator).
|
||||
//! .incoming(stream::once(future::ready(server_transport)))
|
||||
//! .respond_with(HelloServer.serve());
|
||||
//!
|
||||
//!
|
||||
//! tokio::spawn(server);
|
||||
//!
|
||||
//!
|
||||
//! // WorldClient is generated by the macro. It has a constructor `new` that takes a config and
|
||||
//! // any Transport as input
|
||||
//! let mut client = WorldClient::new(client::Config::default(), client_transport).spawn()?;
|
||||
//!
|
||||
//!
|
||||
//! // The client has an RPC method for each RPC defined in the annotated trait. It takes the same
|
||||
//! // args as defined, with the addition of a Context, which is always the first arg. The Context
|
||||
//! // specifies a deadline and trace information which can be helpful in debugging requests.
|
||||
//! let hello = client.hello(context::current(), "Stim".to_string()).await?;
|
||||
//!
|
||||
//!
|
||||
//! println!("{}", hello);
|
||||
//!
|
||||
//!
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//!
|
||||
//! ## Service Documentation
|
||||
//!
|
||||
//!
|
||||
//! Use `cargo doc` as you normally would to see the documentation created for all
|
||||
//! items expanded by a `service!` invocation.
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
#![feature(async_await)]
|
||||
|
||||
|
||||
use assert_matches::assert_matches;
|
||||
use futures::{
|
||||
future::{ready, Ready},
|
||||
prelude::*,
|
||||
};
|
||||
use std::{rc::Rc, io};
|
||||
use std::{io, rc::Rc};
|
||||
use tarpc::{
|
||||
client::{self, NewClient}, context,
|
||||
client::{self, NewClient},
|
||||
context,
|
||||
server::{self, BaseChannel, Channel, Handler},
|
||||
transport::channel,
|
||||
};
|
||||
@@ -45,7 +45,7 @@ async fn sequential() -> io::Result<()> {
|
||||
tokio::spawn(
|
||||
BaseChannel::new(server::Config::default(), rx)
|
||||
.respond_with(Server.serve())
|
||||
.execute()
|
||||
.execute(),
|
||||
);
|
||||
|
||||
let mut client = ServiceClient::new(client::Config::default(), tx).spawn()?;
|
||||
@@ -148,7 +148,10 @@ fn in_memory_single_threaded() -> io::Result<()> {
|
||||
}
|
||||
});
|
||||
|
||||
let NewClient{mut client, dispatch} = InMemoryClient::new(client::Config::default(), tx);
|
||||
let NewClient {
|
||||
mut client,
|
||||
dispatch,
|
||||
} = InMemoryClient::new(client::Config::default(), tx);
|
||||
runtime.spawn(async move {
|
||||
if let Err(e) = dispatch.await {
|
||||
warn!("Error while running client dispatch: {}", e)
|
||||
|
||||
Reference in New Issue
Block a user