-
Notifications
You must be signed in to change notification settings - Fork 304
feat: Cache ballista clients on executor #1578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b1c31ac
initial implementation of client pool
milenkovicm 504f536
integrate with executor
milenkovicm 05a6b75
add option to disable eviction thread
milenkovicm 09d8d14
minor change
milenkovicm 0e23a98
address some review comments
milenkovicm 5c739e7
move client pool to new location
milenkovicm cae99e8
address review comments and refactor
milenkovicm df21989
address comments
milenkovicm 0bcbfaf
address review comments
milenkovicm 85b99b8
minor
milenkovicm bdf1f34
fmt fix
milenkovicm a06a2fd
Address comments
milenkovicm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Connection pool for `BallistaClient` instances. | ||
|
|
||
| use crate::client::BallistaClient; | ||
| use crate::error::Result; | ||
| use crate::extension::BallistaConfigGrpcEndpoint; | ||
| use crate::utils::GrpcClientConfig; | ||
| use async_trait::async_trait; | ||
| use std::fmt::Debug; | ||
| use std::ops::{Deref, DerefMut}; | ||
| use std::sync::Arc; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Trait | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Manages a pool of reusable [BallistaClient] connections. | ||
| #[async_trait] | ||
| pub trait BallistaClientPool: Send + Sync + Debug { | ||
| /// Acquire an idle client for `(host, port, config)`, or create a new one if the | ||
| /// pool is empty for that key. The returned [PooledClient] returns itself | ||
| /// to the pool on drop. | ||
| async fn acquire( | ||
| &self, | ||
| host: &str, | ||
| port: u16, | ||
| config: &GrpcClientConfig, | ||
| customize_endpoint: Option<Arc<BallistaConfigGrpcEndpoint>>, | ||
| ) -> Result<PooledClient>; | ||
|
|
||
| /// Remove all idle clients that have been sitting unused longer than the | ||
| /// configured idle timeout. | ||
| async fn evict_idle(&self); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // PooledClient guard | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// A [BallistaClient] checked out from a pool. | ||
| /// | ||
| /// Implements [Deref] / [DerefMut] so it can be used exactly like a | ||
| /// [BallistaClient]. On drop, the inner client is returned to the pool | ||
| /// automatically. Call [PooledClient::discard] before dropping if the | ||
| /// connection should **not** be reused (e.g. after a transport error). | ||
| pub struct PooledClient { | ||
| client: BallistaClient, | ||
| /// Invoked in `Drop::drop` to push the client back into the idle deque. | ||
| /// `None` after `discard()` is called. | ||
| return_fn: Option<Box<dyn FnOnce(BallistaClient) + Send>>, | ||
| } | ||
|
|
||
| impl PooledClient { | ||
| /// Creates new PooledClient | ||
| pub fn new( | ||
| client: BallistaClient, | ||
| return_fn: Box<dyn FnOnce(BallistaClient) + Send>, | ||
| ) -> Self { | ||
| Self { | ||
| client, | ||
| return_fn: Some(return_fn), | ||
| } | ||
| } | ||
|
|
||
| /// Close the connection instead of returning it to the pool. | ||
| pub fn discard(mut self) { | ||
| self.return_fn = None; | ||
| } | ||
| } | ||
|
|
||
| impl Deref for PooledClient { | ||
| type Target = BallistaClient; | ||
| fn deref(&self) -> &Self::Target { | ||
| &self.client | ||
| } | ||
| } | ||
|
|
||
| impl DerefMut for PooledClient { | ||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||
| &mut self.client | ||
| } | ||
| } | ||
|
|
||
| impl Drop for PooledClient { | ||
| fn drop(&mut self) { | ||
| if let Some(f) = self.return_fn.take() { | ||
| let client = self.client.clone(); | ||
| f(client); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no
#[cfg(test)].Does it need to be here ? It could be a helper method in
mod teststooUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should but apparently it does not work across different crates, or I'm doing something wrong