mirror of
https://github.com/OMGeeky/tarpc.git
synced 2025-12-28 07:12:05 +01:00
The basic strategy is to start a reactor on a dedicated thread running a request stream. Requests are spawned onto the reactor, allowing multiple requests to be processed concurrently. For example, if you clone the client to make requests from multiple threads, they won't have to wait for each others' requests to complete before theirs start being sent out. Also, client rpcs only take &self now, which was also required for clients to be usable in a service. Also added a test to prevent regressions.
44 lines
1.2 KiB
Rust
44 lines
1.2 KiB
Rust
// Copyright 2016 Google Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the MIT License, <LICENSE or http://opensource.org/licenses/MIT>.
|
|
// This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
// required by `FutureClient` (not used directly in this example)
|
|
#![feature(plugin)]
|
|
#![plugin(tarpc_plugins)]
|
|
|
|
extern crate futures;
|
|
#[macro_use]
|
|
extern crate tarpc;
|
|
extern crate tokio_core;
|
|
|
|
use std::sync::mpsc;
|
|
use std::thread;
|
|
use tarpc::{client, server};
|
|
use tarpc::client::sync::ClientExt;
|
|
use tarpc::util::Never;
|
|
|
|
service! {
|
|
rpc hello(name: String) -> String;
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct HelloServer;
|
|
|
|
impl SyncService for HelloServer {
|
|
fn hello(&self, name: String) -> Result<String, Never> {
|
|
Ok(format!("Hello, {}!", name))
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let (tx, rx) = mpsc::channel();
|
|
thread::spawn(move || {
|
|
let handle = HelloServer.listen("localhost:0", server::Options::default()).unwrap();
|
|
tx.send(handle.addr()).unwrap();
|
|
handle.run();
|
|
});
|
|
let client = SyncClient::connect(rx.recv().unwrap(), client::Options::default()).unwrap();
|
|
println!("{}", client.hello("Mom".to_string()).unwrap());
|
|
}
|