mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
call Reset instead of replacing the whole map
the reason is that we have the update worker, which is already started.
This commit is contained in:
parent
2b441ad3bc
commit
50fc1fd3ac
|
@ -11,7 +11,8 @@ import (
|
|||
|
||||
//go:generate callbackgen -type DepthFrame
|
||||
type DepthFrame struct {
|
||||
client *binance.Client
|
||||
client *binance.Client
|
||||
context context.Context
|
||||
|
||||
mu sync.Mutex
|
||||
once sync.Once
|
||||
|
@ -26,12 +27,12 @@ type DepthFrame struct {
|
|||
func (f *DepthFrame) Reset() {
|
||||
f.mu.Lock()
|
||||
f.SnapshotDepth = nil
|
||||
f.once = sync.Once{}
|
||||
f.BufEvents = nil
|
||||
f.mu.Unlock()
|
||||
}
|
||||
|
||||
func (f *DepthFrame) loadDepthSnapshot() {
|
||||
depth, err := f.fetch(context.Background())
|
||||
depth, err := f.fetch(f.context)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -75,6 +76,7 @@ func (f *DepthFrame) PushEvent(e DepthEvent) {
|
|||
|
||||
// before the snapshot is loaded, we need to buffer the events until we loaded the snapshot.
|
||||
if f.SnapshotDepth == nil {
|
||||
// buffer the events until we loaded the snapshot
|
||||
f.BufEvents = append(f.BufEvents, e)
|
||||
f.mu.Unlock()
|
||||
|
||||
|
@ -84,16 +86,15 @@ func (f *DepthFrame) PushEvent(e DepthEvent) {
|
|||
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():
|
||||
case <-f.context.Done():
|
||||
return
|
||||
|
||||
case <-ticker.C:
|
||||
f.loadDepthSnapshot()
|
||||
}
|
||||
|
@ -110,6 +111,10 @@ func (f *DepthFrame) PushEvent(e DepthEvent) {
|
|||
|
||||
// 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 {
|
||||
if debugBinanceDepth {
|
||||
log.Warnf("event first update id %d > final update id + 1 (%d), resetting snapshot", e.FirstUpdateID, f.SnapshotDepth.FirstUpdateID+1)
|
||||
}
|
||||
|
||||
f.SnapshotDepth = nil
|
||||
f.mu.Unlock()
|
||||
return
|
||||
|
|
|
@ -74,24 +74,28 @@ func NewStream(client *binance.Client) *Stream {
|
|||
depthFrames: make(map[string]*DepthFrame),
|
||||
}
|
||||
|
||||
stream.OnConnect(func() {
|
||||
// clear the previous frames
|
||||
stream.depthFrames = make(map[string]*DepthFrame)
|
||||
})
|
||||
|
||||
stream.OnDepthEvent(func(e *DepthEvent) {
|
||||
f, ok := stream.depthFrames[e.Symbol]
|
||||
if !ok {
|
||||
f = &DepthFrame{
|
||||
client: client,
|
||||
context: context.Background(),
|
||||
Symbol: e.Symbol,
|
||||
}
|
||||
|
||||
stream.depthFrames[e.Symbol] = f
|
||||
|
||||
f.OnReady(func(e DepthEvent, bufEvents []DepthEvent) {
|
||||
snapshot, err := e.OrderBook()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("book convert error")
|
||||
return
|
||||
}
|
||||
|
||||
if !snapshot.IsValid() {
|
||||
log.Warnf("depth snapshot is invalid, event: %+v", e)
|
||||
}
|
||||
|
||||
stream.EmitBookSnapshot(snapshot)
|
||||
|
||||
for _, e := range bufEvents {
|
||||
|
@ -114,7 +118,6 @@ func NewStream(client *binance.Client) *Stream {
|
|||
|
||||
stream.EmitBookUpdate(book)
|
||||
})
|
||||
stream.depthFrames[e.Symbol] = f
|
||||
} else {
|
||||
f.PushEvent(*e)
|
||||
}
|
||||
|
@ -168,6 +171,11 @@ func NewStream(client *binance.Client) *Stream {
|
|||
})
|
||||
|
||||
stream.OnConnect(func() {
|
||||
// reset the previous frames
|
||||
for _, f := range stream.depthFrames {
|
||||
f.Reset()
|
||||
}
|
||||
|
||||
var params []string
|
||||
for _, subscription := range stream.Subscriptions {
|
||||
params = append(params, convertSubscription(subscription))
|
||||
|
@ -452,4 +460,3 @@ func maskListenKey(listenKey string) string {
|
|||
}
|
||||
|
||||
//go:generate callbackgen -type DepthFrame
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user