-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lesson 56: Send async tasks to Redis with DB Transaction
- Loading branch information
1 parent
5d68e66
commit 15d7ff1
Showing
6 changed files
with
167 additions
and
109 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package db | ||
|
||
import "context" | ||
|
||
// CreateUserTxParams contains the input parameters of the create user transaction | ||
type CreateUserTxParams struct { | ||
CreateUserParams | ||
AfterCreate func(user User) error | ||
} | ||
|
||
// CreateUserTxResult is the result of the create user transaction | ||
type CreateUserTxResult struct { | ||
User User | ||
} | ||
|
||
// CreateUserTx performs a | ||
func (s *SQLStore) CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error) { | ||
var result CreateUserTxResult | ||
|
||
err := s.execTx(ctx, func(q *Queries) error { | ||
var err error | ||
|
||
result.User, err = q.CreateUser(ctx, arg.CreateUserParams) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return arg.AfterCreate(result.User) // call the callback function to allow for retry if there is an error | ||
}) | ||
|
||
return result, err | ||
} |
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package db | ||
|
||
import "context" | ||
|
||
// TransferTxParams contains the input parameters of the transfer transaction | ||
type TransferTxParams struct { | ||
FromAccountID int64 `json:"from_account_id"` | ||
ToAccountID int64 `json:"to_account_id"` | ||
Amount int64 `json:"amount"` | ||
} | ||
|
||
// TransferTxResult is the result of the transfer transaction | ||
type TransferTxResult struct { | ||
Transfer Transfer `json:"transfer"` | ||
FromAccount Account `json:"from_account"` | ||
ToAccount Account `json:"to_account"` | ||
FromEntry Entry `json:"from_entry"` | ||
ToEntry Entry `json:"to_entry"` | ||
} | ||
|
||
// TransferTx performs a money transfer from one account to the other | ||
// create a transfer record, add account entries, update accounts' balance within a single datase transaction | ||
func (s *SQLStore) TransferTx(ctx context.Context, arg TransferTxParams) (TransferTxResult, error) { | ||
var result TransferTxResult | ||
|
||
err := s.execTx(ctx, func(q *Queries) error { | ||
var err error | ||
|
||
result.Transfer, err = q.CreateTransfer(ctx, CreateTransferParams{ | ||
FromAccountID: arg.FromAccountID, | ||
ToAccountID: arg.ToAccountID, | ||
Amount: arg.Amount, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result.FromEntry, err = q.CreateEntry(ctx, CreateEntryParams{ | ||
AccountID: arg.FromAccountID, | ||
Amount: -arg.Amount, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result.ToEntry, err = q.CreateEntry(ctx, CreateEntryParams{ | ||
AccountID: arg.ToAccountID, | ||
Amount: arg.Amount, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// update accounts' balance | ||
if arg.FromAccountID < arg.ToAccountID { | ||
result.FromAccount, result.ToAccount, _ = addMoney(ctx, q, arg.FromAccountID, -arg.Amount, arg.ToAccountID, arg.Amount) | ||
} else { | ||
result.ToAccount, result.FromAccount, _ = addMoney(ctx, q, arg.ToAccountID, arg.Amount, arg.FromAccountID, -arg.Amount) | ||
} | ||
|
||
// return nil | ||
return err | ||
}) | ||
|
||
return result, err | ||
} | ||
|
||
func addMoney(ctx context.Context, q *Queries, accountID1, amount1, accountID2, amount2 int64) (account1, account2 Account, err error) { | ||
account1, err = q.AddAccountBalance(ctx, AddAccountBalanceParams{ | ||
ID: accountID1, | ||
Amount: amount1, | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
|
||
account2, err = q.AddAccountBalance(ctx, AddAccountBalanceParams{ | ||
ID: accountID2, | ||
Amount: amount2, | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} |
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
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