-
-
Notifications
You must be signed in to change notification settings - Fork 92
Don't eprintln in AsyncPgConnection #118
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
Conversation
3a82a22
to
fa27397
Compare
Thanks for opening this PR. I'm open to consider removing that |
Would you accept a PR to add |
The alternative would be to make a breaking change in which we return a JoinHandle from the spawned tokio task and make the user responsible for it. |
No, that's not something I would accept as of now. At least not for handling these errors.
I'm open to consider a breaking change there as long as it does not introduce changes that are specific to this connection impl (i.e which are also reasonable for the other connection implementations). You might want to provide an overview of said changes first, to prevent working on something that will not be accepted. |
Out of curiosity, why would you not accept a PR to use |
As for returning the JoinHandle, my plan would just to add a new field to AsyncPgConnection like This way the |
I have two reasons for not just using tracing for this:
I honestly do not see how that would improve the error handling as most user likely would ignore that field. Can you elaborate on how that would make things better, instead of just different? Also there is valid reasoning for the |
As it is now, user code has no way to handle when the connection fails, all that happens is a print. If they get access to the handle they can check to see whether the task is still running, for example some kind of select_all on the handle and whatever else the user's code is doing. This is very standard with how you normally use tokio, it's expected that each thing you care about is a task and you check on their status. See the tokio tutorial: https://tokio.rs/tokio/tutorial/select. Just spawning tasks and dropping their handles is discouraged. Yes they can still ignore it but at least the user has the ability to check if the connection is still alive, vs now where it's impossible. We could add a function on |
You can do exactly that by using the provided
By marking a function with That all written: I think better way to improve the situation would be to have some sort of error one-shot channel between the background task and the connection and select from that error channel on each async call on the connection type. So we would propagate these errors as normal errors to our users. Basically something like: struct AsyncPgConnection {
error_state: tokio::sync::oneshot::Receiver<diesel::result::Error>,
/// … other fields
}
impl AsyncConnection for AsyncPgConnection {
async fn establish(database_url: &str) -> ConnectionResult<Self> {
let (client, connection) = tokio_postgres::connect(database_url, tokio_postgres::NoTls)
.await
.map_err(ErrorHelper)?;
let (tx, rx) = tokio::sync::oneshot::channel();
tokio::spawn(async move {
if let Err(e) = connection.await {
rx.send(e.into());
eprintln!("connection error: {e}");
}
});
// normal connection setup here
Ok(Self {error_state: rx, // other fields})
}
fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query>
where
T: AsQuery + 'query,
T::Query: QueryFragment<Self::Backend> + QueryId + 'query,
{
let query = source.as_query();
let f = self.with_prepared_statement(query, |conn, stmt, binds| async move {
let res = conn.query_raw(&stmt, binds).await.map_err(ErrorHelper)?;
Ok(res
.map_err(|e| diesel::result::Error::from(ErrorHelper(e)))
.map_ok(PgRow::new)
.boxed())
});
tokio::select! {
error = self.error_state => return Err(error),
res = f => res,
}
}
// similar changes to other methods as well.
} I'm happy to accept a PR that implements such a change, as long as it contains some documentation and ideally some test to show that it works. |
Alright I see the advantage to this approach since the user doesn't need to handle connection errors themselves. This would necessitate a change to the error, the error returned by functions like Would you be happy with thiserror? |
Thats not needed. There is existing code for this conversion here: https://github.com/weiznich/diesel_async/blob/main/src/pg/error_helper.rs |
Awesome, in that case I'll try implement this! |
Hey hey! I've got a PR here: #121. |
Let's close this one in favor of #121 then |
Summary
I'm using this library from a CLI and I'd rather avoid these prints. Sadly there isn't much downstream folks can do about prints inside a library. If the library used
tracing
I'd switch this eprintln to use that instead, but since it doesn't, I just remove it. Let me know what you think!Test Plan