Separate storage logic from actual business logic (#649)
This commit is contained in:
@@ -3,10 +3,9 @@ package leveldb
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/appleboy/gorush/storage"
|
||||
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
)
|
||||
|
||||
@@ -15,9 +14,10 @@ func (s *Storage) setLevelDB(key string, count int64) {
|
||||
_ = s.db.Put([]byte(key), []byte(value), nil)
|
||||
}
|
||||
|
||||
func (s *Storage) getLevelDB(key string, count *int64) {
|
||||
func (s *Storage) getLevelDB(key string) int64 {
|
||||
data, _ := s.db.Get([]byte(key), nil)
|
||||
*count, _ = strconv.ParseInt(string(data), 10, 64)
|
||||
count, _ := strconv.ParseInt(string(data), 10, 64)
|
||||
return count
|
||||
}
|
||||
|
||||
// New func implements the storage interface for gorush (https://github.com/appleboy/gorush)
|
||||
@@ -31,6 +31,25 @@ func New(config *config.ConfYaml) *Storage {
|
||||
type Storage struct {
|
||||
config *config.ConfYaml
|
||||
db *leveldb.DB
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *Storage) Add(key string, count int64) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
s.setLevelDB(key, s.getLevelDB(key)+count)
|
||||
}
|
||||
|
||||
func (s *Storage) Set(key string, count int64) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
s.setLevelDB(key, count)
|
||||
}
|
||||
|
||||
func (s *Storage) Get(key string) int64 {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
return s.getLevelDB(key)
|
||||
}
|
||||
|
||||
// Init client storage.
|
||||
@@ -48,112 +67,3 @@ func (s *Storage) Close() error {
|
||||
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
// Reset Client storage.
|
||||
func (s *Storage) Reset() {
|
||||
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)
|
||||
s.setLevelDB(storage.HuaweiSuccessKey, 0)
|
||||
s.setLevelDB(storage.HuaweiErrorKey, 0)
|
||||
}
|
||||
|
||||
// AddTotalCount record push notification count.
|
||||
func (s *Storage) AddTotalCount(count int64) {
|
||||
total := s.GetTotalCount() + count
|
||||
s.setLevelDB(storage.TotalCountKey, total)
|
||||
}
|
||||
|
||||
// AddIosSuccess record counts of success iOS push notification.
|
||||
func (s *Storage) AddIosSuccess(count int64) {
|
||||
total := s.GetIosSuccess() + count
|
||||
s.setLevelDB(storage.IosSuccessKey, total)
|
||||
}
|
||||
|
||||
// AddIosError record counts of error iOS push notification.
|
||||
func (s *Storage) AddIosError(count int64) {
|
||||
total := s.GetIosError() + count
|
||||
s.setLevelDB(storage.IosErrorKey, total)
|
||||
}
|
||||
|
||||
// AddAndroidSuccess record counts of success Android push notification.
|
||||
func (s *Storage) AddAndroidSuccess(count int64) {
|
||||
total := s.GetAndroidSuccess() + count
|
||||
s.setLevelDB(storage.AndroidSuccessKey, total)
|
||||
}
|
||||
|
||||
// AddAndroidError record counts of error Android push notification.
|
||||
func (s *Storage) AddAndroidError(count int64) {
|
||||
total := s.GetAndroidError() + count
|
||||
s.setLevelDB(storage.AndroidErrorKey, total)
|
||||
}
|
||||
|
||||
// AddHuaweiSuccess record counts of success Huawei push notification.
|
||||
func (s *Storage) AddHuaweiSuccess(count int64) {
|
||||
total := s.GetHuaweiSuccess() + count
|
||||
s.setLevelDB(storage.HuaweiSuccessKey, total)
|
||||
}
|
||||
|
||||
// AddHuaweiError record counts of error Huawei push notification.
|
||||
func (s *Storage) AddHuaweiError(count int64) {
|
||||
total := s.GetHuaweiError() + count
|
||||
s.setLevelDB(storage.HuaweiErrorKey, total)
|
||||
}
|
||||
|
||||
// GetTotalCount show counts of all notification.
|
||||
func (s *Storage) GetTotalCount() int64 {
|
||||
var count int64
|
||||
s.getLevelDB(storage.TotalCountKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosSuccess show success counts of iOS notification.
|
||||
func (s *Storage) GetIosSuccess() int64 {
|
||||
var count int64
|
||||
s.getLevelDB(storage.IosSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosError show error counts of iOS notification.
|
||||
func (s *Storage) GetIosError() int64 {
|
||||
var count int64
|
||||
s.getLevelDB(storage.IosErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidSuccess show success counts of Android notification.
|
||||
func (s *Storage) GetAndroidSuccess() int64 {
|
||||
var count int64
|
||||
s.getLevelDB(storage.AndroidSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidError show error counts of Android notification.
|
||||
func (s *Storage) GetAndroidError() int64 {
|
||||
var count int64
|
||||
s.getLevelDB(storage.AndroidErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetHuaweiSuccess show success counts of Huawei notification.
|
||||
func (s *Storage) GetHuaweiSuccess() int64 {
|
||||
var count int64
|
||||
s.getLevelDB(storage.HuaweiSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetHuaweiError show error counts of Huawei notification.
|
||||
func (s *Storage) GetHuaweiError() int64 {
|
||||
var count int64
|
||||
s.getLevelDB(storage.HuaweiErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
@@ -2,8 +2,11 @@ package leveldb
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/appleboy/gorush/storage"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -21,34 +24,30 @@ func TestLevelDBEngine(t *testing.T) {
|
||||
levelDB := New(cfg)
|
||||
err := levelDB.Init()
|
||||
assert.Nil(t, err)
|
||||
levelDB.Reset()
|
||||
|
||||
levelDB.AddTotalCount(10)
|
||||
val = levelDB.GetTotalCount()
|
||||
levelDB.Add(storage.HuaweiSuccessKey, 10)
|
||||
val = levelDB.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(10), val)
|
||||
levelDB.AddTotalCount(10)
|
||||
val = levelDB.GetTotalCount()
|
||||
levelDB.Add(storage.HuaweiSuccessKey, 10)
|
||||
val = levelDB.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(20), val)
|
||||
|
||||
levelDB.AddIosSuccess(20)
|
||||
val = levelDB.GetIosSuccess()
|
||||
assert.Equal(t, int64(20), val)
|
||||
|
||||
levelDB.AddIosError(30)
|
||||
val = levelDB.GetIosError()
|
||||
assert.Equal(t, int64(30), val)
|
||||
|
||||
levelDB.AddAndroidSuccess(40)
|
||||
val = levelDB.GetAndroidSuccess()
|
||||
assert.Equal(t, int64(40), val)
|
||||
|
||||
levelDB.AddAndroidError(50)
|
||||
val = levelDB.GetAndroidError()
|
||||
assert.Equal(t, int64(50), val)
|
||||
|
||||
levelDB.Reset()
|
||||
val = levelDB.GetAndroidError()
|
||||
levelDB.Set(storage.HuaweiSuccessKey, 0)
|
||||
val = levelDB.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(0), val)
|
||||
|
||||
// test concurrency issues
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
levelDB.Add(storage.HuaweiSuccessKey, 1)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
val = levelDB.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(10), val)
|
||||
|
||||
assert.NoError(t, levelDB.Close())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user