implement pending removal order ids

This commit is contained in:
c9s 2021-05-10 13:06:23 +08:00
parent ddab6083d4
commit c90871fb39
3 changed files with 56 additions and 4 deletions

View File

@ -1,6 +1,11 @@
package types package types
import "sync" import (
"sync"
"time"
log "github.com/sirupsen/logrus"
)
// OrderMap is used for storing orders by their order id // OrderMap is used for storing orders by their order id
type OrderMap map[uint64]Order type OrderMap map[uint64]Order
@ -67,14 +72,16 @@ func (m OrderMap) Orders() (orders OrderSlice) {
} }
type SyncOrderMap struct { type SyncOrderMap struct {
orders OrderMap orders OrderMap
pendingRemoval map[uint64]time.Time
sync.RWMutex sync.RWMutex
} }
func NewSyncOrderMap() *SyncOrderMap { func NewSyncOrderMap() *SyncOrderMap {
return &SyncOrderMap{ return &SyncOrderMap{
orders: make(OrderMap), orders: make(OrderMap),
pendingRemoval: make(map[uint64]time.Time, 10),
} }
} }
@ -91,6 +98,8 @@ func (m *SyncOrderMap) Remove(orderID uint64) (exists bool) {
m.Lock() m.Lock()
m.orders.Remove(orderID) m.orders.Remove(orderID)
m.Unlock() m.Unlock()
} else {
m.pendingRemoval[orderID] = time.Now()
} }
return exists return exists
@ -98,7 +107,31 @@ func (m *SyncOrderMap) Remove(orderID uint64) (exists bool) {
func (m *SyncOrderMap) Add(o Order) { func (m *SyncOrderMap) Add(o Order) {
m.Lock() m.Lock()
m.orders.Add(o)
match := false
if len(m.pendingRemoval) > 0 {
expireTime := time.Now().Add(-10 * time.Second)
newPendingRemoval := make(map[uint64]time.Time, 10)
for orderID, creationTime := range m.pendingRemoval {
if o.OrderID == orderID {
log.Warnf("found pending removal orderID = %d, removing order %+v from the store", orderID, o)
match = true
continue
}
if creationTime.Before(expireTime) {
continue
}
newPendingRemoval[orderID] = creationTime
}
m.pendingRemoval = newPendingRemoval
}
if !match {
m.orders.Add(o)
}
m.Unlock() m.Unlock()
} }

View File

@ -86,6 +86,20 @@ case "$command" in
jq -r '.[] | "\(.id) \(.market) \(.side) \(.ord_type) \(if .ord_type | test("stop") then "stop@" + .stop_price else "" end) price = \(if .ord_type | test("market") then "any" else .price end) \t volume = \(.volume) \(.state)"' jq -r '.[] | "\(.id) \(.market) \(.side) \(.ord_type) \(if .ord_type | test("stop") then "stop@" + .stop_price else "" end) price = \(if .ord_type | test("market") then "any" else .price end) \t volume = \(.volume) \(.state)"'
;; ;;
order)
if [[ $# < 1 ]] ; then
echo "$0 order [id]"
exit
fi
id=$1
declare -A orders_params=()
orders_params[id]=$id
myOrder orders_params | \
jq -r '.'
;;
cancel) cancel)
if [[ $# < 1 ]] ; then if [[ $# < 1 ]] ; then
echo "$0 cancel [oid]" echo "$0 cancel [oid]"

View File

@ -120,6 +120,11 @@ function myOrders()
send_auth_request "GET" "/api/v2/orders" _params send_auth_request "GET" "/api/v2/orders" _params
} }
function myOrder()
{
local -n _params=$1
send_auth_request "GET" "/api/v2/order" _params
}
function myTrades() function myTrades()
{ {