From 0878f1c522139d4372c31ae82950a56d546111e5 Mon Sep 17 00:00:00 2001 From: Kould <2435992353@qq.com> Date: Sun, 3 Dec 2023 22:14:22 +0800 Subject: [PATCH] feat: make rpc functionality optional --- Cargo.toml | 9 ++++++--- README.md | 2 +- build.rs | 9 ++++++--- src/error.rs | 2 ++ src/lib.rs | 2 ++ src/proto/mod.rs | 1 + 6 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7d50ded..7bee061 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,10 +14,12 @@ default-run = "server" [[bin]] name = "cli" path = "src/bin/cli.rs" +required-features = ["net"] [[bin]] name = "server" path = "src/bin/server.rs" +required-features = ["net"] [[bench]] name = "server_bench" @@ -30,6 +32,7 @@ debug = true [features] sled = ["dep:sled"] rocksdb = ["dep:rocksdb"] +net = ["dep:tonic", "dep:prost", "dep:tonic-build"] [dependencies] thiserror = "1.0.24" @@ -59,8 +62,8 @@ crc32fast = "1.3.2" skiplist = "0.5.1" fslock = "0.2.1" # grpc -tonic = "0.10.2" -prost = "0.12" +tonic = { version = "0.10.2", optional = true } +prost = { version = "0.12", optional = true } # 其他数据库内核 sled = { version = "0.34.7", optional = true } rocksdb = { version = "0.21.0", optional = true } @@ -76,4 +79,4 @@ tempfile = "3.0.7" rand = "0.8.5" [build-dependencies] -tonic-build = "0.10.2" \ No newline at end of file +tonic-build = {version = "0.10.2", optional = true} \ No newline at end of file diff --git a/README.md b/README.md index 79fea32..ff5a726 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ **组件原理Wiki** : https://github.com/KKould/KipDB/wiki ## 快速上手 🤞 -确保 [Protocol Buffer Compiler](https://grpc.io/docs/protoc-installation/) 已安装。 +Tips: 使用RPC时请确保 [Protocol Buffer Compiler](https://grpc.io/docs/protoc-installation/) 已安装。 #### 组件引入 ``` toml diff --git a/build.rs b/build.rs index 86d86a7..ddf747d 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,9 @@ fn main() -> Result<(), Box> { - tonic_build::configure() - .protoc_arg("--experimental_allow_proto3_optional") - .compile(&["src/proto/kipdb.proto"], &["src/proto"])?; + #[cfg(feature = "net")] + { + tonic_build::configure() + .protoc_arg("--experimental_allow_proto3_optional") + .compile(&["src/proto/kipdb.proto"], &["src/proto"])?; + } Ok(()) } diff --git a/src/error.rs b/src/error.rs index bd7d42a..456d3b6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -109,9 +109,11 @@ pub enum ConnectionError { #[error("server flush error")] FlushError, + #[cfg(feature = "net")] #[error("Failed to connect to server, {0}")] TonicTransportErr(#[from] tonic::transport::Error), + #[cfg(feature = "net")] #[error("Failed to call server, {0}")] TonicFailureStatus(#[from] tonic::Status), diff --git a/src/lib.rs b/src/lib.rs index 0297206..ed96651 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,8 @@ pub mod config; pub mod error; pub mod kernel; pub mod proto; + +#[cfg(feature = "net")] pub mod server; pub use error::KernelError; diff --git a/src/proto/mod.rs b/src/proto/mod.rs index eae8ce8..90aa347 100644 --- a/src/proto/mod.rs +++ b/src/proto/mod.rs @@ -1 +1,2 @@ +#[cfg(feature = "net")] tonic::include_proto!("kipdb");