fix f.loadDepthSnapshot timing

This commit is contained in:
c9s 2021-01-24 14:12:44 +08:00
parent 50fc1fd3ac
commit cabc082713
2 changed files with 8 additions and 4 deletions

View File

@ -32,6 +32,10 @@ func (f *DepthFrame) Reset() {
}
func (f *DepthFrame) loadDepthSnapshot() {
if debugBinanceDepth {
log.Infof("loading %s depth from the restful api", f.Symbol)
}
depth, err := f.fetch(f.context)
if err != nil {
return
@ -80,14 +84,14 @@ func (f *DepthFrame) PushEvent(e DepthEvent) {
f.BufEvents = append(f.BufEvents, e)
f.mu.Unlock()
f.loadDepthSnapshot()
// 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)
}
f.loadDepthSnapshot()
ticker := time.NewTicker(1*time.Minute + time.Duration(rand.Intn(10))*time.Millisecond)
defer ticker.Stop()
for {

View File

@ -78,9 +78,9 @@ func NewStream(client *binance.Client) *Stream {
f, ok := stream.depthFrames[e.Symbol]
if !ok {
f = &DepthFrame{
client: client,
client: client,
context: context.Background(),
Symbol: e.Symbol,
Symbol: e.Symbol,
}
stream.depthFrames[e.Symbol] = f