-
Notifications
You must be signed in to change notification settings - Fork 5
/
match.go
85 lines (71 loc) · 1.39 KB
/
match.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
package match
import (
"sync"
)
//写入撮合引擎的数据
type write chan<- *Order
//撮合引擎完成后的数据
type read <-chan [2]*Order
type matcher struct {
Write write
waitCh chan *Order
Read read
finishCh chan [2]*Order
}
//取消指定的撮合订单
//不一定成功 返回nil表示失败
func (m *matcher) Cancel(order *Order) *Order {
readCh := dispatch.cancel(order)
return <-readCh
}
//增量或者创建
func (m *matcher) AddOrCreate(order *Order) {
ch := dispatch.addOrCreate(order)
select {
case <-ch:
return
}
}
func (m *matcher) GetDepth(symbol Symbol, depth int) [2][]Order {
var res [2][]Order
res[0] = book.getDepth(symbol, BUY, depth)
res[1] = book.getDepth(symbol, SELL, depth)
return res
}
var match struct {
sync.Once
m *matcher
}
//实例化一个撮合结构体
func NewMatch() *matcher {
match.Do(func() {
waitCh := make(chan *Order, 1)
finishCh := make(chan [2]*Order, 10000)
match.m = &matcher{
Write: waitCh,
waitCh: waitCh,
Read: finishCh,
finishCh: finishCh,
}
go listen()
})
return match.m
}
//监听撮合数据事件
func listen() {
for {
select {
case order := <-match.m.waitCh:
switch order.Type {
case MARKET:
//放入市价队列
market.push(order)
case LIMIT:
//放入撮合账单队列
book.push(order)
}
//撮合调度
dispatch.dispatch(order.Symbol)
}
}
}