chore(storage): storage performance issues (#500)

* chore(storage): storage performance issues

1. close storage connection before shutdown the service
2. update windows image

https://github.com/appleboy/gorush/issues/393

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2020-04-23 15:39:24 +08:00
committed by GitHub
parent 4e05611577
commit c2136fffc7
16 changed files with 132 additions and 147 deletions

View File

@@ -25,10 +25,12 @@ type Storage struct {
config config.ConfYaml
opts badger.Options
name string
db *badger.DB
}
// Init client storage.
func (s *Storage) Init() error {
var err error
s.name = "badger"
dbPath := s.config.Stat.BadgerDB.Path
if dbPath == "" {
@@ -36,7 +38,18 @@ func (s *Storage) Init() error {
}
s.opts = badger.DefaultOptions(dbPath)
return nil
s.db, err = badger.Open(s.opts)
return err
}
// Close the storage connection
func (s *Storage) Close() error {
if s.db == nil {
return nil
}
return s.db.Close()
}
// Reset Client storage.
@@ -49,21 +62,7 @@ func (s *Storage) Reset() {
}
func (s *Storage) setBadger(key string, count int64) {
db, err := badger.Open(s.opts)
if err != nil {
log.Println(s.name, "open error:", err.Error())
return
}
defer func() {
err := db.Close()
if err != nil {
log.Println(s.name, "close error:", err.Error())
}
}()
err = db.Update(func(txn *badger.Txn) error {
err := s.db.Update(func(txn *badger.Txn) error {
value := convert.ToString(count).(string)
return txn.Set([]byte(key), []byte(value))
})
@@ -74,21 +73,7 @@ func (s *Storage) setBadger(key string, count int64) {
}
func (s *Storage) getBadger(key string, count *int64) {
db, err := badger.Open(s.opts)
if err != nil {
log.Println(s.name, "open error:", err.Error())
return
}
defer func() {
err := db.Close()
if err != nil {
log.Println(s.name, "close error:", err.Error())
}
}()
err = db.View(func(txn *badger.Txn) error {
err := s.db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(key))
if err != nil {
return err

View File

@@ -44,4 +44,6 @@ func TestBadgerEngine(t *testing.T) {
badger.Reset()
val = badger.GetAndroidError()
assert.Equal(t, int64(0), val)
assert.NoError(t, badger.Close())
}