mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-18 07:26:50 +00:00
exchanges/websocket: Allow configuration of orderbook publish period (#805)
* Allow configuration of orderbook publish period For some applications that import GCT it's more interesting to be immediately notified of an exchange orderbook update instead of only getting notified every 10 seconds. This option allows that to happen while keeping the previous default. * exchanges: allow configuration of orderbook update period
This commit is contained in:
@@ -31,6 +31,7 @@ func (w *Orderbook) Setup(obBufferLimit int,
|
||||
sortBufferByUpdateIDs,
|
||||
updateEntriesByID,
|
||||
verbose bool,
|
||||
publishPeriod time.Duration,
|
||||
exchangeName string,
|
||||
dataHandler chan interface{}) error {
|
||||
if exchangeName == "" {
|
||||
@@ -51,6 +52,7 @@ func (w *Orderbook) Setup(obBufferLimit int,
|
||||
w.dataHandler = dataHandler
|
||||
w.ob = make(map[currency.Code]map[currency.Code]map[asset.Item]*orderbookHolder)
|
||||
w.verbose = verbose
|
||||
w.publishPeriod = publishPeriod
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -127,6 +129,17 @@ func (w *Orderbook) Update(u *Update) error {
|
||||
}
|
||||
}
|
||||
|
||||
// a nil ticker means that a zero publish period has been requested,
|
||||
// this means publish now whatever was received with no throttling
|
||||
if book.ticker == nil {
|
||||
go func() {
|
||||
w.dataHandler <- book.ob.Retrieve()
|
||||
book.ob.Publish()
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
select {
|
||||
case <-book.ticker.C:
|
||||
// Opted to wait for receiver because we are limiting here and the sync
|
||||
@@ -143,6 +156,7 @@ func (w *Orderbook) Update(u *Update) error {
|
||||
book.ob.Publish()
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -252,7 +266,11 @@ func (w *Orderbook) LoadSnapshot(book *orderbook.Base) error {
|
||||
}
|
||||
depth.AssignOptions(book)
|
||||
buffer := make([]Update, w.obBufferLimit)
|
||||
ticker := time.NewTicker(timerDefault)
|
||||
|
||||
var ticker *time.Ticker
|
||||
if w.publishPeriod != 0 {
|
||||
ticker = time.NewTicker(w.publishPeriod)
|
||||
}
|
||||
holder = &orderbookHolder{
|
||||
ob: depth,
|
||||
buffer: &buffer,
|
||||
|
||||
Reference in New Issue
Block a user