Separate storage logic from actual business logic (#649)

This commit is contained in:
落心
2022-02-03 17:03:37 +08:00
committed by GitHub
parent 80df82052a
commit 17dad5758f
17 changed files with 380 additions and 858 deletions

View File

@@ -4,10 +4,9 @@ import (
"log"
"os"
"strconv"
"sync"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/storage"
"github.com/dgraph-io/badger/v3"
)
@@ -24,6 +23,26 @@ type Storage struct {
opts badger.Options
name string
db *badger.DB
lock sync.RWMutex
}
func (s *Storage) Add(key string, count int64) {
s.lock.Lock()
defer s.lock.Unlock()
s.setBadger(key, s.getBadger(key)+count)
}
func (s *Storage) Set(key string, count int64) {
s.lock.Lock()
defer s.lock.Unlock()
s.setBadger(key, count)
}
func (s *Storage) Get(key string) int64 {
s.lock.RLock()
defer s.lock.RUnlock()
return s.getBadger(key)
}
// Init client storage.
@@ -50,17 +69,6 @@ func (s *Storage) Close() error {
return s.db.Close()
}
// Reset Client storage.
func (s *Storage) Reset() {
s.setBadger(storage.TotalCountKey, 0)
s.setBadger(storage.IosSuccessKey, 0)
s.setBadger(storage.IosErrorKey, 0)
s.setBadger(storage.AndroidSuccessKey, 0)
s.setBadger(storage.AndroidErrorKey, 0)
s.setBadger(storage.HuaweiSuccessKey, 0)
s.setBadger(storage.HuaweiErrorKey, 0)
}
func (s *Storage) setBadger(key string, count int64) {
err := s.db.Update(func(txn *badger.Txn) error {
value := strconv.FormatInt(count, 10)
@@ -71,126 +79,28 @@ func (s *Storage) setBadger(key string, count int64) {
}
}
func (s *Storage) getBadger(key string, count *int64) {
func (s *Storage) getBadger(key string) int64 {
var count int64
err := s.db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(key))
if err != nil {
return err
}
dst := []byte{}
var dst []byte
val, err := item.ValueCopy(dst)
if err != nil {
return err
}
i, err := strconv.ParseInt(string(val), 10, 64)
count, err = strconv.ParseInt(string(val), 10, 64)
if err != nil {
return err
}
*count = i
return nil
})
if err != nil {
log.Println(s.name, "get error:", err.Error())
}
}
// AddTotalCount record push notification count.
func (s *Storage) AddTotalCount(count int64) {
total := s.GetTotalCount() + count
s.setBadger(storage.TotalCountKey, total)
}
// AddIosSuccess record counts of success iOS push notification.
func (s *Storage) AddIosSuccess(count int64) {
total := s.GetIosSuccess() + count
s.setBadger(storage.IosSuccessKey, total)
}
// AddIosError record counts of error iOS push notification.
func (s *Storage) AddIosError(count int64) {
total := s.GetIosError() + count
s.setBadger(storage.IosErrorKey, total)
}
// AddAndroidSuccess record counts of success Android push notification.
func (s *Storage) AddAndroidSuccess(count int64) {
total := s.GetAndroidSuccess() + count
s.setBadger(storage.AndroidSuccessKey, total)
}
// AddAndroidError record counts of error Android push notification.
func (s *Storage) AddAndroidError(count int64) {
total := s.GetAndroidError() + count
s.setBadger(storage.AndroidErrorKey, total)
}
// AddHuaweiSuccess record counts of success Huawei push notification.
func (s *Storage) AddHuaweiSuccess(count int64) {
total := s.GetHuaweiSuccess() + count
s.setBadger(storage.HuaweiSuccessKey, total)
}
// AddHuaweiError record counts of error Huawei push notification.
func (s *Storage) AddHuaweiError(count int64) {
total := s.GetHuaweiError() + count
s.setBadger(storage.HuaweiErrorKey, total)
}
// GetTotalCount show counts of all notification.
func (s *Storage) GetTotalCount() int64 {
var count int64
s.getBadger(storage.TotalCountKey, &count)
return count
}
// GetIosSuccess show success counts of iOS notification.
func (s *Storage) GetIosSuccess() int64 {
var count int64
s.getBadger(storage.IosSuccessKey, &count)
return count
}
// GetIosError show error counts of iOS notification.
func (s *Storage) GetIosError() int64 {
var count int64
s.getBadger(storage.IosErrorKey, &count)
return count
}
// GetAndroidSuccess show success counts of Android notification.
func (s *Storage) GetAndroidSuccess() int64 {
var count int64
s.getBadger(storage.AndroidSuccessKey, &count)
return count
}
// GetAndroidError show error counts of Android notification.
func (s *Storage) GetAndroidError() int64 {
var count int64
s.getBadger(storage.AndroidErrorKey, &count)
return count
}
// GetHuaweiSuccess show success counts of Huawei notification.
func (s *Storage) GetHuaweiSuccess() int64 {
var count int64
s.getBadger(storage.HuaweiSuccessKey, &count)
return count
}
// GetHuaweiError show error counts of Huawei notification.
func (s *Storage) GetHuaweiError() int64 {
var count int64
s.getBadger(storage.HuaweiErrorKey, &count)
return count
}

View File

@@ -1,8 +1,11 @@
package badger
import (
"sync"
"testing"
"github.com/appleboy/gorush/storage"
"github.com/appleboy/gorush/config"
"github.com/stretchr/testify/assert"
)
@@ -15,35 +18,30 @@ func TestBadgerEngine(t *testing.T) {
badger := New(cfg)
err := badger.Init()
assert.Nil(t, err)
badger.Reset()
badger.AddTotalCount(10)
val = badger.GetTotalCount()
badger.Add(storage.HuaweiSuccessKey, 10)
val = badger.Get(storage.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
badger.AddTotalCount(10)
val = badger.GetTotalCount()
badger.Add(storage.HuaweiSuccessKey, 10)
val = badger.Get(storage.HuaweiSuccessKey)
assert.Equal(t, int64(20), val)
badger.AddIosSuccess(20)
val = badger.GetIosSuccess()
assert.Equal(t, int64(20), val)
badger.AddIosError(30)
val = badger.GetIosError()
assert.Equal(t, int64(30), val)
badger.AddAndroidSuccess(40)
val = badger.GetAndroidSuccess()
assert.Equal(t, int64(40), val)
badger.AddAndroidError(50)
val = badger.GetAndroidError()
assert.Equal(t, int64(50), val)
// test reset db
badger.Reset()
val = badger.GetAndroidError()
badger.Set(storage.HuaweiSuccessKey, 0)
val = badger.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() {
badger.Add(storage.HuaweiSuccessKey, 1)
wg.Done()
}()
}
wg.Wait()
val = badger.Get(storage.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
assert.NoError(t, badger.Close())
}