support memory persistence

This commit is contained in:
c9s 2020-12-07 12:03:56 +08:00
parent 2d98336fb6
commit 4addf65f64
5 changed files with 63 additions and 11 deletions

1
go.mod
View File

@ -41,6 +41,5 @@ require (
gonum.org/v1/gonum v0.8.1 gonum.org/v1/gonum v0.8.1
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/tucnak/telebot.v2 v2.3.5 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 gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c
) )

View File

@ -50,7 +50,6 @@ type Environment struct {
startTime time.Time startTime time.Time
tradeScanTime time.Time tradeScanTime time.Time
sessions map[string]*ExchangeSession sessions map[string]*ExchangeSession
} }
func NewEnvironment() *Environment { func NewEnvironment() *Environment {
@ -226,7 +225,10 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
} }
func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error { func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error {
var facade = &PersistenceServiceFacade{} var facade = &PersistenceServiceFacade{
Memory: NewMemoryService(),
}
if conf.Redis != nil { if conf.Redis != nil {
if err := env.Set(conf.Redis); err != nil { if err := env.Set(conf.Redis); err != nil {
return err return err

View File

@ -25,6 +25,9 @@ func (p *Persistence) backendService(t string) (service PersistenceService, err
case "redis": case "redis":
service = p.Facade.Redis service = p.Facade.Redis
case "memory":
service = p.Facade.Memory
default: default:
err = fmt.Errorf("unsupported persistent type %s", t) err = fmt.Errorf("unsupported persistent type %s", t)
} }
@ -63,4 +66,5 @@ func (p *Persistence) Save(val interface{}, subIDs ...string) error {
type PersistenceServiceFacade struct { type PersistenceServiceFacade struct {
Redis *RedisPersistenceService Redis *RedisPersistenceService
Json *JsonPersistenceService Json *JsonPersistenceService
Memory *MemoryService
} }

View File

@ -7,6 +7,7 @@ import (
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"strings" "strings"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
@ -21,6 +22,42 @@ type Store interface {
Save(val interface{}) error 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 { type JsonPersistenceService struct {
Directory string Directory string
} }

View File

@ -246,7 +246,15 @@ func (trader *Trader) Run(ctx context.Context) error {
if field, ok := hasField(rs, "Persistence"); ok { if field, ok := hasField(rs, "Persistence"); ok {
log.Infof("found Persistence field, injecting...") 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() elem := field.Elem()
if elem.Kind() != reflect.Struct { if elem.Kind() != reflect.Struct {
return fmt.Errorf("the field Persistence is not a struct element") return fmt.Errorf("the field Persistence is not a struct element")
@ -258,6 +266,8 @@ func (trader *Trader) Run(ctx context.Context) error {
} }
} }
}
if err := injectField(rs, "Graceful", &trader.Graceful, true); err != nil { if err := injectField(rs, "Graceful", &trader.Graceful, true); err != nil {
log.WithError(err).Errorf("strategy Graceful injection failed") log.WithError(err).Errorf("strategy Graceful injection failed")
return err return err