From 837fc018f8cc03ce1a8ed9f9f862bab6dc1031a3 Mon Sep 17 00:00:00 2001 From: Pure White Date: Thu, 19 Oct 2023 16:11:03 +0800 Subject: [PATCH] fix: pilota codegen && remove afit --- Cargo.lock | 78 +++++++++++++++++----------- Cargo.toml | 18 +++---- volo-build/src/thrift_backend.rs | 82 +++++++++++++++++++++--------- volo-thrift/src/error.rs | 2 - volo-thrift/src/message.rs | 8 ++- volo-thrift/src/message_wrapper.rs | 3 +- volo/Cargo.toml | 8 ++- 7 files changed, 126 insertions(+), 73 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f82a7a57..a184fb66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -97,11 +97,12 @@ checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" [[package]] name = "async-broadcast" -version = "0.5.1" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c48ccdbf6ca6b121e0f586cbc0e73ae440e56c67c30fa0873b4e110d9c26d2b" +checksum = "334d75cf09b33bede6cbc20e52515853ae7bee3d4eadd9540e13ce92af983d34" dependencies = [ "event-listener", + "event-listener-strategy", "futures-core", ] @@ -315,6 +316,15 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "concurrent-queue" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f057a694a54f12365049b0958a1685bb52d567f5593b355fbf685838e873d400" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "core-foundation-sys" version = "0.8.4" @@ -360,7 +370,7 @@ dependencies = [ "autocfg", "cfg-if", "crossbeam-utils", - "memoffset 0.9.0", + "memoffset", "scopeguard", ] @@ -490,9 +500,24 @@ dependencies = [ [[package]] name = "event-listener" -version = "2.5.3" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +checksum = "29e56284f00d94c1bc7fd3c77027b4623c88c1f53d8d2394c6199f2921dea325" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15c97b4e30ea7e4b7e7b429d6e2d8510433ba8cee4e70dfb3243794e539d29fd" +dependencies = [ + "event-listener", + "pin-project-lite", +] [[package]] name = "examples" @@ -1060,15 +1085,6 @@ version = "2.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" -[[package]] -name = "memoffset" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" -dependencies = [ - "autocfg", -] - [[package]] name = "memoffset" version = "0.9.0" @@ -1154,15 +1170,14 @@ checksum = "97af489e1e21b68de4c390ecca6703318bc1aa16e9733bcb62c089b73c6fbb1b" [[package]] name = "nix" -version = "0.26.4" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.4.0", "cfg-if", "libc", - "memoffset 0.7.1", - "pin-utils", + "memoffset", ] [[package]] @@ -1216,18 +1231,18 @@ dependencies = [ [[package]] name = "num_enum" -version = "0.6.1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a015b430d3c108a207fd776d2e2196aaf8b1cf8cf93253e3a097ff3085076a1" +checksum = "70bf6736f74634d299d00086f02986875b3c2d924781a6a2cb6c201e73da0ceb" dependencies = [ "num_enum_derive", ] [[package]] name = "num_enum_derive" -version = "0.6.1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96667db765a921f7b295ffee8b60472b686a51d4f21c2ee4ffdb94c7013b65a6" +checksum = "56ea360eafe1022f7cc56cd7b869ed57330fb2453d0c7831d99b74c65d2f5597" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -1268,6 +1283,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" +[[package]] +name = "parking" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" + [[package]] name = "parking_lot" version = "0.11.2" @@ -1389,8 +1410,7 @@ dependencies = [ [[package]] name = "pilota" version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6bdd97afd388beda3267ee10487cd8d6d8f44e88b29c8e837ce3bf925eb362e" +source = "git+https://github.com/cloudwego/pilota?branch=main#d6693254e00af826ada7d6ff4364fd0462a99044" dependencies = [ "anyhow", "async-recursion", @@ -1411,9 +1431,8 @@ dependencies = [ [[package]] name = "pilota-build" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8151d64b6ecf41e035e151391b374810572f574c5ab272fa31b5222ff4476748" +version = "0.8.6" +source = "git+https://github.com/cloudwego/pilota?branch=main#d6693254e00af826ada7d6ff4364fd0462a99044" dependencies = [ "anyhow", "dashmap", @@ -1445,8 +1464,7 @@ dependencies = [ [[package]] name = "pilota-thrift-parser" version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6248d1c9c5eb01b579ae6b9373eca87919a205e41d7b815758cdf323d97029f0" +source = "git+https://github.com/cloudwego/pilota?branch=main#d6693254e00af826ada7d6ff4364fd0462a99044" dependencies = [ "nom", ] diff --git a/Cargo.toml b/Cargo.toml index 3bd9c839..f12dff64 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,19 +21,19 @@ repository = "https://github.com/cloudwego/volo" license = "MIT OR Apache-2.0" [workspace.dependencies] -pilota = "0.8" -pilota-build = "0.8" -pilota-thrift-parser = "0.4" -# pilota = { git = "https://github.com/cloudwego/pilota", branch = "main" } -# pilota-build = { git = "https://github.com/cloudwego/pilota", branch = "main" } -# pilota-thrift-parser = { git = "https://github.com/cloudwego/pilota", branch = "main" } +# pilota = "0.8" +# pilota-build = "0.8" +# pilota-thrift-parser = "0.4" +pilota = { git = "https://github.com/cloudwego/pilota", branch = "main" } +pilota-build = { git = "https://github.com/cloudwego/pilota", branch = "main" } +pilota-thrift-parser = { git = "https://github.com/cloudwego/pilota", branch = "main" } motore = "0.3" metainfo = "0.7" anyhow = "1" -async-broadcast = "0.5" +async-broadcast = "0.6" async-stream = "0.3" async-trait = "0.1" base64 = "0.13" @@ -67,10 +67,10 @@ linked-hash-map = "0.5" log = "0.4" matchit = "0.7" mur3 = "0.1" -nix = "0.26" +nix = "0.27" nom = "7" normpath = "1" -num_enum = "0.6" +num_enum = "0.7" once_cell = "1" parking_lot = "0.12" paste = "1" diff --git a/volo-build/src/thrift_backend.rs b/volo-build/src/thrift_backend.rs index 28147682..6e9ea759 100644 --- a/volo-build/src/thrift_backend.rs +++ b/volo-build/src/thrift_backend.rs @@ -46,10 +46,27 @@ impl VoloThriftBackend { let res_send_name = format!("{service_name}ResponseSend"); let req_impl = { - let mk_decode = |is_async: bool| { + let mk_decode = |is_async: bool, is_send: bool| { let helper = DecodeHelper::new(is_async); - let decode_variants = helper.codegen_item_decode(); - let match_methods = crate::join_multi_strs!("", |methods_names, variant_names| -> "\"{methods_names}\" => {{ Self::{variant_names}({decode_variants}) }},"); + let mut match_methods = String::new(); + let args_names = if is_send { + args_send_names.clone() + } else { + args_recv_names.clone() + }; + for (methods_names, variant_names, args_name) in itertools::multizip(( + methods_names.iter(), + variant_names.iter(), + args_names.iter(), + )) { + let decode_variants = helper.codegen_item_decode(args_name.clone().into()); + match_methods.push_str(&format!( + "\"{methods_names}\" => {{ Self::{variant_names}({decode_variants}) }}," + )); + } + // let decode_variants = helper.codegen_item_decode(req_recv_name.clone().into()); + // let match_methods = crate::join_multi_strs!("", |methods_names, variant_names| -> + // "\"{methods_names}\" => {{ Self::{variant_names}({decode_variants}) }},"); format! { r#"Ok(match &*msg_ident.name {{ @@ -61,8 +78,10 @@ impl VoloThriftBackend { } }; - let decode = mk_decode(false); - let decode_async = mk_decode(true); + let send_decode = mk_decode(false, true); + let send_decode_async = mk_decode(true, true); + let recv_decode = mk_decode(false, false); + let recv_decode_async = mk_decode(true, false); let mut match_encode = crate::join_multi_strs!(",", |variant_names| -> "Self::{variant_names}(value) => {{::pilota::thrift::Message::encode(value, protocol).map_err(|err| err.into())}}"); let mut match_size = crate::join_multi_strs!(",", |variant_names| -> "Self::{variant_names}(value) => {{::volo_thrift::Message::size(value, protocol)}}"); @@ -73,8 +92,7 @@ impl VoloThriftBackend { } format! { - r#"#[::async_trait::async_trait] - impl ::volo_thrift::EntryMessage for {req_recv_name} {{ + r#"impl ::volo_thrift::EntryMessage for {req_recv_name} {{ fn encode(&self, protocol: &mut T) -> ::core::result::Result<(), ::pilota::thrift::EncodeError> {{ match self {{ {match_encode} @@ -82,7 +100,7 @@ impl VoloThriftBackend { }} fn decode(protocol: &mut T, msg_ident: &::pilota::thrift::TMessageIdentifier) -> ::core::result::Result {{ - {decode} + {recv_decode} }} async fn decode_async( @@ -90,7 +108,7 @@ impl VoloThriftBackend { msg_ident: &::pilota::thrift::TMessageIdentifier ) -> ::core::result::Result {{ - {decode_async} + {recv_decode_async} }} fn size(&self, protocol: &mut T) -> usize {{ @@ -100,7 +118,6 @@ impl VoloThriftBackend { }} }} - #[::async_trait::async_trait] impl ::volo_thrift::EntryMessage for {req_send_name} {{ fn encode(&self, protocol: &mut T) -> ::core::result::Result<(), ::pilota::thrift::EncodeError> {{ match self {{ @@ -109,7 +126,7 @@ impl VoloThriftBackend { }} fn decode(protocol: &mut T, msg_ident: &::pilota::thrift::TMessageIdentifier) -> ::core::result::Result {{ - {decode} + {send_decode} }} async fn decode_async( @@ -117,7 +134,7 @@ impl VoloThriftBackend { msg_ident: &::pilota::thrift::TMessageIdentifier ) -> ::core::result::Result {{ - {decode_async} + {send_decode_async} }} fn size(&self, protocol: &mut T) -> usize {{ @@ -130,11 +147,28 @@ impl VoloThriftBackend { }; let res_impl = { - let mk_decode = |is_async: bool| { + let mk_decode = |is_async: bool, is_send: bool| { let helper = DecodeHelper::new(is_async); - let decode_item = helper.codegen_item_decode(); + let mut match_methods = String::new(); + let args_names = if is_send { + result_send_names.clone() + } else { + result_recv_names.clone() + }; + for (methods_names, variant_names, args_name) in itertools::multizip(( + methods_names.iter(), + variant_names.iter(), + args_names.iter(), + )) { + let decode_item = helper.codegen_item_decode(args_name.clone().into()); + match_methods.push_str(&format!( + "\"{methods_names}\" => {{ Self::{variant_names}({decode_item}) }}," + )); + } + // let decode_item = helper.codegen_item_decode(res_recv_name.clone().into()); - let match_methods = crate::join_multi_strs!("", |methods_names, variant_names| -> "\"{methods_names}\" => {{ Self::{variant_names}({decode_item}) }},"); + // let match_methods = crate::join_multi_strs!("", |methods_names, variant_names| -> + // "\"{methods_names}\" => {{ Self::{variant_names}({decode_item}) }},"); format!( r#"Ok(match &*msg_ident.name {{ @@ -154,11 +188,12 @@ impl VoloThriftBackend { match_size = "_ => unreachable!(),".to_string(); } - let decode = mk_decode(false); - let decode_async = mk_decode(true); + let send_decode = mk_decode(false, true); + let send_decode_async = mk_decode(true, true); + let recv_decode = mk_decode(false, false); + let recv_decode_async = mk_decode(true, false); format! { - r#"#[::async_trait::async_trait] - impl ::volo_thrift::EntryMessage for {res_recv_name} {{ + r#"impl ::volo_thrift::EntryMessage for {res_recv_name} {{ fn encode(&self, protocol: &mut T) -> ::core::result::Result<(), ::pilota::thrift::EncodeError> {{ match self {{ {match_encode} @@ -166,7 +201,7 @@ impl VoloThriftBackend { }} fn decode(protocol: &mut T, msg_ident: &::pilota::thrift::TMessageIdentifier) -> ::core::result::Result {{ - {decode} + {recv_decode} }} async fn decode_async( @@ -174,7 +209,7 @@ impl VoloThriftBackend { msg_ident: &::pilota::thrift::TMessageIdentifier, ) -> ::core::result::Result {{ - {decode_async} + {recv_decode_async} }} fn size(&self, protocol: &mut T) -> usize {{ @@ -184,7 +219,6 @@ impl VoloThriftBackend { }} }} - #[::async_trait::async_trait] impl ::volo_thrift::EntryMessage for {res_send_name} {{ fn encode(&self, protocol: &mut T) -> ::core::result::Result<(), ::pilota::thrift::EncodeError> {{ match self {{ @@ -193,7 +227,7 @@ impl VoloThriftBackend { }} fn decode(protocol: &mut T, msg_ident: &::pilota::thrift::TMessageIdentifier) -> ::core::result::Result {{ - {decode} + {send_decode} }} async fn decode_async( @@ -201,7 +235,7 @@ impl VoloThriftBackend { msg_ident: &::pilota::thrift::TMessageIdentifier, ) -> ::core::result::Result {{ - {decode_async} + {send_decode_async} }} fn size(&self, protocol: &mut T) -> usize {{ diff --git a/volo-thrift/src/error.rs b/volo-thrift/src/error.rs index 1fbf154b..eea31d4b 100644 --- a/volo-thrift/src/error.rs +++ b/volo-thrift/src/error.rs @@ -33,7 +33,6 @@ pub enum Error { #[derive(Debug, Clone, Copy)] pub struct DummyError; -#[async_trait::async_trait] impl Message for DummyError { fn encode(&self, _protocol: &mut T) -> Result<(), EncodeError> { panic!() @@ -210,7 +209,6 @@ impl Display for ApplicationError { } } -#[async_trait::async_trait] impl Message for ApplicationError { /// Convert an `ApplicationError` into its wire representation and write /// it to the remote. diff --git a/volo-thrift/src/message.rs b/volo-thrift/src/message.rs index 0c7b7a7b..6864942c 100644 --- a/volo-thrift/src/message.rs +++ b/volo-thrift/src/message.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{future::Future, sync::Arc}; pub use pilota::thrift::Message; use pilota::thrift::{ @@ -6,7 +6,6 @@ use pilota::thrift::{ TMessageIdentifier, TOutputProtocol, }; -#[async_trait::async_trait] pub trait EntryMessage: Sized + Send { fn encode(&self, protocol: &mut T) -> Result<(), EncodeError>; @@ -15,15 +14,14 @@ pub trait EntryMessage: Sized + Send { msg_ident: &TMessageIdentifier, ) -> Result; - async fn decode_async( + fn decode_async( protocol: &mut T, msg_ident: &TMessageIdentifier, - ) -> Result; + ) -> impl Future> + Send; fn size(&self, protocol: &mut T) -> usize; } -#[async_trait::async_trait] impl EntryMessage for Arc where Message: EntryMessage + Sync, diff --git a/volo-thrift/src/message_wrapper.rs b/volo-thrift/src/message_wrapper.rs index d2c1b7ee..f42897be 100644 --- a/volo-thrift/src/message_wrapper.rs +++ b/volo-thrift/src/message_wrapper.rs @@ -24,7 +24,6 @@ pub struct ThriftMessage { pub(crate) struct DummyMessage; -#[async_trait::async_trait] impl EntryMessage for DummyMessage { #[inline] fn encode(&self, _protocol: &mut T) -> Result<(), EncodeError> { @@ -186,7 +185,7 @@ where let res = match msg_ident.message_type { TMessageType::Exception => Err(crate::Error::Application( - Message::decode_async(protocol).await?, + ::decode_async(protocol).await?, )), _ => Ok(U::decode_async(protocol, &msg_ident).await?), }; diff --git a/volo/Cargo.toml b/volo/Cargo.toml index 1665c4f9..c33304fe 100644 --- a/volo/Cargo.toml +++ b/volo/Cargo.toml @@ -31,7 +31,13 @@ lazy_static.workspace = true libc.workspace = true metainfo.workspace = true mur3.workspace = true -nix.workspace = true +nix = { workspace = true, features = [ + "uio", + "socket", + "process", + "signal", + "feature", +] } once_cell.workspace = true pin-project.workspace = true rand.workspace = true