mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
Merge pull request #1728 from c9s/kbearXD/dca2/fix-memory-leak
FIX: [core] fix memory leak
This commit is contained in:
commit
f2a443a499
|
@ -5,6 +5,8 @@ import "github.com/c9s/bbgo/pkg/types"
|
||||||
const MaxNumOfKLines = 5_000
|
const MaxNumOfKLines = 5_000
|
||||||
const MaxNumOfKLinesTruncate = 100
|
const MaxNumOfKLinesTruncate = 100
|
||||||
|
|
||||||
|
const CapacityOfKLineWindowLimit = 5_000
|
||||||
|
|
||||||
// MarketDataStore receives and maintain the public market data of a single symbol
|
// MarketDataStore receives and maintain the public market data of a single symbol
|
||||||
//go:generate callbackgen -type MarketDataStore
|
//go:generate callbackgen -type MarketDataStore
|
||||||
type MarketDataStore struct {
|
type MarketDataStore struct {
|
||||||
|
@ -57,10 +59,20 @@ func (store *MarketDataStore) AddKLine(k types.KLine) {
|
||||||
}
|
}
|
||||||
window.Add(k)
|
window.Add(k)
|
||||||
|
|
||||||
if len(*window) > MaxNumOfKLines {
|
truncateKLineWindowIfNeeded(window)
|
||||||
*window = (*window)[MaxNumOfKLinesTruncate-1:]
|
|
||||||
}
|
|
||||||
|
|
||||||
store.EmitKLineClosed(k)
|
store.EmitKLineClosed(k)
|
||||||
store.EmitKLineWindowUpdate(k.Interval, *window)
|
store.EmitKLineWindowUpdate(k.Interval, *window)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func truncateKLineWindowIfNeeded(window *types.KLineWindow) {
|
||||||
|
lenOfWindow := len(*window)
|
||||||
|
capOfWindow := cap(*window)
|
||||||
|
|
||||||
|
if lenOfWindow == capOfWindow && capOfWindow > CapacityOfKLineWindowLimit {
|
||||||
|
size := CapacityOfKLineWindowLimit / 2
|
||||||
|
start := lenOfWindow - size
|
||||||
|
copy(*window, (*window)[start:])
|
||||||
|
*window = (*window)[:size]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
45
pkg/bbgo/marketdatastore_test.go
Normal file
45
pkg/bbgo/marketdatastore_test.go
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
package bbgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMarketDataStore_AddKLineAndTruncateWindow(t *testing.T) {
|
||||||
|
store := NewMarketDataStore("BTCUSD")
|
||||||
|
|
||||||
|
interval := types.Interval1s
|
||||||
|
|
||||||
|
var maxCap int = 0
|
||||||
|
capFixed := false
|
||||||
|
|
||||||
|
var gid uint64 = 0
|
||||||
|
// insert 1.5 * CapacityOfKLineWindowLimit KLine into window
|
||||||
|
for ; gid < CapacityOfKLineWindowLimit+(CapacityOfKLineWindowLimit/2); gid++ {
|
||||||
|
store.AddKLine(types.KLine{
|
||||||
|
Interval: interval,
|
||||||
|
GID: gid,
|
||||||
|
})
|
||||||
|
|
||||||
|
// if the capacity is > CapacityOfKLineWindowLimit, the capacity should be fixed. We use this if expression to verify it then.
|
||||||
|
if !capFixed && cap(*store.KLineWindows[interval]) > CapacityOfKLineWindowLimit {
|
||||||
|
maxCap = cap(*store.KLineWindows[interval])
|
||||||
|
capFixed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window := store.KLineWindows[interval]
|
||||||
|
|
||||||
|
// make sure the capacity is fixed
|
||||||
|
assert.Equal(t, maxCap, cap(*window))
|
||||||
|
|
||||||
|
// after truncate, it will remain (CapacityOfKLineWindowLimit / 2) KLine in the window
|
||||||
|
// so the first GIC will be the maxCap - (CapacityOfKLineWindowLimit / 2)
|
||||||
|
truncatedGID := uint64(maxCap - (CapacityOfKLineWindowLimit / 2))
|
||||||
|
for _, kline := range *window {
|
||||||
|
assert.Equal(t, truncatedGID, kline.GID)
|
||||||
|
truncatedGID++
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,8 +2,9 @@ package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestKLineWindow_Tail(t *testing.T) {
|
func TestKLineWindow_Tail(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user