Last active
October 31, 2022 11:32
-
-
Save batiati/2c64214b819d96bb74ef3ae3594fa0c8 to your computer and use it in GitHub Desktop.
TigerBeetle async maxConcurrency
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"log" | |
tb "github.com/tigerbeetledb/tigerbeetle-go" | |
"github.com/tigerbeetledb/tigerbeetle-go/pkg/types" | |
tb_types "github.com/tigerbeetledb/tigerbeetle-go/pkg/types" | |
) | |
func uint128(value string) tb_types.Uint128 { | |
x, err := tb_types.HexStringToUint128(value) | |
if err != nil { | |
panic(err) | |
} | |
return x | |
} | |
func main() { | |
// To reproduce this bug, | |
// maxConcurrency must be greater than "message_pool.messages_max_client". | |
maxConcurrency := uint(64) | |
client, err := tb.NewClient(0, []string{"3000"}, maxConcurrency) | |
if err != nil { | |
log.Printf("Error creating client: %s", err) | |
return | |
} | |
defer client.Close() | |
// Create two accounts | |
res, err := client.CreateAccounts([]tb_types.Account{ | |
{ | |
ID: uint128("1"), | |
Ledger: 1, | |
Code: 1, | |
}, | |
{ | |
ID: uint128("2"), | |
Ledger: 1, | |
Code: 1, | |
}, | |
}) | |
if err != nil { | |
log.Printf("Error creating accounts: %s", err) | |
return | |
} | |
for _, err := range res { | |
log.Printf("Error creating account %d: %s", err.Index, err.Code) | |
return | |
} | |
// This function is called in a gorountine, returning the value into a channel | |
async := func(transfer types.Transfer, channel chan []types.TransferEventResult) { | |
results, err := client.CreateTransfers([]types.Transfer{transfer}) | |
if err != nil || len(results) > 0 { | |
panic("unexpected result") | |
} | |
channel <- results | |
} | |
// Submitting all transfers at once in a gorountine | |
channel := make(chan []types.TransferEventResult, maxConcurrency) | |
for i := uint(0); i < maxConcurrency; i++ { | |
transfer := types.Transfer{ | |
ID: uint128(fmt.Sprintf("%d", i+1)), | |
CreditAccountID: uint128("1"), | |
DebitAccountID: uint128("2"), | |
Amount: 100, | |
Ledger: 1, | |
Code: 1, | |
} | |
go async(transfer, channel) | |
} | |
// Receives all results, it hangs here. | |
for i := uint(0); i < maxConcurrency; i++ { | |
results := <-channel | |
_ = results | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment