-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathengine.go
More file actions
921 lines (810 loc) · 22.3 KB
/
engine.go
File metadata and controls
921 lines (810 loc) · 22.3 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
package match
import (
"context"
"encoding/binary"
"encoding/json"
"errors"
"hash/crc32"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/quagmt/udecimal"
"github.com/0x5487/matching-engine/protocol"
)
// MatchingEngine manages multiple order books for different markets.
// It uses a single shared RingBuffer (Disruptor) for all commands,
// allowing the entire event loop to run on a single goroutine.
// This enables runtime.LockOSThread() for CPU affinity scenarios.
type MatchingEngine struct {
isShutdown atomic.Bool
engineID string
orderbooks map[string]*OrderBook
ring *RingBuffer[InputEvent]
publishTrader Publisher
responsePool *sync.Pool
}
const (
defaultRingBufferSize = 32768
snapshotTimeout = 10 * time.Second
tmpDirPerm = 0o750
metaFilePerm = 0o600
footerSizeLimit = 4294967295
footerLenSize = 4
)
// NewMatchingEngine creates a new matching engine instance.
func NewMatchingEngine(engineID string, publishTrader Publisher) *MatchingEngine {
engine := &MatchingEngine{
engineID: engineID,
orderbooks: make(map[string]*OrderBook),
publishTrader: publishTrader,
responsePool: &sync.Pool{
New: func() any {
return make(chan any, 1)
},
},
}
engine.ring = NewRingBuffer(defaultRingBufferSize, &engineEventHandler{engine})
return engine
}
// engineEventHandler is a private wrapper to hide the onEvent method from the public API.
type engineEventHandler struct {
engine *MatchingEngine
}
func (h *engineEventHandler) OnEvent(ev *InputEvent) {
h.engine.onEvent(ev)
}
// CreateMarket enqueues a typed create-market request and returns a future for completion.
func (engine *MatchingEngine) CreateMarket(
ctx context.Context,
req *protocol.CreateMarketRequest,
) (*Future[bool], error) {
if err := engine.validateTypedRequest(ctx, req); err != nil {
return nil, err
}
respChan := engine.acquireResponseChannel()
if err := engine.enqueue(ctx, req, nil, respChan); err != nil {
engine.releaseResponseChannel(respChan)
return nil, err
}
return &Future[bool]{
engine: engine,
respChan: respChan,
}, nil
}
// SuspendMarket enqueues a typed suspend-market request and returns a future for completion.
func (engine *MatchingEngine) SuspendMarket(
ctx context.Context,
req *protocol.SuspendMarketRequest,
) (*Future[bool], error) {
if err := engine.validateTypedRequest(ctx, req); err != nil {
return nil, err
}
respChan := engine.acquireResponseChannel()
if err := engine.enqueue(ctx, req, nil, respChan); err != nil {
engine.releaseResponseChannel(respChan)
return nil, err
}
return &Future[bool]{
engine: engine,
respChan: respChan,
}, nil
}
// ResumeMarket enqueues a typed resume-market request and returns a future for completion.
func (engine *MatchingEngine) ResumeMarket(
ctx context.Context,
req *protocol.ResumeMarketRequest,
) (*Future[bool], error) {
if err := engine.validateTypedRequest(ctx, req); err != nil {
return nil, err
}
respChan := engine.acquireResponseChannel()
if err := engine.enqueue(ctx, req, nil, respChan); err != nil {
engine.releaseResponseChannel(respChan)
return nil, err
}
return &Future[bool]{
engine: engine,
respChan: respChan,
}, nil
}
// UpdateConfig enqueues a typed update-config request and returns a future for completion.
func (engine *MatchingEngine) UpdateConfig(
ctx context.Context,
req *protocol.UpdateConfigRequest,
) (*Future[bool], error) {
if err := engine.validateTypedRequest(ctx, req); err != nil {
return nil, err
}
respChan := engine.acquireResponseChannel()
if err := engine.enqueue(ctx, req, nil, respChan); err != nil {
engine.releaseResponseChannel(respChan)
return nil, err
}
return &Future[bool]{
engine: engine,
respChan: respChan,
}, nil
}
// PlaceOrderAsync enqueues a typed place-order request without waiting for execution.
func (engine *MatchingEngine) PlaceOrderAsync(ctx context.Context, req *protocol.PlaceOrderRequest) error {
if err := engine.validateTypedRequest(ctx, req); err != nil {
return err
}
return engine.enqueue(ctx, req, nil, nil)
}
// PlaceOrderBatchAsync enqueues a batch of typed place-order requests without waiting for execution.
func (engine *MatchingEngine) PlaceOrderBatchAsync(
ctx context.Context,
reqs []*protocol.PlaceOrderRequest,
) error {
if len(reqs) == 0 {
return nil
}
if ctx == nil {
return ErrInvalidParam
}
requests := make([]any, 0, len(reqs))
for _, req := range reqs {
if err := engine.validateTypedRequest(ctx, req); err != nil {
return err
}
requests = append(requests, req)
}
return engine.enqueueBatch(ctx, requests)
}
// CancelOrderAsync enqueues a typed cancel-order request without waiting for execution.
func (engine *MatchingEngine) CancelOrderAsync(ctx context.Context, req *protocol.CancelOrderRequest) error {
if err := engine.validateTypedRequest(ctx, req); err != nil {
return err
}
return engine.enqueue(ctx, req, nil, nil)
}
// AmendOrderAsync enqueues a typed amend-order request without waiting for execution.
func (engine *MatchingEngine) AmendOrderAsync(ctx context.Context, req *protocol.AmendOrderRequest) error {
if err := engine.validateTypedRequest(ctx, req); err != nil {
return err
}
return engine.enqueue(ctx, req, nil, nil)
}
// SendUserEvent enqueues a typed user event request without waiting for execution.
func (engine *MatchingEngine) SendUserEvent(ctx context.Context, req *protocol.UserEventRequest) error {
if err := engine.validateTypedRequest(ctx, req); err != nil {
return err
}
return engine.enqueue(ctx, req, nil, nil)
}
// Run starts the engine's event loop. This is a blocking call.
// The consumer loop runs on the calling goroutine, enabling the caller
// to control thread affinity via runtime.LockOSThread().
//
// Usage:
//
// go func() {
// runtime.LockOSThread()
// defer runtime.UnlockOSThread()
// engine.Run()
// }()
func (engine *MatchingEngine) Run() error {
engine.ring.Run()
return nil
}
// GetDepth executes a read-only depth query against the matching engine.
func (engine *MatchingEngine) GetDepth(
ctx context.Context,
query *protocol.GetDepthQuery,
) (*Future[*protocol.GetDepthResponse], error) {
if query == nil {
return nil, ErrInvalidParam
}
if engine.isShutdown.Load() {
return nil, ErrShutdown
}
respChan := engine.acquireResponseChannel()
if err := engine.enqueue(ctx, nil, query, respChan); err != nil {
engine.releaseResponseChannel(respChan)
return nil, err
}
return &Future[*protocol.GetDepthResponse]{
engine: engine,
respChan: respChan,
}, nil
}
// GetStats executes a read-only statistics query against the matching engine.
func (engine *MatchingEngine) GetStats(
ctx context.Context,
query *protocol.GetStatsQuery,
) (*Future[*protocol.GetStatsResponse], error) {
if query == nil {
return nil, ErrInvalidParam
}
if engine.isShutdown.Load() {
return nil, ErrShutdown
}
respChan := engine.acquireResponseChannel()
if err := engine.enqueue(ctx, nil, query, respChan); err != nil {
engine.releaseResponseChannel(respChan)
return nil, err
}
return &Future[*protocol.GetStatsResponse]{
engine: engine,
respChan: respChan,
}, nil
}
// Shutdown gracefully shuts down the engine.
// It blocks until all pending commands in the RingBuffer are processed
// or the context is canceled.
func (engine *MatchingEngine) Shutdown(ctx context.Context) error {
engine.isShutdown.Store(true)
return engine.ring.Shutdown(ctx)
}
// snapshotResult wraps a snapshot result with potential error.
type snapshotResult struct {
snap *OrderBookSnapshot
err error
}
type snapshotQuery struct{}
// TakeSnapshot captures a consistent snapshot of all order books and writes them to the specified directory.
// It generates two files: `snapshot.bin` (binary data) and `metadata.json` (metadata).
// Returns the metadata object or an error.
func (engine *MatchingEngine) TakeSnapshot(
ctx context.Context,
outputDir string,
) (*SnapshotMetadata, error) {
if engine.isShutdown.Load() {
return nil, ErrShutdown
}
respChan := engine.acquireResponseChannel()
defer engine.releaseResponseChannel(respChan)
query := &snapshotQuery{}
if err := engine.enqueue(ctx, nil, query, respChan); err != nil {
return nil, err
}
var results []snapshotResult
select {
case res := <-respChan:
r, ok := res.([]snapshotResult)
if !ok {
return nil, errors.New("unexpected response type for snapshot")
}
results = r
case <-ctx.Done():
return nil, ctx.Err()
}
return engine.writeSnapshot(outputDir, results)
}
// RestoreFromSnapshot restores the entire matching engine state from a snapshot.
func (engine *MatchingEngine) RestoreFromSnapshot(inputDir string) (*SnapshotMetadata, error) {
meta, err := engine.readMetadata(inputDir)
if err != nil {
return nil, err
}
binPath := filepath.Join(inputDir, "snapshot.bin")
fileChecksum, err := calculateFileCRC32(binPath)
if err != nil {
return nil, err
}
if fileChecksum != meta.SnapshotChecksum {
return nil, errors.New("snapshot.bin checksum mismatch")
}
binFile, err := os.Open(filepath.Clean(binPath))
if err != nil {
return nil, err
}
defer binFile.Close()
footer, footerOffset, err := engine.readFooter(binFile)
if err != nil {
return nil, err
}
for _, segment := range footer.Markets {
if err := engine.restoreMarket(binFile, segment, footerOffset); err != nil {
return nil, err
}
}
return meta, nil
}
func (engine *MatchingEngine) validateTypedRequest(ctx context.Context, req any) error {
if ctx == nil || req == nil {
return ErrInvalidParam
}
base, ok := protocol.GetRequestBase(req)
if !ok {
return ErrInvalidParam
}
if err := requireCommandID(base.CommandID); err != nil {
return err
}
if engine.isShutdown.Load() {
return ErrShutdown
}
return nil
}
func (engine *MatchingEngine) enqueue(
ctx context.Context,
request any,
query any,
resp chan any,
) error {
strategy := YieldingIdleStrategy{}
for {
seq, ev := engine.ring.TryClaim()
if seq != -1 {
ev.Request = request
ev.Query = query
ev.Resp = resp
engine.ring.Commit(seq)
return nil
}
if engine.isShutdown.Load() {
return ErrShutdown
}
if err := ctx.Err(); err != nil {
return err
}
strategy.Idle()
}
}
func (engine *MatchingEngine) enqueueBatch(ctx context.Context, requests []any) error {
if len(requests) == 0 {
return nil
}
if engine.isShutdown.Load() {
return ErrShutdown
}
n := int64(len(requests))
if n > engine.ring.capacity {
for _, request := range requests {
if err := engine.enqueue(ctx, request, nil, nil); err != nil {
return err
}
}
return nil
}
strategy := YieldingIdleStrategy{}
for {
startSeq, endSeq := engine.ring.TryClaimN(n)
if startSeq != -1 {
for i, request := range requests {
seq := startSeq + int64(i)
slot := &engine.ring.buffer[seq&engine.ring.bufferMask]
slot.Request = request
slot.Query = nil
slot.Resp = nil
}
engine.ring.CommitN(startSeq, endSeq)
return nil
}
if engine.isShutdown.Load() {
return ErrShutdown
}
if err := ctx.Err(); err != nil {
return err
}
strategy.Idle()
}
}
func (engine *MatchingEngine) writeSnapshot(
outputDir string,
results []snapshotResult,
) (*SnapshotMetadata, error) {
tmpDir := outputDir + ".tmp"
if err := os.RemoveAll(tmpDir); err != nil {
return nil, err
}
if err := os.MkdirAll(tmpDir, tmpDirPerm); err != nil {
return nil, err
}
binPath := filepath.Join(tmpDir, "snapshot.bin")
binFile, err := os.Create(filepath.Clean(binPath))
if err != nil {
return nil, err
}
var globalSeqID uint64
markets := make([]MarketSegment, 0, len(results))
currentOffset := int64(0)
var snapshotErrors []error
for _, result := range results {
if result.err != nil {
snapshotErrors = append(snapshotErrors, result.err)
continue
}
snap := result.snap
snapData, errMarshal := json.Marshal(snap)
if errMarshal != nil {
_ = binFile.Close()
return nil, errMarshal
}
n, errWrite := binFile.Write(snapData)
if errWrite != nil {
_ = binFile.Close()
return nil, errWrite
}
length := int64(n)
markets = append(markets, MarketSegment{
MarketID: snap.MarketID,
Offset: currentOffset,
Length: length,
Checksum: crc32.ChecksumIEEE(snapData),
})
currentOffset += length
if snap.LastCmdSeqID > globalSeqID {
globalSeqID = snap.LastCmdSeqID
}
}
if len(snapshotErrors) > 0 {
_ = binFile.Close()
return nil, errors.Join(snapshotErrors...)
}
if errFooter := engine.writeFooter(binFile, markets); errFooter != nil {
_ = binFile.Close()
return nil, errFooter
}
if errSync := binFile.Sync(); errSync != nil {
_ = binFile.Close()
return nil, errSync
}
if errClose := binFile.Close(); errClose != nil {
return nil, errClose
}
snapshotChecksum, errCRC := calculateFileCRC32(binPath)
if errCRC != nil {
return nil, errCRC
}
meta := &SnapshotMetadata{
SchemaVersion: SnapshotSchemaVersion,
Timestamp: time.Now().UnixNano(),
GlobalLastCmdSeqID: globalSeqID,
EngineVersion: EngineVersion,
SnapshotChecksum: snapshotChecksum,
}
if errMeta := engine.writeMetadata(tmpDir, meta); errMeta != nil {
return nil, errMeta
}
if errRemove := os.RemoveAll(outputDir); errRemove != nil {
return nil, errRemove
}
return meta, os.Rename(tmpDir, outputDir)
}
func (engine *MatchingEngine) writeFooter(f *os.File, markets []MarketSegment) error {
footer := SnapshotFileFooter{Markets: markets}
footerData, err := json.Marshal(footer)
if err != nil {
return err
}
if _, err = f.Write(footerData); err != nil {
return err
}
if len(footerData) > footerSizeLimit {
return errors.New("footer too large")
}
/* #nosec G115 */
return binary.Write(f, binary.BigEndian, uint32(len(footerData)))
}
func (engine *MatchingEngine) writeMetadata(dir string, meta *SnapshotMetadata) error {
metaBytes, err := json.MarshalIndent(meta, "", " ")
if err != nil {
return err
}
metaPath := filepath.Join(dir, "metadata.json")
return os.WriteFile(metaPath, metaBytes, metaFilePerm)
}
func (engine *MatchingEngine) readMetadata(dir string) (*SnapshotMetadata, error) {
metaPath := filepath.Join(dir, "metadata.json")
metaBytes, err := os.ReadFile(filepath.Clean(metaPath))
if err != nil {
return nil, err
}
var meta SnapshotMetadata
if err = json.Unmarshal(metaBytes, &meta); err != nil {
return nil, err
}
return &meta, nil
}
func (engine *MatchingEngine) readFooter(f *os.File) (*SnapshotFileFooter, int64, error) {
stat, err := f.Stat()
if err != nil {
return nil, 0, err
}
fileSize := stat.Size()
footerLenBytes := make([]byte, footerLenSize)
if _, err = f.ReadAt(footerLenBytes, fileSize-int64(footerLenSize)); err != nil {
return nil, 0, err
}
footerLen := binary.BigEndian.Uint32(footerLenBytes)
footerOffset := fileSize - int64(footerLenSize) - int64(footerLen)
if footerOffset < 0 {
return nil, 0, errors.New("invalid snapshot footer offset")
}
footerBytes := make([]byte, footerLen)
if _, err := f.ReadAt(footerBytes, footerOffset); err != nil {
return nil, 0, err
}
var footer SnapshotFileFooter
if err := json.Unmarshal(footerBytes, &footer); err != nil {
return nil, 0, err
}
return &footer, footerOffset, nil
}
func (engine *MatchingEngine) restoreMarket(f *os.File, segment MarketSegment, footerOffset int64) error {
if segment.Offset < 0 || segment.Length < 0 ||
segment.Offset > footerOffset || segment.Length > footerOffset-segment.Offset {
return errors.New("invalid snapshot segment bounds")
}
segmentData := make([]byte, segment.Length)
if _, err := f.ReadAt(segmentData, segment.Offset); err != nil {
return err
}
if crc32.ChecksumIEEE(segmentData) != segment.Checksum {
return errors.New("checksum mismatch for market " + segment.MarketID)
}
var snap OrderBookSnapshot
if err := json.Unmarshal(segmentData, &snap); err != nil {
return err
}
book := newOrderBook(engine.engineID, segment.MarketID, engine.publishTrader)
book.Restore(&snap)
engine.orderbooks[segment.MarketID] = book
return nil
}
func (engine *MatchingEngine) processCommand(ev *InputEvent) {
req := ev.Request
base, ok := protocol.GetRequestBase(req)
if !ok || base.CommandID == "" {
engine.rejectRequest(req, protocol.RejectReasonInvalidPayload)
engine.respondQueryError(ev, errors.New(string(protocol.RejectReasonInvalidPayload)))
return
}
switch request := req.(type) {
case *protocol.CreateMarketRequest:
engine.handleCreateMarketRequest(request, ev.Resp)
case *protocol.UserEventRequest:
engine.handleUserEvent(request)
case *protocol.PlaceOrderRequest,
*protocol.CancelOrderRequest,
*protocol.AmendOrderRequest,
*protocol.SuspendMarketRequest,
*protocol.ResumeMarketRequest,
*protocol.UpdateConfigRequest:
book := engine.orderbooks[base.MarketID]
if book == nil {
engine.rejectRequest(req, protocol.RejectReasonMarketNotFound)
engine.respondQueryError(ev, ErrNotFound)
return
}
book.processCommand(ev)
if base.SeqID > 0 {
book.lastCmdSeqID.Store(base.SeqID)
}
default:
engine.rejectRequest(req, protocol.RejectReasonUnknownCommand)
engine.respondQueryError(ev, ErrUnknownCommand)
}
}
func (engine *MatchingEngine) handleCreateMarketRequest(
req *protocol.CreateMarketRequest,
resp chan<- any,
) {
engine.handleCreateMarket(
req.CommandID,
req.Timestamp,
req.MarketID,
req.UserID,
req.MinLotSize,
resp,
)
}
func (engine *MatchingEngine) processQuery(ev *InputEvent) {
switch q := ev.Query.(type) {
case *protocol.GetDepthQuery:
book := engine.orderbooks[q.MarketID]
if book != nil {
book.processQuery(ev)
} else {
engine.respondQueryError(ev, ErrNotFound)
}
case *protocol.GetStatsQuery:
book := engine.orderbooks[q.MarketID]
if book != nil {
book.processQuery(ev)
} else {
engine.respondQueryError(ev, ErrNotFound)
}
case *snapshotQuery:
engine.handleSnapshotQuery(ev)
default:
// Unsupported query type
engine.respondQueryError(ev, ErrUnknownQuery)
}
}
// handleSnapshotQuery creates snapshots for all OrderBooks synchronously
// (on the same goroutine as the event loop, ensuring consistency).
func (engine *MatchingEngine) handleSnapshotQuery(ev *InputEvent) {
results := make([]snapshotResult, 0, len(engine.orderbooks))
for _, book := range engine.orderbooks {
snap := book.createSnapshot()
results = append(results, snapshotResult{snap: snap})
}
if ev.Resp != nil {
select {
case ev.Resp <- results:
default:
}
}
}
// handleCreateMarket handles the creation of a new market.
func (engine *MatchingEngine) handleCreateMarket(
cmdID string,
ts int64,
marketID string,
userID uint64,
minLotSize udecimal.Decimal,
resp chan<- any,
) {
if ts <= 0 {
engine.rejectLog(cmdID, marketID, userID, protocol.RejectReasonInvalidPayload, ts)
if resp != nil {
resp <- errors.New(string(protocol.RejectReasonInvalidPayload))
}
return
}
if _, exists := engine.orderbooks[marketID]; exists {
engine.rejectLog(cmdID, marketID, userID, protocol.RejectReasonMarketAlreadyExists, ts)
if resp != nil {
resp <- errors.New(string(protocol.RejectReasonMarketAlreadyExists))
}
return
}
// Create and Store (no goroutine, no individual RingBuffer)
opts := []OrderBookOption{}
if !minLotSize.IsZero() {
opts = append(opts, WithLotSize(minLotSize))
}
newbook := newOrderBook(engine.engineID, marketID, engine.publishTrader, opts...)
engine.orderbooks[marketID] = newbook
log := NewAdminLog(
newbook.seqID.Add(1),
cmdID,
engine.engineID,
marketID,
userID,
"market_created",
ts,
)
batch := acquireLogBatch()
batch.Logs = append(batch.Logs, log)
engine.publishTrader.Publish(batch.Logs)
releaseBookLog(log)
batch.Release()
if resp != nil {
select {
case resp <- true:
default:
}
}
}
// handleUserEvent processes a generic user event.
func (engine *MatchingEngine) handleUserEvent(req *protocol.UserEventRequest) {
if req.Timestamp <= 0 {
engine.rejectRequest(req, protocol.RejectReasonInvalidPayload)
return
}
// Create and Publish Log
log := NewUserEventLog(
req.SeqID,
req.CommandID,
engine.engineID,
req.UserID,
req.EventType,
req.Key,
req.Data,
req.Timestamp,
)
// Publish via the shared publishTrader
batch := acquireLogBatch()
batch.Logs = append(batch.Logs, log)
engine.publishTrader.Publish(batch.Logs)
releaseBookLog(log)
batch.Release()
}
func (engine *MatchingEngine) rejectLog(
cmdID, marketID string,
userID uint64,
reason protocol.RejectReason,
ts int64,
) {
log := NewRejectLog(
0,
cmdID,
engine.engineID,
marketID,
"", // OrderID and UserID are business data, not in envelope
userID,
reason,
ts,
)
batch := acquireLogBatch()
batch.Logs = append(batch.Logs, log)
engine.publishTrader.Publish(batch.Logs)
releaseBookLog(log)
batch.Release()
}
// respondQueryError returns a query-side error without waiting for timeout.
func (engine *MatchingEngine) respondQueryError(ev *InputEvent, err error) {
if ev.Resp == nil {
return
}
select {
case ev.Resp <- err:
default:
}
}
// rejectRequest emits a standardized reject log for engine-level command failures.
func (engine *MatchingEngine) rejectRequest(req any, reason protocol.RejectReason) {
base, ok := protocol.GetRequestBase(req)
if !ok {
return
}
log := NewRejectLog(
0,
base.CommandID,
engine.engineID,
base.MarketID,
engine.requestOrderID(req),
base.UserID,
reason,
base.Timestamp,
)
batch := acquireLogBatch()
batch.Logs = append(batch.Logs, log)
engine.publishTrader.Publish(batch.Logs)
releaseBookLog(log)
batch.Release()
}
// requestOrderID extracts the business identifier used for reject logs.
func (engine *MatchingEngine) requestOrderID(req any) string {
switch r := req.(type) {
case *protocol.PlaceOrderRequest:
return r.OrderID
case *protocol.CancelOrderRequest:
return r.OrderID
case *protocol.AmendOrderRequest:
return r.OrderID
case *protocol.UserEventRequest:
return r.Key
}
return "unknown"
}
func (engine *MatchingEngine) acquireResponseChannel() chan any {
val := engine.responsePool.Get()
ch, ok := val.(chan any)
if !ok {
// Should not happen with our pool setup, but satisfies linter
return make(chan any, 1)
}
return ch
}
func (engine *MatchingEngine) releaseResponseChannel(ch chan any) {
// Drain the channel if not empty
select {
case <-ch:
default:
}
engine.responsePool.Put(ch)
}
func (engine *MatchingEngine) onEvent(ev *InputEvent) {
if ev.Request != nil {
engine.processCommand(ev)
return
}
if ev.Query != nil {
engine.processQuery(ev)
return
}
}
func requireCommandID(commandID string) error {
if commandID == "" {
return ErrInvalidParam
}
return nil
}