mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 00:05:15 +00:00
fix: race condition in buffer
This commit is contained in:
parent
dca80cfb7e
commit
0c7a98cc4b
|
@ -3,6 +3,7 @@ package depth
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -40,7 +41,7 @@ type Buffer struct {
|
||||||
updateTimeout time.Duration
|
updateTimeout time.Duration
|
||||||
|
|
||||||
// bufferingPeriod is used to buffer the update message before we get the full depth
|
// bufferingPeriod is used to buffer the update message before we get the full depth
|
||||||
bufferingPeriod time.Duration
|
bufferingPeriod atomic.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBuffer(fetcher SnapshotFetcher) *Buffer {
|
func NewBuffer(fetcher SnapshotFetcher) *Buffer {
|
||||||
|
@ -55,7 +56,7 @@ func (b *Buffer) SetUpdateTimeout(d time.Duration) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffer) SetBufferingPeriod(d time.Duration) {
|
func (b *Buffer) SetBufferingPeriod(d time.Duration) {
|
||||||
b.bufferingPeriod = d
|
b.bufferingPeriod.Store(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffer) resetSnapshot() {
|
func (b *Buffer) resetSnapshot() {
|
||||||
|
@ -185,8 +186,8 @@ func (b *Buffer) fetchAndPush() error {
|
||||||
|
|
||||||
func (b *Buffer) tryFetch() {
|
func (b *Buffer) tryFetch() {
|
||||||
for {
|
for {
|
||||||
if b.bufferingPeriod > 0 {
|
if period := b.bufferingPeriod.Load(); period != nil {
|
||||||
<-time.After(b.bufferingPeriod)
|
<-time.After(period.(time.Duration))
|
||||||
}
|
}
|
||||||
|
|
||||||
err := b.fetchAndPush()
|
err := b.fetchAndPush()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user