-
Notifications
You must be signed in to change notification settings - Fork 44
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
feat: Add async transaction callbacks #2708
Merged
nasdf
merged 2 commits into
sourcenetwork:develop
from
nasdf:nasdf/feat/async-txn-callbacks
Jun 10, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,105 +43,116 @@ type Txn interface { | |
|
||
// OnDiscard registers a function to be called when the transaction is discarded. | ||
OnDiscard(fn func()) | ||
|
||
// OnSuccessAsync registers a function to be called asynchronously when the transaction is committed. | ||
OnSuccessAsync(fn func()) | ||
|
||
// OnErrorAsync registers a function to be called asynchronously when the transaction is rolled back. | ||
OnErrorAsync(fn func()) | ||
|
||
// OnDiscardAsync registers a function to be called asynchronously when the transaction is discarded. | ||
OnDiscardAsync(fn func()) | ||
} | ||
|
||
type txn struct { | ||
t ds.Txn | ||
MultiStore | ||
|
||
t ds.Txn | ||
id uint64 | ||
|
||
successFns []func() | ||
errorFns []func() | ||
discardFns []func() | ||
|
||
successAsyncFns []func() | ||
errorAsyncFns []func() | ||
discardAsyncFns []func() | ||
} | ||
|
||
var _ Txn = (*txn)(nil) | ||
|
||
// NewTxnFrom returns a new Txn from the rootstore. | ||
func NewTxnFrom(ctx context.Context, rootstore ds.TxnDatastore, id uint64, readonly bool) (Txn, error) { | ||
func newTxnFrom(ctx context.Context, rootstore ds.TxnDatastore, readonly bool) (ds.Txn, error) { | ||
// check if our datastore natively supports iterable transaction, transactions or batching | ||
if iterableTxnStore, ok := rootstore.(iterable.IterableTxnDatastore); ok { | ||
rootTxn, err := iterableTxnStore.NewIterableTransaction(ctx, readonly) | ||
if err != nil { | ||
return nil, err | ||
} | ||
multistore := MultiStoreFrom(ShimTxnStore{rootTxn}) | ||
return &txn{ | ||
rootTxn, | ||
multistore, | ||
id, | ||
[]func(){}, | ||
[]func(){}, | ||
[]func(){}, | ||
}, nil | ||
switch t := rootstore.(type) { | ||
case iterable.IterableTxnDatastore: | ||
return t.NewIterableTransaction(ctx, readonly) | ||
|
||
default: | ||
return rootstore.NewTransaction(ctx, readonly) | ||
} | ||
} | ||
|
||
rootTxn, err := rootstore.NewTransaction(ctx, readonly) | ||
// NewTxnFrom returns a new Txn from the rootstore. | ||
func NewTxnFrom(ctx context.Context, rootstore ds.TxnDatastore, id uint64, readonly bool) (Txn, error) { | ||
rootTxn, err := newTxnFrom(ctx, rootstore, readonly) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
multistore := MultiStoreFrom(ShimTxnStore{rootTxn}) | ||
return &txn{ | ||
rootTxn, | ||
multistore, | ||
id, | ||
[]func(){}, | ||
[]func(){}, | ||
[]func(){}, | ||
t: rootTxn, | ||
MultiStore: multistore, | ||
id: id, | ||
}, nil | ||
} | ||
|
||
// ID returns the unique immutable identifier for this transaction. | ||
func (t *txn) ID() uint64 { | ||
return t.id | ||
} | ||
|
||
// Commit finalizes a transaction, attempting to commit it to the Datastore. | ||
func (t *txn) Commit(ctx context.Context) error { | ||
if err := t.t.Commit(ctx); err != nil { | ||
runFns(t.errorFns) | ||
return err | ||
var fns []func() | ||
var asyncFns []func() | ||
|
||
err := t.t.Commit(ctx) | ||
if err != nil { | ||
fns = t.errorFns | ||
asyncFns = t.errorAsyncFns | ||
} else { | ||
fns = t.successFns | ||
asyncFns = t.successAsyncFns | ||
} | ||
runFns(t.successFns) | ||
return nil | ||
|
||
for _, fn := range asyncFns { | ||
go fn() | ||
} | ||
for _, fn := range fns { | ||
fn() | ||
} | ||
return err | ||
} | ||
|
||
// Discard throws away changes recorded in a transaction without committing. | ||
func (t *txn) Discard(ctx context.Context) { | ||
t.t.Discard(ctx) | ||
runFns(t.discardFns) | ||
for _, fn := range t.discardAsyncFns { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: Same here. Asyncs need to be called first. |
||
go fn() | ||
} | ||
for _, fn := range t.discardFns { | ||
fn() | ||
} | ||
} | ||
|
||
// OnSuccess registers a function to be called when the transaction is committed. | ||
func (txn *txn) OnSuccess(fn func()) { | ||
if fn == nil { | ||
return | ||
} | ||
txn.successFns = append(txn.successFns, fn) | ||
func (t *txn) OnSuccess(fn func()) { | ||
t.successFns = append(t.successFns, fn) | ||
} | ||
|
||
// OnError registers a function to be called when the transaction is rolled back. | ||
func (txn *txn) OnError(fn func()) { | ||
if fn == nil { | ||
return | ||
} | ||
txn.errorFns = append(txn.errorFns, fn) | ||
func (t *txn) OnError(fn func()) { | ||
t.errorFns = append(t.errorFns, fn) | ||
} | ||
|
||
// OnDiscard registers a function to be called when the transaction is discarded. | ||
func (txn *txn) OnDiscard(fn func()) { | ||
if fn == nil { | ||
return | ||
} | ||
txn.discardFns = append(txn.discardFns, fn) | ||
func (t *txn) OnDiscard(fn func()) { | ||
t.discardFns = append(t.discardFns, fn) | ||
} | ||
|
||
func runFns(fns []func()) { | ||
for _, fn := range fns { | ||
fn() | ||
} | ||
func (t *txn) OnSuccessAsync(fn func()) { | ||
t.successAsyncFns = append(t.successAsyncFns, fn) | ||
} | ||
|
||
func (t *txn) OnErrorAsync(fn func()) { | ||
t.errorAsyncFns = append(t.errorAsyncFns, fn) | ||
} | ||
|
||
func (t *txn) OnDiscardAsync(fn func()) { | ||
t.discardAsyncFns = append(t.discardAsyncFns, fn) | ||
} | ||
|
||
// Shim to make ds.Txn support ds.Datastore. | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: The async functions need to be called before the non-async one otherwise they are less async than they could be..