matching

package
v0.0.0-...-55a620b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2020 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LogTypeMatch = LogType("match")
	LogTypeOpen  = LogType("open")
	LogTypeDone  = LogType("done")
)
View Source
const (
	TopicOrderPrefix = "matching_order_"
)

Variables

This section is empty.

Functions

func Get

func Get(m []byte, i int64) bool

func GetBit

func GetBit(b byte, i int64) bool

func Len

func Len(m []byte) int

func NewOrderBook

func NewOrderBook(product *models.Product) *orderBook

func NewSlice

func NewSlice(l int64) []byte

func Set

func Set(m []byte, i int64, v bool)

func SetBit

func SetBit(b byte, i int64, v bool) byte

func SetBitRef

func SetBitRef(b *byte, i int64, v bool)

func StartEngine

func StartEngine()

Types

type Base

type Base struct {
	Type      LogType
	Sequence  int64
	ProductId string
	Time      time.Time
}

type Bitmap

type Bitmap []byte

func New

func New(l int64) Bitmap

func (Bitmap) Data

func (b Bitmap) Data(copy bool) []byte

func (Bitmap) Get

func (b Bitmap) Get(i int64) bool

func (Bitmap) Len

func (b Bitmap) Len() int

func (Bitmap) Set

func (b Bitmap) Set(i int64, v bool)

type BookOrder

type BookOrder struct {
	OrderId int64
	Size    decimal.Decimal
	Funds   decimal.Decimal
	Price   decimal.Decimal
	Side    models.Side
	Type    models.OrderType
}

type DoneLog

type DoneLog struct {
	Base
	OrderId       int64
	Price         decimal.Decimal
	RemainingSize decimal.Decimal
	Reason        models.DoneReason
	Side          models.Side
}

func (*DoneLog) GetSeq

func (l *DoneLog) GetSeq() int64

type Engine

type Engine struct {

	// engine持有的orderBook,和product对应,需要快照,并从快照中恢复
	OrderBook *orderBook
	// contains filtered or unexported fields
}

func NewEngine

func NewEngine(product *models.Product, orderReader OrderReader, logStore LogStore, snapshotStore SnapshotStore) *Engine

func (*Engine) Start

func (e *Engine) Start()

type KafkaLogReader

type KafkaLogReader struct {
	// contains filtered or unexported fields
}

func (*KafkaLogReader) GetProductId

func (r *KafkaLogReader) GetProductId() string

func (*KafkaLogReader) RegisterObserver

func (r *KafkaLogReader) RegisterObserver(observer LogObserver)

func (*KafkaLogReader) Run

func (r *KafkaLogReader) Run(seq, offset int64)

type KafkaLogStore

type KafkaLogStore struct {
	// contains filtered or unexported fields
}

func NewKafkaLogStore

func NewKafkaLogStore(productId string, brokers []string) *KafkaLogStore

func (*KafkaLogStore) Store

func (s *KafkaLogStore) Store(logs []interface{}) error

type KafkaOrderReader

type KafkaOrderReader struct {
	// contains filtered or unexported fields
}

func NewKafkaOrderReader

func NewKafkaOrderReader(productId string, brokers []string) *KafkaOrderReader

func (*KafkaOrderReader) FetchOrder

func (s *KafkaOrderReader) FetchOrder() (offset int64, order *models.Order, err error)

func (*KafkaOrderReader) SetOffset

func (s *KafkaOrderReader) SetOffset(offset int64) error

type Log

type Log interface {
	GetSeq() int64
}

type LogObserver

type LogObserver interface {
	// 当读到OpenLog时回调
	OnOpenLog(log *OpenLog, offset int64)
	// 当读到MatchLog时回调
	OnMatchLog(log *MatchLog, offset int64)
	// 当读到DoneLog是回调
	OnDoneLog(log *DoneLog, offset int64)
}

撮合日志reader观察者

type LogReader

type LogReader interface {
	// 获取当前的productId
	GetProductId() string
	// 注册一个日志观察者
	RegisterObserver(observer LogObserver)
	// 开始执行读取log,读取到的log将会回调给观察者
	Run(seq, offset int64)
}

以观察者模式读取撮合日志

func NewKafkaLogReader

func NewKafkaLogReader(readerId, productId string, brokers []string) LogReader

type LogStore

type LogStore interface {
	// 保存日志
	Store(logs []interface{}) error
}

用于保存撮合日志

type LogType

type LogType string

type MatchLog

type MatchLog struct {
	Base
	TradeId      int64
	TakerOrderId int64
	MakerOrderId int64
	Side         models.Side
	Price        decimal.Decimal
	Size         decimal.Decimal
}

func (*MatchLog) GetSeq

func (l *MatchLog) GetSeq() int64

type OpenLog

type OpenLog struct {
	Base
	OrderId       int64
	RemainingSize decimal.Decimal
	Price         decimal.Decimal
	Side          models.Side
}

func (*OpenLog) GetSeq

func (l *OpenLog) GetSeq() int64

type OrderReader

type OrderReader interface {
	// 设置读取的起始offset
	SetOffset(offset int64) error
	// 拉取order
	FetchOrder() (offset int64, order *models.Order, err error)
}

用于撮合引擎读取order,需要支持设置offset,从指定的offset开始读取

type ReceivedLog

type ReceivedLog struct {
	Base
	OrderId   int64
	Size      decimal.Decimal
	Price     decimal.Decimal
	Side      models.Side
	OrderType models.OrderType
}

func (*ReceivedLog) GetSeq

func (l *ReceivedLog) GetSeq() int64

type RedisSnapshotStore

type RedisSnapshotStore struct {
	// contains filtered or unexported fields
}

func (*RedisSnapshotStore) GetLatest

func (s *RedisSnapshotStore) GetLatest() (*Snapshot, error)

func (*RedisSnapshotStore) Store

func (s *RedisSnapshotStore) Store(snapshot *Snapshot) error

type Snapshot

type Snapshot struct {
	OrderBookSnapshot orderBookSnapshot
	OrderOffset       int64
}

快照是engine在某一时候的一致性内存状态

type SnapshotStore

type SnapshotStore interface {
	// 保存快照
	Store(snapshot *Snapshot) error
	// 获取最后一次快照
	GetLatest() (*Snapshot, error)
}

用于保存撮合引擎的快照

func NewRedisSnapshotStore

func NewRedisSnapshotStore(productId string) SnapshotStore

type Window

type Window struct {
	Min    int64
	Max    int64
	Cap    int64
	Bitmap Bitmap
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL