Commit 5b6a128f authored by Yi Wang's avatar Yi Wang

fix: sync works with bincode and 2 Runtime

parent 04faa6f9
......@@ -85,6 +85,15 @@ dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bincode"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "1.1.0"
......@@ -1082,12 +1091,12 @@ name = "tokio-serde"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bincode 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"bytes 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
"derivative 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"pin-project 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -1222,6 +1231,7 @@ dependencies = [
"checksum backtrace 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)" = "18b50f5258d1a9ad8396d2d345827875de4261b158124d4c819d9b351454fae5"
"checksum backtrace-sys 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)" = "5b3a000b9c543553af61bc01cbfc403b04b5caa9e421033866f2e98061eb3e61"
"checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
"checksum bincode 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5753e2a71534719bf3f4e57006c3a4f0d2c672a4b676eec84161f763eca87dbf"
"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd"
"checksum blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400"
"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
......
......@@ -26,7 +26,7 @@ rustc-serialize = "0.3.24"
uuid = { version = "0.7.4", features = ["v4"] }
semver = "0.9.0"
tokio = { version = "0.2", features = ["full"] }
tokio-serde = { version = "0.6", features = ["json"] }
tokio-serde = { version = "0.6", features = ["bincode"] }
futures = "0.3"
tarpc = { version = "0.20.0", features = ["full"]}
......
......@@ -27,10 +27,10 @@ use std::io::{Error, ErrorKind};
use std::{io, net::SocketAddr};
use tarpc::{client, context};
use tokio::runtime::Runtime;
use tokio_serde::formats::Json;
use tokio_serde::formats::Bincode;
async fn run_sync_to_server(addr: &SocketAddr) -> io::Result<()> {
let transport = tarpc::serde_transport::tcp::connect(addr, Json::default()).await?;
let transport = tarpc::serde_transport::tcp::connect(addr, Bincode::default()).await?;
let mut client = LocalNativeClient::new(client::Config::default(), transport).spawn()?;
let conn = get_sqlite_connection();
......@@ -60,7 +60,7 @@ async fn run_sync_to_server(addr: &SocketAddr) -> io::Result<()> {
}
async fn run_sync_from_server(addr: &SocketAddr) -> io::Result<()> {
let transport = tarpc::serde_transport::tcp::connect(addr, Json::default()).await?;
let transport = tarpc::serde_transport::tcp::connect(addr, Bincode::default()).await?;
let mut client = LocalNativeClient::new(client::Config::default(), transport).spawn()?;
let conn = get_sqlite_connection();
......@@ -96,6 +96,9 @@ pub fn sync(addr: &str) -> Result<String, String> {
rt.block_on(async {
run_sync_to_server(&server_addr).await;
eprintln!("sync to server done");
});
let mut rt2 = Runtime::new().unwrap();
rt2.block_on(async {
run_sync_from_server(&server_addr).await;
eprintln!("sync from server done");
});
......@@ -103,7 +106,7 @@ pub fn sync(addr: &str) -> Result<String, String> {
}
async fn run_stop_server(addr: &SocketAddr) -> io::Result<()> {
let transport = tarpc::serde_transport::tcp::connect(addr, Json::default()).await?;
let transport = tarpc::serde_transport::tcp::connect(addr, Bincode::default()).await?;
let mut client = LocalNativeClient::new(client::Config::default(), transport).spawn()?;
let conn = get_sqlite_connection();
......
......@@ -35,10 +35,10 @@ use tarpc::{
server::{self, BaseChannel, Channel, Handler},
};
use tokio::runtime::Runtime;
use tokio_serde::formats::Json;
use tokio_serde::formats::Bincode;
#[derive(Clone)]
struct LocalNativeServer;
struct LocalNativeServer(SocketAddr);
impl LocalNative for LocalNativeServer {
type IsVersionMatchFut = Ready<bool>;
......@@ -92,13 +92,16 @@ impl LocalNative for LocalNativeServer {
}
async fn start_server(addr: &SocketAddr) -> io::Result<()> {
tarpc::serde_transport::tcp::listen(addr, Json::default)
tarpc::serde_transport::tcp::listen(addr, Bincode::default)
.await?
.filter_map(|r| future::ready(r.ok()))
.map(server::BaseChannel::with_defaults)
// Limit channels to 1 per IP.
.max_channels_per_key(1, |t| t.as_ref().peer_addr().unwrap().ip())
.map(|channel| channel.respond_with(LocalNativeServer.serve()).execute())
.map(|channel| {
let server = LocalNativeServer(channel.as_ref().as_ref().peer_addr().unwrap());
channel.respond_with(server.serve()).execute()
})
// Max 10 channels.
.buffer_unordered(10)
.for_each(|_| async {})
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment