-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
ordermap.go
245 lines (198 loc) · 4.47 KB
/
ordermap.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
package types
import (
"sync"
"time"
)
// OrderMap is used for storing orders by their order id
type OrderMap map[uint64]Order
func (m OrderMap) Backup() (orderForms []SubmitOrder) {
for _, order := range m {
orderForms = append(orderForms, order.Backup())
}
return orderForms
}
// Add the order the the map
func (m OrderMap) Add(o Order) {
m[o.OrderID] = o
}
// Update only updates the order when the order ID exists in the map
func (m OrderMap) Update(o Order) {
if _, ok := m[o.OrderID]; ok {
m[o.OrderID] = o
}
}
func (m OrderMap) Lookup(f func(o Order) bool) *Order {
for _, order := range m {
if f(order) {
// copy and return
o := order
return &o
}
}
return nil
}
func (m OrderMap) Remove(orderID uint64) {
delete(m, orderID)
}
func (m OrderMap) IDs() (ids []uint64) {
for id := range m {
ids = append(ids, id)
}
return ids
}
func (m OrderMap) Exists(orderID uint64) bool {
_, ok := m[orderID]
return ok
}
func (m OrderMap) Get(orderID uint64) (Order, bool) {
order, ok := m[orderID]
return order, ok
}
func (m OrderMap) FindByStatus(status OrderStatus) (orders OrderSlice) {
for _, o := range m {
if o.Status == status {
orders = append(orders, o)
}
}
return orders
}
func (m OrderMap) Filled() OrderSlice {
return m.FindByStatus(OrderStatusFilled)
}
func (m OrderMap) Canceled() OrderSlice {
return m.FindByStatus(OrderStatusCanceled)
}
func (m OrderMap) Orders() (orders OrderSlice) {
for _, o := range m {
orders = append(orders, o)
}
return orders
}
type SyncOrderMap struct {
orders OrderMap
// pendingRemoval is for recording the order remove message for unknown orders.
// the order removal message might arrive before the order update, so if we found there is a pending removal,
// we should not keep the order in the order map
pendingRemoval map[uint64]time.Time
sync.RWMutex
}
func NewSyncOrderMap() *SyncOrderMap {
return &SyncOrderMap{
orders: make(OrderMap),
pendingRemoval: make(map[uint64]time.Time, 10),
}
}
func (m *SyncOrderMap) Backup() (orders []SubmitOrder) {
m.Lock()
orders = m.orders.Backup()
m.Unlock()
return orders
}
func (m *SyncOrderMap) Remove(orderID uint64) (exists bool) {
m.Lock()
defer m.Unlock()
exists = m.orders.Exists(orderID)
if exists {
m.orders.Remove(orderID)
} else {
m.pendingRemoval[orderID] = time.Now()
}
return exists
}
func (m *SyncOrderMap) processPendingRemoval() {
m.Lock()
defer m.Unlock()
if len(m.pendingRemoval) == 0 {
return
}
expireTime := time.Now().Add(-5 * time.Minute)
removing := make(map[uint64]struct{})
for orderID, creationTime := range m.pendingRemoval {
if m.orders.Exists(orderID) || creationTime.Before(expireTime) {
m.orders.Remove(orderID)
removing[orderID] = struct{}{}
}
}
for orderID := range removing {
delete(m.pendingRemoval, orderID)
}
}
func (m *SyncOrderMap) Add(o Order) {
m.Lock()
m.orders.Add(o)
m.Unlock()
m.processPendingRemoval()
}
func (m *SyncOrderMap) Update(o Order) {
m.Lock()
m.orders.Update(o)
m.Unlock()
}
func (m *SyncOrderMap) Iterate(it func(id uint64, order Order) bool) {
m.Lock()
for id := range m.orders {
if it(id, m.orders[id]) {
break
}
}
m.Unlock()
}
func (m *SyncOrderMap) Exists(orderID uint64) (exists bool) {
m.Lock()
exists = m.orders.Exists(orderID)
m.Unlock()
return exists
}
func (m *SyncOrderMap) Get(orderID uint64) (Order, bool) {
m.Lock()
order, ok := m.orders.Get(orderID)
m.Unlock()
return order, ok
}
func (m *SyncOrderMap) Lookup(f func(o Order) bool) *Order {
m.Lock()
defer m.Unlock()
return m.orders.Lookup(f)
}
func (m *SyncOrderMap) Len() int {
m.Lock()
defer m.Unlock()
return len(m.orders)
}
func (m *SyncOrderMap) IDs() (ids []uint64) {
m.Lock()
ids = m.orders.IDs()
m.Unlock()
return ids
}
func (m *SyncOrderMap) FindByStatus(status OrderStatus) OrderSlice {
m.Lock()
defer m.Unlock()
return m.orders.FindByStatus(status)
}
func (m *SyncOrderMap) Filled() OrderSlice {
return m.FindByStatus(OrderStatusFilled)
}
// AnyFilled find any order is filled and stop iterating the order map
func (m *SyncOrderMap) AnyFilled() (order Order, ok bool) {
m.Lock()
defer m.Unlock()
for _, o := range m.orders {
if o.Status == OrderStatusFilled {
ok = true
order = o
return order, ok
}
}
return
}
func (m *SyncOrderMap) Canceled() OrderSlice {
return m.FindByStatus(OrderStatusCanceled)
}
func (m *SyncOrderMap) Orders() (slice OrderSlice) {
m.RLock()
slice = m.orders.Orders()
m.RUnlock()
return slice
}
type OrderSlice []Order