From 014b04a5d16f2b185c4841e55cb1e1ef3941e35b Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Sat, 30 Mar 2024 00:24:44 -0400 Subject: [PATCH] remove GeyserGrpcBuilder::build --- examples/rust/src/bin/client.rs | 3 +- examples/rust/src/bin/subscribe-ping.rs | 3 +- examples/rust/src/bin/tx-blocktime.rs | 3 +- yellowstone-grpc-client/src/lib.rs | 55 ++++++++----------- .../src/bin/grpc-google-pubsub.rs | 3 +- yellowstone-grpc-tools/src/bin/grpc-kafka.rs | 3 +- 6 files changed, 29 insertions(+), 41 deletions(-) diff --git a/examples/rust/src/bin/client.rs b/examples/rust/src/bin/client.rs index fe31cdd5..6c532f43 100644 --- a/examples/rust/src/bin/client.rs +++ b/examples/rust/src/bin/client.rs @@ -57,8 +57,7 @@ impl Args { .connect_timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(10)) .connect() - .await? - .build() + .await .map_err(Into::into) } } diff --git a/examples/rust/src/bin/subscribe-ping.rs b/examples/rust/src/bin/subscribe-ping.rs index e3c3b19c..89540a80 100644 --- a/examples/rust/src/bin/subscribe-ping.rs +++ b/examples/rust/src/bin/subscribe-ping.rs @@ -36,8 +36,7 @@ async fn main() -> anyhow::Result<()> { let mut client = GeyserGrpcClient::build_from_shared(args.endpoint)? .x_token(args.x_token)? .connect() - .await? - .build()?; + .await?; let (mut subscribe_tx, mut stream) = client.subscribe().await?; futures::try_join!( diff --git a/examples/rust/src/bin/tx-blocktime.rs b/examples/rust/src/bin/tx-blocktime.rs index 1af5e1da..e3a9313b 100644 --- a/examples/rust/src/bin/tx-blocktime.rs +++ b/examples/rust/src/bin/tx-blocktime.rs @@ -86,8 +86,7 @@ async fn main() -> anyhow::Result<()> { let mut client = GeyserGrpcClient::build_from_shared(args.endpoint)? .x_token(args.x_token)? .connect() - .await? - .build()?; + .await?; let (mut subscribe_tx, mut stream) = client.subscribe().await?; let commitment: CommitmentLevel = args.commitment.unwrap_or_default().into(); diff --git a/yellowstone-grpc-client/src/lib.rs b/yellowstone-grpc-client/src/lib.rs index 6357e59a..61fcf804 100644 --- a/yellowstone-grpc-client/src/lib.rs +++ b/yellowstone-grpc-client/src/lib.rs @@ -30,6 +30,12 @@ pub struct InterceptorXToken { pub x_token: Option, } +impl From> for InterceptorXToken { + fn from(x_token: Option) -> Self { + Self { x_token } + } +} + impl Interceptor for InterceptorXToken { fn call(&mut self, mut request: Request<()>) -> Result, Status> { if let Some(x_token) = self.x_token.clone() { @@ -209,10 +215,7 @@ pub type GeyserGrpcBuilderResult = Result; #[derive(Debug)] pub struct GeyserGrpcBuilder { pub endpoint: Endpoint, - pub channel: Option, - pub x_token: Option, - pub send_compressed: Option, pub accept_compressed: Option, pub max_decoding_message_size: Option, @@ -224,10 +227,7 @@ impl GeyserGrpcBuilder { fn new(endpoint: Endpoint) -> Self { Self { endpoint, - channel: None, - x_token: None, - send_compressed: None, accept_compressed: None, max_decoding_message_size: None, @@ -244,14 +244,11 @@ impl GeyserGrpcBuilder { } // Create client - pub fn build(mut self) -> GeyserGrpcBuilderResult> { - let channel = self - .channel - .take() - .ok_or(GeyserGrpcBuilderError::EmptyChannel)?; - let interceptor = InterceptorXToken { - x_token: self.x_token, - }; + fn build( + self, + channel: Channel, + ) -> GeyserGrpcBuilderResult> { + let interceptor: InterceptorXToken = self.x_token.into(); let mut geyser = GeyserClient::with_interceptor(channel.clone(), interceptor.clone()); if let Some(encoding) = self.send_compressed { @@ -273,6 +270,16 @@ impl GeyserGrpcBuilder { )) } + pub async fn connect(self) -> GeyserGrpcBuilderResult> { + let channel = self.endpoint.connect().await?; + self.build(channel) + } + + pub fn connect_lazy(self) -> GeyserGrpcBuilderResult> { + let channel = self.endpoint.connect_lazy(); + self.build(channel) + } + // Set x-token pub fn x_token(self, x_token: Option) -> GeyserGrpcBuilderResult where @@ -294,20 +301,6 @@ impl GeyserGrpcBuilder { } // Endpoint options - pub async fn connect(self) -> GeyserGrpcBuilderResult { - Ok(Self { - channel: Some(self.endpoint.connect().await?), - ..self - }) - } - - pub fn connect_lazy(self) -> Self { - Self { - channel: Some(self.endpoint.connect_lazy()), - ..self - } - } - pub fn connect_timeout(self, dur: Duration) -> Self { Self { endpoint: self.endpoint.connect_timeout(dur), @@ -437,7 +430,7 @@ mod tests { let res = res.unwrap().x_token(Some(x_token)); assert!(res.is_ok()); - let res = res.unwrap().connect_lazy().build(); + let res = res.unwrap().connect_lazy(); assert!(res.is_ok()); } @@ -452,7 +445,7 @@ mod tests { let res = res.unwrap().x_token(Some(x_token)); assert!(res.is_ok()); - let res = res.unwrap().connect_lazy().build(); + let res = res.unwrap().connect_lazy(); assert!(res.is_ok()); } @@ -481,7 +474,7 @@ mod tests { let res = res.unwrap().x_token::(None); assert!(res.is_ok()); - let res = res.unwrap().connect_lazy().build(); + let res = res.unwrap().connect_lazy(); assert!(res.is_ok()); } diff --git a/yellowstone-grpc-tools/src/bin/grpc-google-pubsub.rs b/yellowstone-grpc-tools/src/bin/grpc-google-pubsub.rs index 22f715b6..ce8c3fad 100644 --- a/yellowstone-grpc-tools/src/bin/grpc-google-pubsub.rs +++ b/yellowstone-grpc-tools/src/bin/grpc-google-pubsub.rs @@ -123,8 +123,7 @@ impl ArgsAction { .timeout(Duration::from_secs(5)) .max_decoding_message_size(config.max_message_size) .connect() - .await? - .build()?; + .await?; let mut geyser = client.subscribe_once(config.request.to_proto()).await?; // Receive-send loop diff --git a/yellowstone-grpc-tools/src/bin/grpc-kafka.rs b/yellowstone-grpc-tools/src/bin/grpc-kafka.rs index 029e0e04..1a647855 100644 --- a/yellowstone-grpc-tools/src/bin/grpc-kafka.rs +++ b/yellowstone-grpc-tools/src/bin/grpc-kafka.rs @@ -231,8 +231,7 @@ impl ArgsAction { .connect_timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(5)) .connect() - .await? - .build()?; + .await?; let mut geyser = client.subscribe_once(config.request.to_proto()).await?; // Receive-send loop