mirror of
https://github.com/OMGeeky/tarpc.git
synced 2026-01-10 13:34:03 +01:00
Add service name to generated items.
With this change, the service definitions don't need to be isolated in their own modules.
This commit is contained in:
@@ -44,13 +44,12 @@ async fn main() -> io::Result<()> {
|
||||
|
||||
let transport = json_transport::connect(&server_addr).await?;
|
||||
|
||||
// new_stub is generated by the service! macro. Like Server, it takes a config and any
|
||||
// Transport as input, and returns a Client, also generated by the macro.
|
||||
// by the service mcro.
|
||||
let mut client = service::new_stub(client::Config::default(), transport).await?;
|
||||
// world_stub is generated by the service attribute. Like Server, it takes a config and any
|
||||
// Transport as input, and returns a Client, also generated by the attribute.
|
||||
let mut client = service::world_stub(client::Config::default(), transport).await?;
|
||||
|
||||
// The client has an RPC method for each RPC defined in service!. It takes the same args
|
||||
// as defined, with the addition of a Context, which is always the first arg. The Context
|
||||
// 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(), name).await?;
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
/// 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]
|
||||
pub trait Service {
|
||||
pub trait World {
|
||||
/// Returns a greeting for name.
|
||||
async fn hello(name: String) -> String;
|
||||
}
|
||||
|
||||
@@ -17,12 +17,12 @@ use tarpc::{
|
||||
server::{self, Channel, Handler},
|
||||
};
|
||||
|
||||
// This is the type that implements the generated Service trait. It is the business logic
|
||||
// This is the type that implements the generated World trait. It is the business logic
|
||||
// and is used to start the server.
|
||||
#[derive(Clone)]
|
||||
struct HelloServer(SocketAddr);
|
||||
|
||||
impl service::Service for HelloServer {
|
||||
impl service::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.
|
||||
|
||||
@@ -70,11 +70,11 @@ async fn main() -> io::Result<()> {
|
||||
.map(server::BaseChannel::with_defaults)
|
||||
// Limit channels to 1 per IP.
|
||||
.max_channels_per_key(1, |t| t.as_ref().peer_addr().unwrap().ip())
|
||||
// serve is generated by the service! macro. It takes as input any type implementing
|
||||
// the generated Service trait.
|
||||
// serve is generated by the service attribute. It takes as input any type implementing
|
||||
// the generated World trait.
|
||||
.map(|channel| {
|
||||
let server = HelloServer(channel.as_ref().as_ref().peer_addr().unwrap());
|
||||
channel.respond_with(service::serve(server))
|
||||
channel.respond_with(service::serve_world(server))
|
||||
})
|
||||
// Max 10 channels.
|
||||
.buffer_unordered(10)
|
||||
|
||||
Reference in New Issue
Block a user