mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
support memory persistence
This commit is contained in:
parent
2d98336fb6
commit
4addf65f64
1
go.mod
1
go.mod
|
@ -41,6 +41,5 @@ require (
|
|||
gonum.org/v1/gonum v0.8.1
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||
gopkg.in/tucnak/telebot.v2 v2.3.5
|
||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c
|
||||
)
|
||||
|
|
|
@ -50,7 +50,6 @@ type Environment struct {
|
|||
startTime time.Time
|
||||
tradeScanTime time.Time
|
||||
sessions map[string]*ExchangeSession
|
||||
|
||||
}
|
||||
|
||||
func NewEnvironment() *Environment {
|
||||
|
@ -226,9 +225,12 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
|
|||
}
|
||||
|
||||
func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error {
|
||||
var facade = &PersistenceServiceFacade{}
|
||||
var facade = &PersistenceServiceFacade{
|
||||
Memory: NewMemoryService(),
|
||||
}
|
||||
|
||||
if conf.Redis != nil {
|
||||
if err := env.Set(conf.Redis) ; err != nil {
|
||||
if err := env.Set(conf.Redis); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,9 @@ func (p *Persistence) backendService(t string) (service PersistenceService, err
|
|||
case "redis":
|
||||
service = p.Facade.Redis
|
||||
|
||||
case "memory":
|
||||
service = p.Facade.Memory
|
||||
|
||||
default:
|
||||
err = fmt.Errorf("unsupported persistent type %s", t)
|
||||
}
|
||||
|
@ -63,4 +66,5 @@ func (p *Persistence) Save(val interface{}, subIDs ...string) error {
|
|||
type PersistenceServiceFacade struct {
|
||||
Redis *RedisPersistenceService
|
||||
Json *JsonPersistenceService
|
||||
Memory *MemoryService
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
|
@ -21,6 +22,42 @@ type Store interface {
|
|||
Save(val interface{}) error
|
||||
}
|
||||
|
||||
type MemoryService struct {
|
||||
Slots map[string]interface{}
|
||||
}
|
||||
|
||||
func NewMemoryService() *MemoryService {
|
||||
return &MemoryService{
|
||||
Slots: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryService) NewStore(id string, subIDs ...string) Store {
|
||||
key := strings.Join(append([]string{id}, subIDs...), ":")
|
||||
return &MemoryStore{
|
||||
Key: key,
|
||||
memory: s,
|
||||
}
|
||||
}
|
||||
|
||||
type MemoryStore struct {
|
||||
Key string
|
||||
memory *MemoryService
|
||||
}
|
||||
|
||||
func (store *MemoryStore) Save(val interface{}) error {
|
||||
store.memory.Slots[store.Key] = val
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) Load(val interface{}) error {
|
||||
v := reflect.ValueOf(val)
|
||||
if data, ok := store.memory.Slots[store.Key]; ok {
|
||||
v.Elem().Set(reflect.ValueOf(data).Elem())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type JsonPersistenceService struct {
|
||||
Directory string
|
||||
}
|
||||
|
|
|
@ -246,16 +246,26 @@ func (trader *Trader) Run(ctx context.Context) error {
|
|||
|
||||
if field, ok := hasField(rs, "Persistence"); ok {
|
||||
log.Infof("found Persistence field, injecting...")
|
||||
if field.IsNil() {
|
||||
field.Set(reflect.ValueOf(&Persistence{
|
||||
PersistenceSelector: &PersistenceSelector{
|
||||
StoreID: "default",
|
||||
Type: "memory",
|
||||
},
|
||||
Facade: trader.environment.PersistenceServiceFacade,
|
||||
}))
|
||||
} else {
|
||||
elem := field.Elem()
|
||||
if elem.Kind() != reflect.Struct {
|
||||
return fmt.Errorf("the field Persistence is not a struct element")
|
||||
}
|
||||
|
||||
elem := field.Elem()
|
||||
if elem.Kind() != reflect.Struct {
|
||||
return fmt.Errorf("the field Persistence is not a struct element")
|
||||
if err := injectField(elem, "Facade", trader.environment.PersistenceServiceFacade, true); err != nil {
|
||||
log.WithError(err).Errorf("strategy Persistence injection failed")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := injectField(elem, "Facade", trader.environment.PersistenceServiceFacade, true); err != nil {
|
||||
log.WithError(err).Errorf("strategy Persistence injection failed")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := injectField(rs, "Graceful", &trader.Graceful, true); err != nil {
|
||||
|
|
Loading…
Reference in New Issue
Block a user