Skip to content

Commit e992744

Browse files
committed
README: example should refer to online doc
1 parent a03df8e commit e992744

1 file changed

Lines changed: 6 additions & 101 deletions

File tree

README.md

Lines changed: 6 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ In an `RpcServer`, for each connection, there is one coroutine to read requests
3636
coroutine to write responses. Requests can be dispatched with a user-defined
3737
`Dispatch` trait implementation.
3838

39+
### Builtin connection pools:
40+
41+
- [ClientPool](https://docs.rs/razor-stream/latest/razor_stream/client/struct.ClientPool.html): Auto scalable pool:
42+
- [FailoverPool](https://docs.rs/razor-stream/latest/razor_stream/client/struct.FailoverPool.html): Fault-tolerance pool
43+
3944
## API call interface
4045

41-
`razor-rpc` <https://docs.rs/razor-rpc>
46+
Detail document and example refer to the `razor-rpc` crate <https://docs.rs/razor-rpc>
4247

4348
- Independent from async runtime (with plugins)
4449
- With service trait very similar to grpc / tarpc (stream in API interface is not supported
@@ -47,103 +52,3 @@ currently)
4752
wrapper
4853
- Each method can have different custom error type (requires the type implements [RpcErrCodec](https://docs.rs/razor-stream/latest/razor_stream/error/trait.RpcErrCodec.html))
4954
- based on razor-stream`: Full duplex in each connection, with sliding window threshold, allow maximizing throughput and lower cpu usage.
50-
51-
(Warning: The API and feature is still evolving, might changed in the future)
52-
53-
### Example
54-
55-
```rust
56-
use razor_rpc::client::{endpoint_client, endpoint_async, APIClientReq, ClientConfig};
57-
use razor_rpc::server::{service, ServerConfig};
58-
use razor_rpc::error::RpcError;
59-
use razor_rpc_tcp::{TcpClient, TcpServer};
60-
use nix::errno::Errno;
61-
use std::future::Future;
62-
use std::sync::Arc;
63-
64-
// 1. Choose the async runtime, and the codec
65-
type OurRt = orb_smol::SmolRT;
66-
type OurCodec = razor_rpc_codec::MsgpCodec;
67-
// 2. Choose transport
68-
type ServerProto = TcpServer<OurRt>;
69-
type ClientProto = TcpClient<OurRt>;
70-
71-
// 3. Define the client struct and service trait
72-
endpoint_client!(CalculatorClient);
73-
74-
#[endpoint_async(CalculatorClient)]
75-
pub trait CalculatorService {
76-
// Method with unit error type using impl Future
77-
fn add(&self, args: (i32, i32)) -> impl Future<Output = Result<i32, RpcError<()>>> + Send;
78-
79-
// Method with string error type using impl Future
80-
fn div(&self, args: (i32, i32)) -> impl Future<Output = Result<i32, RpcError<String>>> + Send;
81-
82-
// Method with errno error type using impl Future
83-
fn might_fail_with_errno(&self, value: i32) -> impl Future<Output = Result<i32, RpcError<Errno>>> + Send;
84-
}
85-
86-
// 4. Server implementation, can use Arc with internal context, but we are a simple demo
87-
#[derive(Clone)]
88-
pub struct CalculatorServer;
89-
90-
#[service]
91-
impl CalculatorService for CalculatorServer {
92-
async fn add(&self, args: (i32, i32)) -> Result<i32, RpcError<()>> {
93-
let (a, b) = args;
94-
Ok(a + b)
95-
}
96-
97-
async fn div(&self, args: (i32, i32)) -> Result<i32, RpcError<String>> {
98-
let (a, b) = args;
99-
if b == 0 {
100-
Err(RpcError::User("division by zero".to_string()))
101-
} else {
102-
Ok(a / b)
103-
}
104-
}
105-
106-
async fn might_fail_with_errno(&self, value: i32) -> Result<i32, RpcError<Errno>> {
107-
if value < 0 {
108-
Err(RpcError::User(Errno::EINVAL))
109-
} else {
110-
Ok(value * 2)
111-
}
112-
}
113-
}
114-
115-
fn setup_server() -> std::io::Result<String> {
116-
// 5. Server setup with default ServerFacts
117-
use razor_rpc::server::{RpcServer, ServerDefault};
118-
let server_config = ServerConfig::default();
119-
let mut server = RpcServer::new(ServerDefault::new(server_config, OurRt::new_global()));
120-
// 6. dispatch
121-
use razor_rpc::server::dispatch::Inline;
122-
let disp = Inline::<OurCodec, _>::new(CalculatorServer);
123-
// 7. Start listening
124-
let actual_addr = server.listen::<ServerProto, _>("127.0.0.1:8082", disp)?;
125-
Ok(actual_addr)
126-
}
127-
128-
async fn use_client(server_addr: &str) {
129-
use razor_rpc::client::*;
130-
// 8. ClientFacts
131-
let mut client_config = ClientConfig::default();
132-
client_config.task_timeout = 5;
133-
let rt = OurRt::new_global();
134-
type OurFacts = APIClientDefault<OurRt, OurCodec>;
135-
let client_facts = OurFacts::new(client_config, rt);
136-
// 9. Create client connection pool
137-
let pool: ClientPool<OurFacts, ClientProto> =
138-
client_facts.create_pool_async::<ClientProto>(server_addr);
139-
let client = CalculatorClient::new(pool);
140-
// Call methods with different error types
141-
if let Ok(r) = client.add((10, 20)).await {
142-
assert_eq!(r, 30);
143-
}
144-
// This will return a string error, but connect might fail, who knows
145-
if let Err(e) = client.div((10, 0)).await {
146-
println!("error occurred: {}", e);
147-
}
148-
}
149-
```

0 commit comments

Comments
 (0)