Separate storage logic from actual business logic (#649)
This commit is contained in:
@@ -1,148 +1,48 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"sync"
|
||||
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
// statApp is app status structure
|
||||
type statApp struct {
|
||||
TotalCount int64 `json:"total_count"`
|
||||
Ios IosStatus `json:"ios"`
|
||||
Android AndroidStatus `json:"android"`
|
||||
Huawei HuaweiStatus `json:"huawei"`
|
||||
}
|
||||
|
||||
// AndroidStatus is android structure
|
||||
type AndroidStatus struct {
|
||||
PushSuccess int64 `json:"push_success"`
|
||||
PushError int64 `json:"push_error"`
|
||||
}
|
||||
|
||||
// IosStatus is iOS structure
|
||||
type IosStatus struct {
|
||||
PushSuccess int64 `json:"push_success"`
|
||||
PushError int64 `json:"push_error"`
|
||||
}
|
||||
|
||||
// HuaweiStatus is android structure
|
||||
type HuaweiStatus struct {
|
||||
PushSuccess int64 `json:"push_success"`
|
||||
PushError int64 `json:"push_error"`
|
||||
}
|
||||
|
||||
// New func implements the storage interface for gorush (https://github.com/appleboy/gorush)
|
||||
func New() *Storage {
|
||||
return &Storage{
|
||||
stat: &statApp{},
|
||||
}
|
||||
return &Storage{}
|
||||
}
|
||||
|
||||
// Storage is interface structure
|
||||
type Storage struct {
|
||||
stat *statApp
|
||||
mem sync.Map
|
||||
}
|
||||
|
||||
func (s *Storage) getValueBtKey(key string) *atomic.Int64 {
|
||||
if val, ok := s.mem.Load(key); ok {
|
||||
return val.(*atomic.Int64)
|
||||
}
|
||||
val := atomic.NewInt64(0)
|
||||
s.mem.Store(key, val)
|
||||
return val
|
||||
}
|
||||
|
||||
func (s *Storage) Add(key string, count int64) {
|
||||
s.getValueBtKey(key).Add(count)
|
||||
}
|
||||
|
||||
func (s *Storage) Set(key string, count int64) {
|
||||
s.getValueBtKey(key).Store(count)
|
||||
}
|
||||
|
||||
func (s *Storage) Get(key string) int64 {
|
||||
return s.getValueBtKey(key).Load()
|
||||
}
|
||||
|
||||
// Init client storage.
|
||||
func (s *Storage) Init() error {
|
||||
func (*Storage) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close the storage connection
|
||||
func (s *Storage) Close() error {
|
||||
func (*Storage) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reset Client storage.
|
||||
func (s *Storage) Reset() {
|
||||
atomic.StoreInt64(&s.stat.TotalCount, 0)
|
||||
atomic.StoreInt64(&s.stat.Ios.PushSuccess, 0)
|
||||
atomic.StoreInt64(&s.stat.Ios.PushError, 0)
|
||||
atomic.StoreInt64(&s.stat.Android.PushSuccess, 0)
|
||||
atomic.StoreInt64(&s.stat.Android.PushError, 0)
|
||||
atomic.StoreInt64(&s.stat.Huawei.PushSuccess, 0)
|
||||
atomic.StoreInt64(&s.stat.Huawei.PushError, 0)
|
||||
}
|
||||
|
||||
// AddTotalCount record push notification count.
|
||||
func (s *Storage) AddTotalCount(count int64) {
|
||||
atomic.AddInt64(&s.stat.TotalCount, count)
|
||||
}
|
||||
|
||||
// AddIosSuccess record counts of success iOS push notification.
|
||||
func (s *Storage) AddIosSuccess(count int64) {
|
||||
atomic.AddInt64(&s.stat.Ios.PushSuccess, count)
|
||||
}
|
||||
|
||||
// AddIosError record counts of error iOS push notification.
|
||||
func (s *Storage) AddIosError(count int64) {
|
||||
atomic.AddInt64(&s.stat.Ios.PushError, count)
|
||||
}
|
||||
|
||||
// AddAndroidSuccess record counts of success Android push notification.
|
||||
func (s *Storage) AddAndroidSuccess(count int64) {
|
||||
atomic.AddInt64(&s.stat.Android.PushSuccess, count)
|
||||
}
|
||||
|
||||
// AddAndroidError record counts of error Android push notification.
|
||||
func (s *Storage) AddAndroidError(count int64) {
|
||||
atomic.AddInt64(&s.stat.Android.PushError, count)
|
||||
}
|
||||
|
||||
// AddHuaweiSuccess record counts of success Huawei push notification.
|
||||
func (s *Storage) AddHuaweiSuccess(count int64) {
|
||||
atomic.AddInt64(&s.stat.Huawei.PushSuccess, count)
|
||||
}
|
||||
|
||||
// AddHuaweiError record counts of error Huawei push notification.
|
||||
func (s *Storage) AddHuaweiError(count int64) {
|
||||
atomic.AddInt64(&s.stat.Huawei.PushError, count)
|
||||
}
|
||||
|
||||
// GetTotalCount show counts of all notification.
|
||||
func (s *Storage) GetTotalCount() int64 {
|
||||
count := atomic.LoadInt64(&s.stat.TotalCount)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosSuccess show success counts of iOS notification.
|
||||
func (s *Storage) GetIosSuccess() int64 {
|
||||
count := atomic.LoadInt64(&s.stat.Ios.PushSuccess)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosError show error counts of iOS notification.
|
||||
func (s *Storage) GetIosError() int64 {
|
||||
count := atomic.LoadInt64(&s.stat.Ios.PushError)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidSuccess show success counts of Android notification.
|
||||
func (s *Storage) GetAndroidSuccess() int64 {
|
||||
count := atomic.LoadInt64(&s.stat.Android.PushSuccess)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidError show error counts of Android notification.
|
||||
func (s *Storage) GetAndroidError() int64 {
|
||||
count := atomic.LoadInt64(&s.stat.Android.PushError)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetHuaweiSuccess show success counts of Huawei notification.
|
||||
func (s *Storage) GetHuaweiSuccess() int64 {
|
||||
count := atomic.LoadInt64(&s.stat.Huawei.PushSuccess)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetHuaweiError show error counts of Huawei notification.
|
||||
func (s *Storage) GetHuaweiError() int64 {
|
||||
count := atomic.LoadInt64(&s.stat.Huawei.PushError)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/appleboy/gorush/storage"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -10,37 +13,32 @@ func TestMemoryEngine(t *testing.T) {
|
||||
var val int64
|
||||
|
||||
memory := New()
|
||||
err := memory.Init()
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Nil(t, memory.Init())
|
||||
memory.Add(storage.HuaweiSuccessKey, 10)
|
||||
val = memory.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(10), val)
|
||||
memory.Add(storage.HuaweiSuccessKey, 10)
|
||||
val = memory.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(20), val)
|
||||
|
||||
memory.AddTotalCount(1)
|
||||
val = memory.GetTotalCount()
|
||||
assert.Equal(t, int64(1), val)
|
||||
|
||||
memory.AddTotalCount(100)
|
||||
val = memory.GetTotalCount()
|
||||
assert.Equal(t, int64(101), val)
|
||||
|
||||
memory.AddIosSuccess(2)
|
||||
val = memory.GetIosSuccess()
|
||||
assert.Equal(t, int64(2), val)
|
||||
|
||||
memory.AddIosError(3)
|
||||
val = memory.GetIosError()
|
||||
assert.Equal(t, int64(3), val)
|
||||
|
||||
memory.AddAndroidSuccess(4)
|
||||
val = memory.GetAndroidSuccess()
|
||||
assert.Equal(t, int64(4), val)
|
||||
|
||||
memory.AddAndroidError(5)
|
||||
val = memory.GetAndroidError()
|
||||
assert.Equal(t, int64(5), val)
|
||||
|
||||
// test reset db
|
||||
memory.Reset()
|
||||
val = memory.GetTotalCount()
|
||||
memory.Set(storage.HuaweiSuccessKey, 0)
|
||||
val = memory.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() {
|
||||
memory.Add(storage.HuaweiSuccessKey, 1)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
val = memory.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(10), val)
|
||||
|
||||
assert.NoError(t, memory.Close())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user