Add support for customized future executor#144
Open
Conversation
Owner
|
What runtime are you using? Why do you want to use bb8 instead of another connection pool that already supports your runtime? |
Author
|
Please see this example:
bb8 = "0.8.0"
bb8-tiberius = { version = "0.13.0", features = ["sql-browser"] }
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let host = ".\\JQZ"; //local instance `JQZ`
let conn_str = format!(
"Server={host};Database=master;TrustServerCertificate=true;IntegratedSecurity=true;"
);
let mgr = bb8_tiberius::ConnectionManager::build(conn_str.as_str())?.using_named_connection();
let pool = bb8::Pool::builder().max_size(1).build_unchecked(mgr);
let (tx_connected, rx_connected) = tokio::sync::oneshot::channel();
let thread_1 = std::thread::spawn({
let pool = pool.clone();
move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(async {
//connect in this runtime
println!("thread_1 connecting");
let conn = pool.get().await.unwrap();
println!("thread_1 connected");
tx_connected.send(());
//make `thread_2` block at the `get` call
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
//release `thread_2`
println!("thread_1 release");
drop(conn);
tokio::time::sleep(tokio::time::Duration::from_secs(0)).await;
});
println!("thread_1 runtime dropping");
//cause `thread_2` error
drop(rt);
println!("thread_1 runtime droped");
}
});
let thread_2 = std::thread::spawn({
let pool = pool.clone();
move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(async {
rx_connected.await;
//use here
println!("thread_2 getting connection");
let mut conn = pool.get().await.unwrap();
println!("thread_2 querying");
//sleep 10 seconds, should have a error `IO driver has terminated`
let e = conn.simple_query("WAITFOR DELAY '00:00:10'").await.err();
println!("thread_2 queried: {e:?}");
});
}
});
thread_1.join().unwrap();
println!("thread_1 exited");
thread_2.join().unwrap();
println!("thread_2 exited");
Ok(())
}
|
Owner
|
Okay, so you want to use a per-thread single-threaded Tokio runtime. Could have just said so... |
Author
|
Yes, there are multiple runtimes in our program,such as independent timer threads. |
djc
reviewed
Dec 7, 2022
djc
reviewed
May 31, 2023
| name = "bb8" | ||
| version = "0.8.0" | ||
| description = "Full-featured async (tokio-based) connection pool (like r2d2)" | ||
| version = "0.8.0-rc.1" |
Owner
There was a problem hiding this comment.
I won't be able to merge this PR with this stuff in it, please revert these changes.
| struct TokioExecutor; | ||
|
|
||
| impl Executor for TokioExecutor { | ||
| fn execute(&self, fut: Pin<Box<dyn Future<Output = ()> + Send>>) { |
| M: ManageConnection + Send, | ||
| { | ||
| pub(crate) statics: Builder<M>, | ||
| pub(crate) executor: Box<dyn Executor>, |
Owner
There was a problem hiding this comment.
Why does this need a trait object? It would be nice if we didn't have to Box every future just to support this fairly niche use case.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Support to customize the runtime associated with
spawnto solve the problem of across runtimes.Scenario:
The connection connected by the runtime
A, use by the runtimeB, destroyAbeforeBwhile connection are using will causeIO driver has terminated.