-
-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
binance: improve depth event filtering and reloading
- v1.61.0
- v1.60.3
- v1.60.2
- v1.60.1
- v1.60.0
- v1.59.2
- v1.59.1
- v1.59.0
- v1.58.0
- v1.57.0
- v1.56.2
- v1.56.1
- v1.56.0
- v1.55.4
- v1.55.3
- v1.55.2
- v1.55.1
- v1.55.0
- v1.54.0
- v1.53.0
- v1.52.0
- v1.51.1
- v1.51.0
- v1.50.3
- v1.50.2
- v1.50.1
- v1.50.0
- v1.49.0
- v1.48.4
- v1.48.3
- v1.48.2
- v1.48.1
- v1.48.0
- v1.47.0
- v1.46.0
- v1.45.0
- v1.44.1
- v1.44.0
- v1.43.1
- v1.43.0
- v1.42.0
- v1.41.0
- v1.40.4
- v1.40.3
- v1.40.2
- v1.40.1
- v1.40.0
- v1.39.2
- v1.39.1
- v1.39.0
- v1.38.0
- v1.37.0
- v1.36.0
- v1.35.0
- v1.34.0
- v1.33.4
- v1.33.3
- v1.33.2
- v1.33.1
- v1.33.0
- v1.32.0
- v1.31.4
- v1.31.3
- v1.31.2
- v1.31.1
- v1.31.0
- v1.30.3
- v1.30.2
- v1.30.1
- v1.30.0
- v1.29.0
- v1.28.0
- v1.27.0
- v1.26.3
- v1.26.2
- v1.26.1
- v1.26.0
- v1.25.5
- v1.25.4
- v1.25.3
- v1.25.2
- v1.25.1
- v1.25.0
- v1.24.0
- v1.23.0
- v1.22.3
- v1.22.2
- v1.22.1
- v1.22.0
- v1.21.4
- v1.21.3
- v1.21.2
- v1.21.1
- v1.21.0
- v1.20.0
- v1.19.4
- v1.19.3
- v1.19.2
- v1.19.1
- v1.19.0
- v1.18.5
- v1.18.4
- v1.18.3
- v1.18.2
- v1.18.1
- v1.18.0
- v1.17.1
- v1.17.0
- v1.16.0
- v1.15.5
- v1.15.4
- v1.15.3
- v1.15.2
- v1.15.1
- v1.15.0
- v1.14.1
- v1.14.0
- v1.13.0
- v1.12.0
- v1.11.1
- v1.11.0
- v1.10.0
Showing
2 changed files
with
162 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package binance | ||
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"sync" | ||
"time" | ||
|
||
"github.com/adshao/go-binance/v2" | ||
) | ||
|
||
//go:generate callbackgen -type DepthFrame | ||
type DepthFrame struct { | ||
client *binance.Client | ||
|
||
mu sync.Mutex | ||
once sync.Once | ||
SnapshotDepth *DepthEvent | ||
Symbol string | ||
BufEvents []DepthEvent | ||
|
||
readyCallbacks []func(snapshotDepth DepthEvent, bufEvents []DepthEvent) | ||
pushCallbacks []func(e DepthEvent) | ||
} | ||
|
||
func (f *DepthFrame) Reset() { | ||
f.mu.Lock() | ||
f.SnapshotDepth = nil | ||
f.once = sync.Once{} | ||
f.mu.Unlock() | ||
} | ||
|
||
func (f *DepthFrame) loadDepthSnapshot() { | ||
depth, err := f.fetch(context.Background()) | ||
if err != nil { | ||
return | ||
} | ||
|
||
f.mu.Lock() | ||
|
||
// filter the events by the event IDs | ||
var events []DepthEvent | ||
for _, e := range f.BufEvents { | ||
if e.FirstUpdateID <= depth.FinalUpdateID || e.FinalUpdateID <= depth.FinalUpdateID { | ||
continue | ||
} | ||
|
||
events = append(events, e) | ||
} | ||
|
||
// since we're buffering the update events, ideally the some of the head events | ||
// should be older than the received depth snapshot. | ||
// if the head event is newer than the depth we got, | ||
// then there are something missed, we need to restart the process. | ||
if len(events) > 0 { | ||
e := events[0] | ||
if e.FirstUpdateID > depth.FinalUpdateID+1 { | ||
log.Warn("miss matched final update id for order book") | ||
f.SnapshotDepth = nil | ||
f.BufEvents = nil | ||
f.mu.Unlock() | ||
return | ||
} | ||
} | ||
|
||
f.SnapshotDepth = depth | ||
f.BufEvents = nil | ||
f.mu.Unlock() | ||
|
||
f.EmitReady(*depth, events) | ||
} | ||
|
||
func (f *DepthFrame) PushEvent(e DepthEvent) { | ||
f.mu.Lock() | ||
|
||
// before the snapshot is loaded, we need to buffer the events until we loaded the snapshot. | ||
if f.SnapshotDepth == nil { | ||
f.BufEvents = append(f.BufEvents, e) | ||
f.mu.Unlock() | ||
|
||
// start a worker to update the snapshot periodically. | ||
go f.once.Do(func() { | ||
if debugBinanceDepth { | ||
log.Infof("starting depth snapshot updater for %s market", f.Symbol) | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
f.loadDepthSnapshot() | ||
|
||
ticker := time.NewTicker(1*time.Minute + time.Duration(rand.Intn(10))*time.Millisecond) | ||
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
f.loadDepthSnapshot() | ||
} | ||
} | ||
}) | ||
} else { | ||
// if we have the snapshot, we could use that final update ID filter the events | ||
|
||
// drop any update ID < the final update ID | ||
if e.FinalUpdateID < f.SnapshotDepth.FinalUpdateID { | ||
f.mu.Unlock() | ||
return | ||
} | ||
|
||
// if the first update ID > final update ID + 1, it means something is missing, we need to reload. | ||
if e.FirstUpdateID > f.SnapshotDepth.FinalUpdateID+1 { | ||
f.SnapshotDepth = nil | ||
f.mu.Unlock() | ||
return | ||
} | ||
|
||
// update the final update ID, so that we can check the next event | ||
f.SnapshotDepth.FinalUpdateID = e.FinalUpdateID | ||
f.mu.Unlock() | ||
|
||
f.EmitPush(e) | ||
} | ||
} | ||
|
||
// fetch fetches the depth and convert to the depth event so that we can reuse the event structure to convert it to the global orderbook type | ||
func (f *DepthFrame) fetch(ctx context.Context) (*DepthEvent, error) { | ||
if debugBinanceDepth { | ||
log.Infof("fetching %s depth snapshot", f.Symbol) | ||
} | ||
|
||
response, err := f.client.NewDepthService().Symbol(f.Symbol).Do(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
event := DepthEvent{ | ||
FirstUpdateID: 0, | ||
FinalUpdateID: response.LastUpdateID, | ||
} | ||
|
||
for _, entry := range response.Bids { | ||
event.Bids = append(event.Bids, DepthEntry{PriceLevel: entry.Price, Quantity: entry.Quantity}) | ||
} | ||
|
||
for _, entry := range response.Asks { | ||
event.Asks = append(event.Asks, DepthEntry{PriceLevel: entry.Price, Quantity: entry.Quantity}) | ||
} | ||
|
||
return &event, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters