Skip to content

Implement transactions operating on async closures.#33

Merged
smklein merged 1 commit into
masterfrom
my-async-txns
Aug 22, 2022
Merged

Implement transactions operating on async closures.#33
smklein merged 1 commit into
masterfrom
my-async-txns

Conversation

@smklein

@smklein smklein commented Aug 19, 2022

Copy link
Copy Markdown
Collaborator

Fixes: #32

This provides an additional method to the AsyncConnection trait: transaction_async.

transaction_async acts similarly to transaction, but instead of operating on a closure (which expects to be synchronous), transaction_async can operate on an asynchronous block.

@smklein

smklein commented Aug 19, 2022

Copy link
Copy Markdown
Collaborator Author

Usage of this PR is demonstrated in oxidecomputer/omicron#1621

@smklein

smklein commented Aug 19, 2022

Copy link
Copy Markdown
Collaborator Author

This PR is also inspired by https://github.com/weiznich/diesel_async/blob/acf3ed3a2d7017098f2eb834633839d75d7256c7/src/pg/mod.rs#L206-L211 , which is the interface used in the (currently still prototype) async interface proposed to Diesel.

It's a goal of async-bb8-diesel to be fairly closely aligned with that crate, to make conversion to it easy as it gains more stability.

@jmpesp jmpesp left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🚀.await

@smklein smklein merged commit 1c51fab into master Aug 22, 2022
@smklein smklein deleted the my-async-txns branch August 22, 2022 15:25
smklein added a commit to oxidecomputer/omicron that referenced this pull request Sep 12, 2022
This PR is dependent on oxidecomputer/async-bb8-diesel#33 , and acts as a demonstration of asynchronous transaction blocks.

This PR should be a big step forward for eliminating "special-case synchronous code" to access the DB within transactions.

Whenever someone reaches for an interactive transaction, we'd prefer:
- Pipelined transactions (not fully implemented, does not cover cases where we read-then-write)
- Sagas (for DB-only operations, this is technically wasteful if we could do the same calculations in SQL)
- CTEs (this is generally the "ideal" solution, but bears the highest complexity)

*however*, given that we are trying to strike a balance between correctness, speed, safety, and developer productivity, this PR provides a cheap way for the "fastest option" - just writing normal async code in rust - to remain async, and interoperate with the rest of the codebase easily.
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.

Support for async transaction functions

2 participants