Separate storage logic from actual business logic (#649)
This commit is contained in:
@@ -4,10 +4,9 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/appleboy/gorush/storage"
|
||||
|
||||
"github.com/tidwall/buntdb"
|
||||
)
|
||||
|
||||
@@ -22,6 +21,25 @@ func New(config *config.ConfYaml) *Storage {
|
||||
type Storage struct {
|
||||
config *config.ConfYaml
|
||||
db *buntdb.DB
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *Storage) Add(key string, count int64) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
s.setBuntDB(key, s.getBuntDB(key)+count)
|
||||
}
|
||||
|
||||
func (s *Storage) Set(key string, count int64) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
s.setBuntDB(key, count)
|
||||
}
|
||||
|
||||
func (s *Storage) Get(key string) int64 {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
return s.getBuntDB(key)
|
||||
}
|
||||
|
||||
// Init client storage.
|
||||
@@ -40,17 +58,6 @@ func (s *Storage) Close() error {
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
// Reset Client storage.
|
||||
func (s *Storage) Reset() {
|
||||
s.setBuntDB(storage.TotalCountKey, 0)
|
||||
s.setBuntDB(storage.IosSuccessKey, 0)
|
||||
s.setBuntDB(storage.IosErrorKey, 0)
|
||||
s.setBuntDB(storage.AndroidSuccessKey, 0)
|
||||
s.setBuntDB(storage.AndroidErrorKey, 0)
|
||||
s.setBuntDB(storage.HuaweiSuccessKey, 0)
|
||||
s.setBuntDB(storage.HuaweiErrorKey, 0)
|
||||
}
|
||||
|
||||
func (s *Storage) setBuntDB(key string, count int64) {
|
||||
err := s.db.Update(func(tx *buntdb.Tx) error {
|
||||
if _, _, err := tx.Set(key, fmt.Sprintf("%d", count), nil); err != nil {
|
||||
@@ -63,111 +70,16 @@ func (s *Storage) setBuntDB(key string, count int64) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Storage) getBuntDB(key string, count *int64) {
|
||||
func (s *Storage) getBuntDB(key string) int64 {
|
||||
var count int64
|
||||
err := s.db.View(func(tx *buntdb.Tx) error {
|
||||
val, _ := tx.Get(key)
|
||||
*count, _ = strconv.ParseInt(val, 10, 64)
|
||||
count, _ = strconv.ParseInt(val, 10, 64)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Println("BuntDB get error:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// AddTotalCount record push notification count.
|
||||
func (s *Storage) AddTotalCount(count int64) {
|
||||
total := s.GetTotalCount() + count
|
||||
s.setBuntDB(storage.TotalCountKey, total)
|
||||
}
|
||||
|
||||
// AddIosSuccess record counts of success iOS push notification.
|
||||
func (s *Storage) AddIosSuccess(count int64) {
|
||||
total := s.GetIosSuccess() + count
|
||||
s.setBuntDB(storage.IosSuccessKey, total)
|
||||
}
|
||||
|
||||
// AddIosError record counts of error iOS push notification.
|
||||
func (s *Storage) AddIosError(count int64) {
|
||||
total := s.GetIosError() + count
|
||||
s.setBuntDB(storage.IosErrorKey, total)
|
||||
}
|
||||
|
||||
// AddAndroidSuccess record counts of success Android push notification.
|
||||
func (s *Storage) AddAndroidSuccess(count int64) {
|
||||
total := s.GetAndroidSuccess() + count
|
||||
s.setBuntDB(storage.AndroidSuccessKey, total)
|
||||
}
|
||||
|
||||
// AddAndroidError record counts of error Android push notification.
|
||||
func (s *Storage) AddAndroidError(count int64) {
|
||||
total := s.GetAndroidError() + count
|
||||
s.setBuntDB(storage.AndroidErrorKey, total)
|
||||
}
|
||||
|
||||
// AddHuaweiSuccess record counts of success Huawei push notification.
|
||||
func (s *Storage) AddHuaweiSuccess(count int64) {
|
||||
total := s.GetHuaweiSuccess() + count
|
||||
s.setBuntDB(storage.HuaweiSuccessKey, total)
|
||||
}
|
||||
|
||||
// AddHuaweiError record counts of error Huawei push notification.
|
||||
func (s *Storage) AddHuaweiError(count int64) {
|
||||
total := s.GetHuaweiError() + count
|
||||
s.setBuntDB(storage.HuaweiErrorKey, total)
|
||||
}
|
||||
|
||||
// GetTotalCount show counts of all notification.
|
||||
func (s *Storage) GetTotalCount() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(storage.TotalCountKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosSuccess show success counts of iOS notification.
|
||||
func (s *Storage) GetIosSuccess() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(storage.IosSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosError show error counts of iOS notification.
|
||||
func (s *Storage) GetIosError() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(storage.IosErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidSuccess show success counts of Android notification.
|
||||
func (s *Storage) GetAndroidSuccess() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(storage.AndroidSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidError show error counts of Android notification.
|
||||
func (s *Storage) GetAndroidError() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(storage.AndroidErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetHuaweiSuccess show success counts of Huawei notification.
|
||||
func (s *Storage) GetHuaweiSuccess() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(storage.HuaweiSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetHuaweiError show error counts of Huawei notification.
|
||||
func (s *Storage) GetHuaweiError() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(storage.HuaweiErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
@@ -2,8 +2,11 @@ package buntdb
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/appleboy/gorush/storage"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -21,34 +24,30 @@ func TestBuntDBEngine(t *testing.T) {
|
||||
buntDB := New(cfg)
|
||||
err := buntDB.Init()
|
||||
assert.Nil(t, err)
|
||||
buntDB.Reset()
|
||||
|
||||
buntDB.AddTotalCount(10)
|
||||
val = buntDB.GetTotalCount()
|
||||
buntDB.Add(storage.HuaweiSuccessKey, 10)
|
||||
val = buntDB.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(10), val)
|
||||
buntDB.AddTotalCount(10)
|
||||
val = buntDB.GetTotalCount()
|
||||
buntDB.Add(storage.HuaweiSuccessKey, 10)
|
||||
val = buntDB.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(20), val)
|
||||
|
||||
buntDB.AddIosSuccess(20)
|
||||
val = buntDB.GetIosSuccess()
|
||||
assert.Equal(t, int64(20), val)
|
||||
|
||||
buntDB.AddIosError(30)
|
||||
val = buntDB.GetIosError()
|
||||
assert.Equal(t, int64(30), val)
|
||||
|
||||
buntDB.AddAndroidSuccess(40)
|
||||
val = buntDB.GetAndroidSuccess()
|
||||
assert.Equal(t, int64(40), val)
|
||||
|
||||
buntDB.AddAndroidError(50)
|
||||
val = buntDB.GetAndroidError()
|
||||
assert.Equal(t, int64(50), val)
|
||||
|
||||
buntDB.Reset()
|
||||
val = buntDB.GetAndroidError()
|
||||
buntDB.Set(storage.HuaweiSuccessKey, 0)
|
||||
val = buntDB.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() {
|
||||
buntDB.Add(storage.HuaweiSuccessKey, 1)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
val = buntDB.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(10), val)
|
||||
|
||||
assert.NoError(t, buntDB.Close())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user