Skip to content

fix: Replace r2d2 with deadpool#3

Merged
DanielVoogsgerd merged 2 commits intomainfrom
deadpool
Jun 26, 2025
Merged

fix: Replace r2d2 with deadpool#3
DanielVoogsgerd merged 2 commits intomainfrom
deadpool

Conversation

@DanielVoogsgerd
Copy link

This seems like it integrates nicer with async.

Copy link

@Lut99 Lut99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's surprisingly drop-in at the moment, which is nice! But see my comment ~ I'm surprised the main body of the work still requires a lock and tokio::block_in_place. If it's possible to remove that, we should (although I also understand it if you want that as a separate PR to get the lib-refactor out of the door) xD

That said, maybe this is a problem of diesel rather than deadpool. Either way, I was surprised.

debug!("Starting transaction...");
tokio::task::block_in_place(move || {
self.conn.exclusive_transaction(|conn| -> Result<u64, Self::Error> {
self.conn.lock().unwrap().exclusive_transaction(|conn| -> Result<u64, Self::Error> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little surprised that this isn't async. Is that our doing that there's a lock there? If we can remove it, that'd be lovely, then we can also get rid of the block_in_place()-call but remain in an async context.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let's split this into two things.

I do not fully understand why this lock is there. I would have expected the connection pool to guarantee that no connection is aliased.

Besides that, I think it's not async because the underlying libs are not sync. deadpool diesel makes use of the deadpool sync library that is meant for sync interfaces. We can do away with block_in_place by using self.conn.interact(|| {}). But this simply is sugar for a thread_spawn the downside of doing that is that the captured variables need to be Sync. Not always a problem, but a guarantee I cannot make with my limited knowledge of this new project.

I did read that block_in_place can be a pain at times, although I haven't run into that problem yet.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to rewrite it using interact? I can add Sync bound to Self::Content I think.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not badly enough to warrant blocking the merge ~ but one day, probably worth it :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, keep in mind that it would be a breaking change then because the trait bound becomes more restrictive.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it was such a little change, I did it anyway. Let me know if you like this better, moving it back to the old variant is fine too.

This seems like it integrates nicer with async.
Mostly syntactic sugar, but also nicely moves our tracing spans to the
new thread.
@Lut99
Copy link

Lut99 commented Jun 26, 2025

Looks good Daniel! And even a green checkmark across the board :)

@DanielVoogsgerd DanielVoogsgerd merged commit af5af07 into main Jun 26, 2025
18 checks passed
@DanielVoogsgerd DanielVoogsgerd deleted the deadpool branch June 26, 2025 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants