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

@@ -2,7 +2,6 @@ package leveldb
import (
"fmt"
"log"
"strconv"
"github.com/appleboy/gorush/config"
@@ -11,34 +10,14 @@ import (
"github.com/syndtr/goleveldb/leveldb"
)
var dbPath string
func setLevelDB(key string, count int64) {
db, _ := leveldb.OpenFile(dbPath, nil)
func (s *Storage) setLevelDB(key string, count int64) {
value := fmt.Sprintf("%d", count)
_ = db.Put([]byte(key), []byte(value), nil)
defer func() {
err := db.Close()
if err != nil {
log.Println("LevelDB error:", err.Error())
}
}()
_ = s.db.Put([]byte(key), []byte(value), nil)
}
func getLevelDB(key string, count *int64) {
db, _ := leveldb.OpenFile(dbPath, nil)
data, _ := db.Get([]byte(key), nil)
func (s *Storage) getLevelDB(key string, count *int64) {
data, _ := s.db.Get([]byte(key), nil)
*count, _ = strconv.ParseInt(string(data), 10, 64)
defer func() {
err := db.Close()
if err != nil {
log.Println("LevelDB error:", err.Error())
}
}()
}
// New func implements the storage interface for gorush (https://github.com/appleboy/gorush)
@@ -51,57 +30,68 @@ func New(config config.ConfYaml) *Storage {
// Storage is interface structure
type Storage struct {
config config.ConfYaml
db *leveldb.DB
}
// Init client storage.
func (s *Storage) Init() error {
dbPath = s.config.Stat.LevelDB.Path
return nil
var err error
s.db, err = leveldb.OpenFile(s.config.Stat.LevelDB.Path, nil)
return err
}
// Close the storage connection
func (s *Storage) Close() error {
if s.db == nil {
return nil
}
return s.db.Close()
}
// Reset Client storage.
func (s *Storage) Reset() {
setLevelDB(storage.TotalCountKey, 0)
setLevelDB(storage.IosSuccessKey, 0)
setLevelDB(storage.IosErrorKey, 0)
setLevelDB(storage.AndroidSuccessKey, 0)
setLevelDB(storage.AndroidErrorKey, 0)
s.setLevelDB(storage.TotalCountKey, 0)
s.setLevelDB(storage.IosSuccessKey, 0)
s.setLevelDB(storage.IosErrorKey, 0)
s.setLevelDB(storage.AndroidSuccessKey, 0)
s.setLevelDB(storage.AndroidErrorKey, 0)
}
// AddTotalCount record push notification count.
func (s *Storage) AddTotalCount(count int64) {
total := s.GetTotalCount() + count
setLevelDB(storage.TotalCountKey, total)
s.setLevelDB(storage.TotalCountKey, total)
}
// AddIosSuccess record counts of success iOS push notification.
func (s *Storage) AddIosSuccess(count int64) {
total := s.GetIosSuccess() + count
setLevelDB(storage.IosSuccessKey, total)
s.setLevelDB(storage.IosSuccessKey, total)
}
// AddIosError record counts of error iOS push notification.
func (s *Storage) AddIosError(count int64) {
total := s.GetIosError() + count
setLevelDB(storage.IosErrorKey, total)
s.setLevelDB(storage.IosErrorKey, total)
}
// AddAndroidSuccess record counts of success Android push notification.
func (s *Storage) AddAndroidSuccess(count int64) {
total := s.GetAndroidSuccess() + count
setLevelDB(storage.AndroidSuccessKey, total)
s.setLevelDB(storage.AndroidSuccessKey, total)
}
// AddAndroidError record counts of error Android push notification.
func (s *Storage) AddAndroidError(count int64) {
total := s.GetAndroidError() + count
setLevelDB(storage.AndroidErrorKey, total)
s.setLevelDB(storage.AndroidErrorKey, total)
}
// GetTotalCount show counts of all notification.
func (s *Storage) GetTotalCount() int64 {
var count int64
getLevelDB(storage.TotalCountKey, &count)
s.getLevelDB(storage.TotalCountKey, &count)
return count
}
@@ -109,7 +99,7 @@ func (s *Storage) GetTotalCount() int64 {
// GetIosSuccess show success counts of iOS notification.
func (s *Storage) GetIosSuccess() int64 {
var count int64
getLevelDB(storage.IosSuccessKey, &count)
s.getLevelDB(storage.IosSuccessKey, &count)
return count
}
@@ -117,7 +107,7 @@ func (s *Storage) GetIosSuccess() int64 {
// GetIosError show error counts of iOS notification.
func (s *Storage) GetIosError() int64 {
var count int64
getLevelDB(storage.IosErrorKey, &count)
s.getLevelDB(storage.IosErrorKey, &count)
return count
}
@@ -125,7 +115,7 @@ func (s *Storage) GetIosError() int64 {
// GetAndroidSuccess show success counts of Android notification.
func (s *Storage) GetAndroidSuccess() int64 {
var count int64
getLevelDB(storage.AndroidSuccessKey, &count)
s.getLevelDB(storage.AndroidSuccessKey, &count)
return count
}
@@ -133,7 +123,7 @@ func (s *Storage) GetAndroidSuccess() int64 {
// GetAndroidError show error counts of Android notification.
func (s *Storage) GetAndroidError() int64 {
var count int64
getLevelDB(storage.AndroidErrorKey, &count)
s.getLevelDB(storage.AndroidErrorKey, &count)
return count
}

View File

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