Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Kirill Smelkov
neo
Commits
16f6f214
Commit
16f6f214
authored
Jul 25, 2018
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
9d022803
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
0 deletions
+32
-0
go/transaction/api.go
go/transaction/api.go
+32
-0
No files found.
go/transaction/api.go
View file @
16f6f214
...
@@ -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
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment