Commit 16f6f214 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 9d022803
...@@ -41,6 +41,16 @@ ...@@ -41,6 +41,16 @@
// That might be useful to pass transactions through API boundaries. // That might be useful to pass transactions through API boundaries.
// //
// //
// For convenience With is also provided to run a function in a new
// transaction, and either commit or abort it, depending on that function
// result:
//
// ok, err := transaction.With(ctx, func(ctx context.Context) error {
// ... // do something
// return err // the transaction will be committed on nil; aborted on !nil
// })
//
//
// Contrary to transaction/py there is no relation in between transaction and // Contrary to transaction/py there is no relation in between transaction and
// current thread - a transaction scope is managed completely by programmer. In // current thread - a transaction scope is managed completely by programmer. In
// particular it is possible to use one transaction in several goroutines // particular it is possible to use one transaction in several goroutines
...@@ -271,3 +281,25 @@ type Synchronizer interface { ...@@ -271,3 +281,25 @@ type Synchronizer interface {
// - either committed or aborted. // - either committed or aborted.
AfterCompletion(txn Transaction) // XXX +ctx, error? AfterCompletion(txn Transaction) // XXX +ctx, error?
} }
// ---- syntactic sugar ----
// With runs f in a new transaction, and either commits or aborts it depending on f result.
//
// If f succeeds - the transaction is committed.
// If f fails - the transaction is aborted.
//
// On return ok indicates whether f succeeded, and the error, depending on ok,
// is either the error from f, or the error from transaction commit.
//
// XXX + note?
func With(ctx context.Context, f func(context.Context) error) (ok bool, _ error) {
txn, ctx := New(ctx)
err := f(ctx)
if err != nil {
txn.Abort() // XXX err
return false, err
}
return true, txn.Commit(ctx)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment