Skip to content

Commit e011a79

Browse files
author
Hui Zhu
authored
Merge pull request #35 from Tim-Zhang/async-server
Add async support
2 parents ba1efe3 + 0c03473 commit e011a79

32 files changed

+1984
-372
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Cargo.lock
66
.vscode
77
.idea
88
*.o
9-
example/protocols/*.rs
10-
!example/protocols/mod.rs
9+
example/protocols/**/*.rs
10+
!example/protocols/**/mod.rs

Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ttrpc"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
authors = ["The AntFin Kata Team <[email protected]>"]
55
edition = "2018"
66
license = "Apache-2.0"
@@ -17,11 +17,17 @@ libc = { version = "0.2.59", features = [ "extra_traits" ] }
1717
nix = "0.16.1"
1818
log = "0.4"
1919
byteorder = "1.3.2"
20+
async-trait = "0.1.31"
21+
22+
tokio = { version = "0.2", features = ["rt-threaded", "sync", "uds", "stream", "macros", "io-util"] }
23+
futures = "0.3"
2024

2125
[build-dependencies]
2226
protobuf-codegen-pure = "2.14.0"
2327

2428
[features]
25-
default = ["protobuf-codec"]
29+
default = ["protobuf-codec", "sync"]
2630
protobuf-codec = ["protobuf"]
31+
async = []
32+
sync = []
2733

README.md

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,65 @@ fn main() {
5252
}
5353
```
5454

55+
# async/.await
56+
ttrpc-rust supports async/.await. By using async/.await you can reduce the overhead and resource consumption caused by threads.
57+
58+
## Usage
59+
### 1. Generate codes in async version
60+
Currently we only support generating async codes by using protoc_rust_ttrpc
61+
62+
```
63+
protoc_rust_ttrpc::Codegen::new()
64+
.out_dir("protocols/asynchronous")
65+
.inputs(&protos)
66+
.include("protocols/protos")
67+
.rust_protobuf()
68+
.customize(Customize {
69+
async_all: true, // It's the key option.
70+
..Default::default()
71+
})
72+
.run()
73+
.expect("Gen async codes failed.");
74+
```
75+
76+
Provide customize option
77+
- `async_all`: generate async codes for both server and client
78+
- `async_server`: generate async codes for server
79+
- `async_client`: generate async codes for client
80+
81+
> See more in `example/build.rs`
82+
83+
### 2. Write your implemention in async/.await's way
84+
Please follow the guidlines in `example/async-server.rs` and `example/async-client.rs`
85+
5586
# Run Examples
5687
1. Go to the directory
5788

58-
`$ cd ttrpc-rust/example`
89+
```
90+
$ cd ttrpc-rust/example
91+
```
5992
6093
2. Start the server
6194
62-
`$ cargo run --example server unix:///tmp/1`
95+
```
96+
$ cargo run --example server
97+
```
98+
or
99+
100+
```
101+
$ cargo run --example async-server
102+
```
63103
64104
3. Start a client
65105
66-
`$ cargo run --example client /tmp/1`
106+
```
107+
$ cargo run --example client
108+
```
109+
or
110+
```
111+
$ cargo run --example async-client
112+
```
113+
67114
68115
# Notes: the version of protobuf
69116
protobuf-codegen, ttrpc_rust_plugin and your code should use the same version protobuf.

compiler/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ttrpc-compiler"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
edition = "2018"
55
authors = ["The AntFin Kata Team <[email protected]>"]
66
license = "Apache-2.0"

0 commit comments

Comments
 (0)