forked from tigerbeetle/tigerbeetle-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
455 lines (383 loc) · 10.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package tigerbeetle_go
/*
#cgo LDFLAGS: -L./ -lclient_c
#include <stdint.h>
#include <stdlib.h>
#include "./internal/client_c/client.h"
void onResult(void* const user_data, uint8_t error, uint8_t operation, uint8_t* const results, const uint32_t results_length);
*/
import "C"
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"strings"
"sync"
"unsafe"
tberrors "github.com/coilhq/tigerbeetle_go/pkg/errors"
"github.com/coilhq/tigerbeetle_go/pkg/types"
)
// Go may only pass a pointer to C provided that it does not point to memory that
// contains Go pointers. https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers.
// We therefore keep a registry of user data here that can be accessed when we
// receive a response from the C client.
var userDataMap map[uint32]*UserData = make(map[uint32]*UserData)
var userDataIndex uint32 = 0
// We acquire a lock when accessing the userDataMap.
var mutex sync.Mutex
//export onResult
func onResult(user_data_raw *C.void, error uint8, operation uint8, results *uint8, results_length uint32) {
index := *(*uint32)((unsafe.Pointer)(user_data_raw))
userData := getUserData(index)
cb := userData.Callback
ctx := userData.Ctx
// copy results as memory lifetime is not guaranteed.
result_slice := unsafe.Slice(results, results_length)
ret := make([]byte, results_length)
r := bytes.NewReader(result_slice)
err := binary.Read(r, binary.LittleEndian, &ret)
if err != nil {
fmt.Println("onResult: binary.Read failed:", err)
}
cb(ctx, err, types.Operation(operation), ret)
}
type UserData struct {
Ctx interface{}
Callback ResultsCallback
}
type ResultsCallback func(ctx interface{}, err error, operation types.Operation, results []byte)
type Client interface {
CreateAccounts(batch []types.Account) ([]types.EventResult, error)
LookupAccounts(batch []types.Uint128) ([]types.Account, error)
CreateTransfers(batch []types.Transfer) ([]types.EventResult, error)
CommitTransfers(batch []types.Commit) ([]types.EventResult, error)
LookupTransfers(batch []types.Uint128) ([]types.Transfer, error)
Request(userCtx interface{}, callback ResultsCallback, operation types.Operation, batch []byte) error
Tick()
Deinit()
}
type client struct {
ctx *C.TB_Context
}
func NewClient(clusterID uint32, addresses []string) (Client, error) {
tbCtx := &C.TB_Context{}
addresses_raw := strings.Join(addresses[:], ",")
cstring := C.CString(addresses_raw)
defer C.free(unsafe.Pointer(cstring))
err := C.init(tbCtx, C.uint32_t(clusterID), cstring, C.uint32_t(len(addresses_raw)))
if err != 0 {
return nil, errors.New("Failed to create client.")
}
return &client{ctx: tbCtx}, nil
}
// Raw request method based on callbacks for flexibility.
func (s *client) Request(userCtx interface{}, callback ResultsCallback, operation types.Operation, batch []byte) error {
userData := &UserData{
Ctx: userCtx,
Callback: callback,
}
index := storeUserData(userData)
cErr := C.request(
s.ctx,
unsafe.Pointer(&index),
(C.results_callback)(C.onResult),
(C.uchar)(operation),
(*C.uchar)(&batch[0]),
(C.uint32_t)(len(batch)),
)
if cErr != 0 {
return tberrors.ErrorCast(int(cErr), "Failed to create accounts")
}
return nil
}
func (s *client) Tick() {
C.tick(s.ctx)
}
func (s *client) Deinit() {
C.deinit(s.ctx)
}
func storeUserData(userData *UserData) uint32 {
mutex.Lock()
defer mutex.Unlock()
userDataIndex++
userDataMap[userDataIndex] = userData
return userDataIndex
}
func getUserData(index uint32) *UserData {
mutex.Lock()
defer mutex.Unlock()
userData, found := userDataMap[index]
if !found {
return nil
}
delete(userDataMap, index)
return userData
}
// From here on are wrappers that provide typings and use channels to block until the reponse is received from the
// server.
type resultsOrError struct {
results []types.EventResult
err error
}
func onCreateAccounts(ctx interface{}, err error, operation types.Operation, results []byte) {
responseChannel := ctx.(chan resultsOrError)
defer close(responseChannel)
if operation != types.CREATE_ACCOUNT || len(results)%8 != 0 {
responseChannel <- resultsOrError{
results: nil,
err: errors.New("Did not receive a create account result."),
}
return
}
// TODO: convert to EventResults without copying
events := len(results) / 8 // event result { index: u32, code: u32 }
ret := make([]types.EventResult, events)
r := bytes.NewReader(results)
err = binary.Read(r, binary.LittleEndian, &ret)
if err != nil {
responseChannel <- resultsOrError{
results: nil,
err: err,
}
return
}
responseChannel <- resultsOrError{
results: ret,
err: nil,
}
}
func (s *client) CreateAccounts(batch []types.Account) ([]types.EventResult, error) {
// TODO: convert to []byte without copying
buf := new(bytes.Buffer)
for _, account := range batch {
err := binary.Write(buf, binary.LittleEndian, account)
if err != nil {
return nil, err
}
}
responseChannel := make(chan resultsOrError)
err := s.Request(responseChannel, onCreateAccounts, types.CREATE_ACCOUNT, buf.Bytes())
if err != nil {
return nil, err
}
for {
select {
case res := <-responseChannel:
if res.err != nil {
return nil, res.err
}
return res.results, nil
}
}
}
type accountLookupsOrError struct {
accounts []types.Account
err error
}
func onAccountLookupResults(ctx interface{}, err error, operation types.Operation, results []byte) {
responseChannel := ctx.(chan accountLookupsOrError)
defer close(responseChannel)
if operation != types.ACCOUNT_LOOKUP || len(results)%128 != 0 {
responseChannel <- accountLookupsOrError{
accounts: nil,
err: errors.New("Did not receive account lookup results."),
}
return
}
// TODO: convert to Account without copying
accounts := make([]types.Account, len(results)/128)
r := bytes.NewReader(results)
err = binary.Read(r, binary.LittleEndian, &accounts)
if err != nil {
responseChannel <- accountLookupsOrError{
accounts: nil,
err: err,
}
return
}
responseChannel <- accountLookupsOrError{
accounts: accounts,
err: nil,
}
}
func (s *client) LookupAccounts(batch []types.Uint128) ([]types.Account, error) {
// TODO: convert to []byte without copying
buf := new(bytes.Buffer)
for _, accountID := range batch {
err := binary.Write(buf, binary.LittleEndian, accountID)
if err != nil {
return nil, err
}
}
responseChannel := make(chan accountLookupsOrError)
err := s.Request(responseChannel, onAccountLookupResults, types.ACCOUNT_LOOKUP, buf.Bytes())
if err != nil {
return nil, err
}
for {
select {
case res := <-responseChannel:
if res.err != nil {
return nil, res.err
}
return res.accounts, nil
}
}
}
func onCreateTransfer(ctx interface{}, err error, operation types.Operation, results []byte) {
responseChannel := ctx.(chan resultsOrError)
defer close(responseChannel)
if operation != types.CREATE_TRANSFER || len(results)%8 != 0 {
responseChannel <- resultsOrError{
results: nil,
err: errors.New("Did not receive a create transfer result."),
}
return
}
// TODO: convert to EventResults without copying
events := len(results) / 8 // event result { index: u32, code: u32 }
ret := make([]types.EventResult, events)
r := bytes.NewReader(results)
err = binary.Read(r, binary.LittleEndian, &ret)
if err != nil {
responseChannel <- resultsOrError{
results: nil,
err: err,
}
return
}
responseChannel <- resultsOrError{
results: ret,
err: nil,
}
}
func (s *client) CreateTransfers(batch []types.Transfer) ([]types.EventResult, error) {
// TODO: convert to []byte without copying
buf := new(bytes.Buffer)
for _, transfer := range batch {
err := binary.Write(buf, binary.LittleEndian, transfer)
if err != nil {
return nil, err
}
}
responseChannel := make(chan resultsOrError)
err := s.Request(responseChannel, onCreateTransfer, types.CREATE_TRANSFER, buf.Bytes())
if err != nil {
return nil, err
}
for {
select {
case res := <-responseChannel:
if res.err != nil {
return nil, res.err
}
return res.results, nil
}
}
}
func onCommitTransfer(ctx interface{}, err error, operation types.Operation, results []byte) {
responseChannel := ctx.(chan resultsOrError)
defer close(responseChannel)
if operation != types.COMMIT_TRANSFER || len(results)%8 != 0 {
responseChannel <- resultsOrError{
results: nil,
err: errors.New("Did not receive a commit transfer result."),
}
return
}
// TODO: convert to EventResults without copying
events := len(results) / 8 // event result { index: u32, code: u32 }
ret := make([]types.EventResult, events)
r := bytes.NewReader(results)
err = binary.Read(r, binary.LittleEndian, &ret)
if err != nil {
responseChannel <- resultsOrError{
results: nil,
err: err,
}
return
}
responseChannel <- resultsOrError{
results: ret,
err: nil,
}
}
func (s *client) CommitTransfers(batch []types.Commit) ([]types.EventResult, error) {
// TODO: convert to []byte without copying
buf := new(bytes.Buffer)
for _, commit := range batch {
err := binary.Write(buf, binary.LittleEndian, commit)
if err != nil {
return nil, err
}
}
responseChannel := make(chan resultsOrError)
err := s.Request(responseChannel, onCommitTransfer, types.COMMIT_TRANSFER, buf.Bytes())
if err != nil {
return nil, err
}
for {
select {
case res := <-responseChannel:
if res.err != nil {
return nil, res.err
}
return res.results, nil
}
}
}
type transferLookupsOrError struct {
transfers []types.Transfer
err error
}
func onTransferLookup(ctx interface{}, err error, operation types.Operation, results []byte) {
responseChannel := ctx.(chan transferLookupsOrError)
defer close(responseChannel)
if operation != types.TRANSFER_LOOKUP || len(results)%128 != 0 {
responseChannel <- transferLookupsOrError{
transfers: nil,
err: errors.New("Did not receive transfer lookup results."),
}
return
}
// TODO: convert to Account without copying
transfers := make([]types.Transfer, len(results)/128)
r := bytes.NewReader(results)
err = binary.Read(r, binary.LittleEndian, &transfers)
if err != nil {
responseChannel <- transferLookupsOrError{
transfers: nil,
err: err,
}
return
}
responseChannel <- transferLookupsOrError{
transfers: transfers,
err: nil,
}
}
func (s *client) LookupTransfers(batch []types.Uint128) ([]types.Transfer, error) {
// TODO: convert to []byte without copying
buf := new(bytes.Buffer)
for _, transferID := range batch {
err := binary.Write(buf, binary.LittleEndian, transferID)
if err != nil {
return nil, err
}
}
responseChannel := make(chan transferLookupsOrError)
err := s.Request(responseChannel, onTransferLookup, types.TRANSFER_LOOKUP, buf.Bytes())
if err != nil {
return nil, err
}
for {
select {
case res := <-responseChannel:
if res.err != nil {
return nil, res.err
}
return res.transfers, nil
}
}
}