Documentation
¶
Overview ¶
Package engine provides the core matching engine and multi-market orchestration.
The MatchingEngine manages multiple markets, each with its own order book and matcher. It provides async APIs using the Future pattern for all operations, with context support for cancellation and timeouts.
Key features:
- Multi-market support with isolation
- Async API with Future pattern
- Market state management (running/suspended/halted)
- Snapshot and restore for disaster recovery
- Event-driven architecture with ring buffer
Example usage:
publisher := event.NewChannelPublisher(10000)
engine := engine.NewMatchingEngine(publisher)
engine.Start()
defer engine.Stop()
// Create market
ctx := context.Background()
req := &protocol.CreateMarketRequest{
BaseCommand: protocol.BaseCommand{
CommandID: "cmd-1",
UserID: 1000,
MarketID: "BTC-USDT",
Timestamp: time.Now().UnixNano(),
},
MinLotSize: decimal.NewFromFloat(0.001),
}
future, _ := engine.CreateMarket(ctx, req)
success, err := future.Wait(ctx)
Index ¶
- Variables
- type Config
- type Engine
- func (e *Engine) Events() <-chan *event.OrderBookLog
- func (e *Engine) GetLastCmdSeqID() uint64
- func (e *Engine) GetMatcher() *matcher.Matcher
- func (e *Engine) GetOrderBook() *book.OrderBook
- func (e *Engine) GetState() protocol.OrderBookState
- func (e *Engine) HaltMarket(reason string)
- func (e *Engine) IsRunning() bool
- func (e *Engine) ResumeMarket(reason string)
- func (e *Engine) SetLastCmdSeqID(seqID uint64)
- func (e *Engine) Start()
- func (e *Engine) Stop()
- func (e *Engine) SubmitOrder(o *order.Order) bool
- func (e *Engine) SuspendMarket(reason string)
- type Future
- type Market
- type MatchingEngine
- func (e *MatchingEngine) AmendOrderAsync(ctx context.Context, req *protocol.AmendOrderRequest) (*Future[*protocol.AmendOrderResult], error)
- func (e *MatchingEngine) CancelOrderAsync(ctx context.Context, req *protocol.CancelOrderRequest) (*Future[*protocol.CancelOrderResult], error)
- func (e *MatchingEngine) CreateMarket(ctx context.Context, req *protocol.CreateMarketRequest) (*Future[bool], error)
- func (e *MatchingEngine) GetMarket(marketID string) (*Market, error)
- func (e *MatchingEngine) GetStats(marketID string) (*protocol.MarketStats, error)
- func (e *MatchingEngine) PlaceOrderAsync(ctx context.Context, req *protocol.PlaceOrderRequest) (*Future[*protocol.PlaceOrderResult], error)
- func (e *MatchingEngine) RestoreFromFile(snapshotDir string) (*snapshot.SnapshotMetadata, error)
- func (e *MatchingEngine) RestoreFromSnapshot(snapshots []*OrderBookSnapshot) error
- func (e *MatchingEngine) ResumeMarket(ctx context.Context, req *protocol.ResumeMarketRequest) (*Future[bool], error)
- func (e *MatchingEngine) Shutdown()
- func (e *MatchingEngine) SuspendMarket(ctx context.Context, req *protocol.SuspendMarketRequest) (*Future[bool], error)
- func (e *MatchingEngine) TakeSnapshot() ([]*OrderBookSnapshot, uint64)
- func (e *MatchingEngine) TakeSnapshotToFile(targetDir string) (string, error)
- func (e *MatchingEngine) UpdateConfig(ctx context.Context, req *protocol.UpdateConfigRequest) (*Future[bool], error)
- type OrderBookSnapshot
Constants ¶
This section is empty.
Variables ¶
var DefaultLotSize = decimal.NewFromFloat(0.00000001) // 0.00000001
DefaultLotSize is the minimum trade unit (1e-8) This prevents infinite loops in market order quote mode
Functions ¶
This section is empty.
Types ¶
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
func (*Engine) Events ¶
func (e *Engine) Events() <-chan *event.OrderBookLog
Events returns the event log channel
func (*Engine) GetLastCmdSeqID ¶
GetLastCmdSeqID returns the last processed command SeqID Used for replay checkpoint after snapshot restore
func (*Engine) GetMatcher ¶
GetMatcher returns the matcher for snapshot purposes
func (*Engine) GetOrderBook ¶
func (*Engine) GetState ¶
func (e *Engine) GetState() protocol.OrderBookState
GetState returns the current market state
func (*Engine) HaltMarket ¶
HaltMarket halts the market (emergency stop, no operations)
func (*Engine) ResumeMarket ¶
ResumeMarket resumes the market to running state
func (*Engine) SetLastCmdSeqID ¶
SetLastCmdSeqID sets the last processed command SeqID Used when restoring from snapshot
func (*Engine) SuspendMarket ¶
SuspendMarket suspends the market (no new orders, can cancel existing)
type Future ¶
type Future[T any] struct { // contains filtered or unexported fields }
Future represents a placeholder for an asynchronous operation result
func (*Future[T]) Complete ¶
func (f *Future[T]) Complete(value T)
Complete completes the future with a value
type Market ¶
type Market struct {
ID string
OrderBook *book.OrderBook
Matcher *matcher.Matcher
State protocol.OrderBookState
MinLotSize decimal.Decimal
// contains filtered or unexported fields
}
Market represents a single trading market with its own order book
func NewMarket ¶
func NewMarket( id string, minLotSize decimal.Decimal, seqGen *event.SequenceGenerator, publisher event.PublishLog, ) *Market
NewMarket creates a new market
func (*Market) CanAmendOrder ¶
CanAmendOrder returns true if orders can be amended
func (*Market) CanCancelOrder ¶
CanCancelOrder returns true if orders can be cancelled
func (*Market) CanPlaceOrder ¶
CanPlaceOrder returns true if new orders can be placed
func (*Market) GetState ¶
func (m *Market) GetState() protocol.OrderBookState
GetState returns the current market state (thread-safe)
func (*Market) SetState ¶
func (m *Market) SetState(state protocol.OrderBookState)
SetState sets the market state (thread-safe)
func (*Market) UpdateConfig ¶
UpdateConfig updates market configuration
type MatchingEngine ¶
type MatchingEngine struct {
// contains filtered or unexported fields
}
MatchingEngine manages multiple markets and routes commands
func NewMatchingEngine ¶
func NewMatchingEngine(publisher event.PublishLog) *MatchingEngine
NewMatchingEngine creates a new matching engine
func (*MatchingEngine) AmendOrderAsync ¶
func (e *MatchingEngine) AmendOrderAsync(ctx context.Context, req *protocol.AmendOrderRequest) (*Future[*protocol.AmendOrderResult], error)
AmendOrderAsync amends an existing order asynchronously
func (*MatchingEngine) CancelOrderAsync ¶
func (e *MatchingEngine) CancelOrderAsync(ctx context.Context, req *protocol.CancelOrderRequest) (*Future[*protocol.CancelOrderResult], error)
CancelOrderAsync cancels an existing order asynchronously
func (*MatchingEngine) CreateMarket ¶
func (e *MatchingEngine) CreateMarket(ctx context.Context, req *protocol.CreateMarketRequest) (*Future[bool], error)
CreateMarket creates a new market
func (*MatchingEngine) GetMarket ¶
func (e *MatchingEngine) GetMarket(marketID string) (*Market, error)
GetMarket retrieves a market by ID
func (*MatchingEngine) GetStats ¶
func (e *MatchingEngine) GetStats(marketID string) (*protocol.MarketStats, error)
GetStats returns market statistics
func (*MatchingEngine) PlaceOrderAsync ¶
func (e *MatchingEngine) PlaceOrderAsync(ctx context.Context, req *protocol.PlaceOrderRequest) (*Future[*protocol.PlaceOrderResult], error)
PlaceOrderAsync places a new order asynchronously
func (*MatchingEngine) RestoreFromFile ¶
func (e *MatchingEngine) RestoreFromFile(snapshotDir string) (*snapshot.SnapshotMetadata, error)
RestoreFromFile reads a snapshot from disk and restores engine state
func (*MatchingEngine) RestoreFromSnapshot ¶
func (e *MatchingEngine) RestoreFromSnapshot(snapshots []*OrderBookSnapshot) error
RestoreFromSnapshot restores engine state from snapshots
func (*MatchingEngine) ResumeMarket ¶
func (e *MatchingEngine) ResumeMarket(ctx context.Context, req *protocol.ResumeMarketRequest) (*Future[bool], error)
ResumeMarket resumes trading on a market
func (*MatchingEngine) Shutdown ¶
func (e *MatchingEngine) Shutdown()
Shutdown gracefully shuts down the engine
func (*MatchingEngine) SuspendMarket ¶
func (e *MatchingEngine) SuspendMarket(ctx context.Context, req *protocol.SuspendMarketRequest) (*Future[bool], error)
SuspendMarket suspends trading on a market
func (*MatchingEngine) TakeSnapshot ¶
func (e *MatchingEngine) TakeSnapshot() ([]*OrderBookSnapshot, uint64)
TakeSnapshot creates a point-in-time snapshot of all markets
func (*MatchingEngine) TakeSnapshotToFile ¶
func (e *MatchingEngine) TakeSnapshotToFile(targetDir string) (string, error)
TakeSnapshotToFile takes a snapshot and saves it to disk atomically
func (*MatchingEngine) UpdateConfig ¶
func (e *MatchingEngine) UpdateConfig(ctx context.Context, req *protocol.UpdateConfigRequest) (*Future[bool], error)
UpdateConfig updates market configuration