integrate boltdb engine.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
03ab8eeac7
commit
5994f48e7e
|
@ -1,6 +1,7 @@
|
||||||
package gorush
|
package gorush
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/appleboy/gorush/gorush/storage/boltdb"
|
||||||
"github.com/appleboy/gorush/gorush/storage/memory"
|
"github.com/appleboy/gorush/gorush/storage/memory"
|
||||||
"github.com/appleboy/gorush/gorush/storage/redis"
|
"github.com/appleboy/gorush/gorush/storage/redis"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -29,18 +30,27 @@ type IosStatus struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitAppStatus for initialize app status
|
// InitAppStatus for initialize app status
|
||||||
func InitAppStatus() {
|
func InitAppStatus() error {
|
||||||
switch PushConf.Stat.Engine {
|
switch PushConf.Stat.Engine {
|
||||||
case "memory":
|
case "memory":
|
||||||
StatStorage = memory.New()
|
StatStorage = memory.New()
|
||||||
case "redis":
|
case "redis":
|
||||||
StatStorage = redis.New(PushConf)
|
StatStorage = redis.New(PushConf)
|
||||||
StatStorage.Init()
|
err := StatStorage.Init()
|
||||||
// case "boltdb":
|
|
||||||
// initBoltDB()
|
if err != nil {
|
||||||
|
LogError.Error("redis error: " + err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
case "boltdb":
|
||||||
|
StatStorage = boltdb.New(PushConf)
|
||||||
default:
|
default:
|
||||||
StatStorage = memory.New()
|
StatStorage = memory.New()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func appStatusHandler(c *gin.Context) {
|
func appStatusHandler(c *gin.Context) {
|
||||||
|
|
|
@ -74,21 +74,23 @@ func TestStatForMemoryEngine(t *testing.T) {
|
||||||
assert.Equal(t, int64(500), val)
|
assert.Equal(t, int64(500), val)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestRedisServerSuccess(t *testing.T) {
|
func TestRedisServerSuccess(t *testing.T) {
|
||||||
// PushConf.Stat.Redis.Addr = "localhost:6379"
|
PushConf.Stat.Engine = "redis"
|
||||||
|
PushConf.Stat.Redis.Addr = "localhost:6379"
|
||||||
|
|
||||||
// err := initRedis()
|
err := InitAppStatus()
|
||||||
|
|
||||||
// assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// func TestRedisServerError(t *testing.T) {
|
func TestRedisServerError(t *testing.T) {
|
||||||
// PushConf.Stat.Redis.Addr = "localhost:6370"
|
PushConf.Stat.Engine = "redis"
|
||||||
|
PushConf.Stat.Redis.Addr = "localhost:6370"
|
||||||
|
|
||||||
// err := initRedis()
|
err := InitAppStatus()
|
||||||
|
|
||||||
// assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
// }
|
}
|
||||||
|
|
||||||
func TestStatForRedisEngine(t *testing.T) {
|
func TestStatForRedisEngine(t *testing.T) {
|
||||||
var val int64
|
var val int64
|
||||||
|
@ -117,48 +119,50 @@ func TestStatForRedisEngine(t *testing.T) {
|
||||||
assert.Equal(t, int64(500), val)
|
assert.Equal(t, int64(500), val)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestDefaultEngine(t *testing.T) {
|
func TestDefaultEngine(t *testing.T) {
|
||||||
// var val int64
|
var val int64
|
||||||
// PushConf.Stat.Engine = "test"
|
PushConf.Stat.Engine = "test"
|
||||||
// InitAppStatus()
|
InitAppStatus()
|
||||||
|
|
||||||
// addTotalCount(1)
|
StatStorage.AddTotalCount(100)
|
||||||
// addIosSuccess(2)
|
StatStorage.AddIosSuccess(200)
|
||||||
// addIosError(3)
|
StatStorage.AddIosError(300)
|
||||||
// addAndroidSuccess(4)
|
StatStorage.AddAndroidSuccess(400)
|
||||||
// addAndroidError(5)
|
StatStorage.AddAndroidError(500)
|
||||||
|
|
||||||
// val = getTotalCount()
|
val = StatStorage.GetTotalCount()
|
||||||
// assert.Equal(t, int64(1), val)
|
assert.Equal(t, int64(100), val)
|
||||||
// val = getIosSuccess()
|
val = StatStorage.GetIosSuccess()
|
||||||
// assert.Equal(t, int64(2), val)
|
assert.Equal(t, int64(200), val)
|
||||||
// val = getIosError()
|
val = StatStorage.GetIosError()
|
||||||
// assert.Equal(t, int64(3), val)
|
assert.Equal(t, int64(300), val)
|
||||||
// val = getAndroidSuccess()
|
val = StatStorage.GetAndroidSuccess()
|
||||||
// assert.Equal(t, int64(4), val)
|
assert.Equal(t, int64(400), val)
|
||||||
// val = getAndroidError()
|
val = StatStorage.GetAndroidError()
|
||||||
// assert.Equal(t, int64(5), val)
|
assert.Equal(t, int64(500), val)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// func TestStatForBoltDBEngine(t *testing.T) {
|
func TestStatForBoltDBEngine(t *testing.T) {
|
||||||
// var val int64
|
var val int64
|
||||||
// PushConf.Stat.Engine = "boltdb"
|
PushConf.Stat.Engine = "boltdb"
|
||||||
// InitAppStatus()
|
InitAppStatus()
|
||||||
|
|
||||||
// addTotalCount(100)
|
StatStorage.Reset()
|
||||||
// addIosSuccess(200)
|
|
||||||
// addIosError(300)
|
|
||||||
// addAndroidSuccess(400)
|
|
||||||
// addAndroidError(500)
|
|
||||||
|
|
||||||
// val = getTotalCount()
|
StatStorage.AddTotalCount(100)
|
||||||
// assert.Equal(t, int64(100), val)
|
StatStorage.AddIosSuccess(200)
|
||||||
// val = getIosSuccess()
|
StatStorage.AddIosError(300)
|
||||||
// assert.Equal(t, int64(200), val)
|
StatStorage.AddAndroidSuccess(400)
|
||||||
// val = getIosError()
|
StatStorage.AddAndroidError(500)
|
||||||
// assert.Equal(t, int64(300), val)
|
|
||||||
// val = getAndroidSuccess()
|
val = StatStorage.GetTotalCount()
|
||||||
// assert.Equal(t, int64(400), val)
|
assert.Equal(t, int64(100), val)
|
||||||
// val = getAndroidError()
|
val = StatStorage.GetIosSuccess()
|
||||||
// assert.Equal(t, int64(500), val)
|
assert.Equal(t, int64(200), val)
|
||||||
// }
|
val = StatStorage.GetIosError()
|
||||||
|
assert.Equal(t, int64(300), val)
|
||||||
|
val = StatStorage.GetAndroidSuccess()
|
||||||
|
assert.Equal(t, int64(400), val)
|
||||||
|
val = StatStorage.GetAndroidError()
|
||||||
|
assert.Equal(t, int64(500), val)
|
||||||
|
}
|
||||||
|
|
|
@ -1,37 +1,40 @@
|
||||||
package boltdb
|
package boltdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/appleboy/gorush/gorush"
|
"github.com/appleboy/gorush/gorush/config"
|
||||||
"github.com/asdine/storm"
|
"github.com/asdine/storm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Stat variable for redis
|
||||||
|
const (
|
||||||
|
TotalCountKey = "gorush-total-count"
|
||||||
|
IosSuccessKey = "gorush-ios-success-count"
|
||||||
|
IosErrorKey = "gorush-ios-error-count"
|
||||||
|
AndroidSuccessKey = "gorush-android-success-count"
|
||||||
|
AndroidErrorKey = "gorush-android-error-count"
|
||||||
|
)
|
||||||
|
|
||||||
// Storage implements the storage interface for gorush (https://github.com/appleboy/gorush)
|
// Storage implements the storage interface for gorush (https://github.com/appleboy/gorush)
|
||||||
func New(config gorush.ConfYaml, stat gorush.StatusApp) *Storage {
|
func New(config config.ConfYaml) *Storage {
|
||||||
return &Storage{
|
return &Storage{
|
||||||
stat: stat,
|
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
config gorush.ConfYaml
|
config config.ConfYaml
|
||||||
stat gorush.StatusApp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) initBoltDB() {
|
func (s *Storage) Init() error {
|
||||||
s.stat.TotalCount = s.getTotalCount()
|
return nil
|
||||||
s.stat.Ios.PushSuccess = s.getIosSuccess()
|
|
||||||
s.stat.Ios.PushError = s.getIosError()
|
|
||||||
s.stat.Android.PushSuccess = s.getAndroidSuccess()
|
|
||||||
s.stat.Android.PushError = s.getAndroidError()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) resetBoltDB() {
|
func (s *Storage) Reset() {
|
||||||
s.setBoltDB(gorush.TotalCountKey, 0)
|
s.setBoltDB(TotalCountKey, 0)
|
||||||
s.setBoltDB(gorush.IosSuccessKey, 0)
|
s.setBoltDB(IosSuccessKey, 0)
|
||||||
s.setBoltDB(gorush.IosErrorKey, 0)
|
s.setBoltDB(IosErrorKey, 0)
|
||||||
s.setBoltDB(gorush.AndroidSuccessKey, 0)
|
s.setBoltDB(AndroidSuccessKey, 0)
|
||||||
s.setBoltDB(gorush.AndroidErrorKey, 0)
|
s.setBoltDB(AndroidErrorKey, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) setBoltDB(key string, count int64) {
|
func (s *Storage) setBoltDB(key string, count int64) {
|
||||||
|
@ -46,62 +49,62 @@ func (s *Storage) getBoltDB(key string, count *int64) {
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) addTotalCount(count int64) {
|
func (s *Storage) AddTotalCount(count int64) {
|
||||||
total := s.getTotalCount() + count
|
total := s.GetTotalCount() + count
|
||||||
s.setBoltDB(gorush.TotalCountKey, total)
|
s.setBoltDB(TotalCountKey, total)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) addIosSuccess(count int64) {
|
func (s *Storage) AddIosSuccess(count int64) {
|
||||||
total := s.getIosSuccess() + count
|
total := s.GetIosSuccess() + count
|
||||||
s.setBoltDB(gorush.IosSuccessKey, total)
|
s.setBoltDB(IosSuccessKey, total)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) addIosError(count int64) {
|
func (s *Storage) AddIosError(count int64) {
|
||||||
total := s.getIosError() + count
|
total := s.GetIosError() + count
|
||||||
s.setBoltDB(gorush.IosErrorKey, total)
|
s.setBoltDB(IosErrorKey, total)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) addAndroidSuccess(count int64) {
|
func (s *Storage) AddAndroidSuccess(count int64) {
|
||||||
total := s.getAndroidSuccess() + count
|
total := s.GetAndroidSuccess() + count
|
||||||
s.setBoltDB(gorush.AndroidSuccessKey, total)
|
s.setBoltDB(AndroidSuccessKey, total)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) addAndroidError(count int64) {
|
func (s *Storage) AddAndroidError(count int64) {
|
||||||
total := s.getAndroidError() + count
|
total := s.GetAndroidError() + count
|
||||||
s.setBoltDB(gorush.AndroidErrorKey, total)
|
s.setBoltDB(AndroidErrorKey, total)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) getTotalCount() int64 {
|
func (s *Storage) GetTotalCount() int64 {
|
||||||
var count int64
|
var count int64
|
||||||
s.getBoltDB(gorush.TotalCountKey, &count)
|
s.getBoltDB(TotalCountKey, &count)
|
||||||
|
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) getIosSuccess() int64 {
|
func (s *Storage) GetIosSuccess() int64 {
|
||||||
var count int64
|
var count int64
|
||||||
s.getBoltDB(gorush.IosSuccessKey, &count)
|
s.getBoltDB(IosSuccessKey, &count)
|
||||||
|
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) getIosError() int64 {
|
func (s *Storage) GetIosError() int64 {
|
||||||
var count int64
|
var count int64
|
||||||
s.getBoltDB(gorush.IosErrorKey, &count)
|
s.getBoltDB(IosErrorKey, &count)
|
||||||
|
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) getAndroidSuccess() int64 {
|
func (s *Storage) GetAndroidSuccess() int64 {
|
||||||
var count int64
|
var count int64
|
||||||
s.getBoltDB(gorush.AndroidSuccessKey, &count)
|
s.getBoltDB(AndroidSuccessKey, &count)
|
||||||
|
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) getAndroidError() int64 {
|
func (s *Storage) GetAndroidError() int64 {
|
||||||
var count int64
|
var count int64
|
||||||
s.getBoltDB(gorush.AndroidErrorKey, &count)
|
s.getBoltDB(AndroidErrorKey, &count)
|
||||||
|
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package boltdb
|
package boltdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/appleboy/gorush/gorush"
|
c "github.com/appleboy/gorush/gorush/config"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -9,32 +9,32 @@ import (
|
||||||
func TestRedisEngine(t *testing.T) {
|
func TestRedisEngine(t *testing.T) {
|
||||||
var val int64
|
var val int64
|
||||||
|
|
||||||
config := gorush.BuildDefaultPushConf()
|
config := c.BuildDefaultPushConf()
|
||||||
|
|
||||||
boltDB := New(config, gorush.StatusApp{})
|
boltDB := New(config)
|
||||||
boltDB.initBoltDB()
|
boltDB.Init()
|
||||||
boltDB.resetBoltDB()
|
boltDB.Reset()
|
||||||
|
|
||||||
boltDB.addTotalCount(10)
|
boltDB.AddTotalCount(10)
|
||||||
val = boltDB.getTotalCount()
|
val = boltDB.GetTotalCount()
|
||||||
assert.Equal(t, int64(10), val)
|
assert.Equal(t, int64(10), val)
|
||||||
boltDB.addTotalCount(10)
|
boltDB.AddTotalCount(10)
|
||||||
val = boltDB.getTotalCount()
|
val = boltDB.GetTotalCount()
|
||||||
assert.Equal(t, int64(20), val)
|
assert.Equal(t, int64(20), val)
|
||||||
|
|
||||||
boltDB.addIosSuccess(20)
|
boltDB.AddIosSuccess(20)
|
||||||
val = boltDB.getIosSuccess()
|
val = boltDB.GetIosSuccess()
|
||||||
assert.Equal(t, int64(20), val)
|
assert.Equal(t, int64(20), val)
|
||||||
|
|
||||||
boltDB.addIosError(30)
|
boltDB.AddIosError(30)
|
||||||
val = boltDB.getIosError()
|
val = boltDB.GetIosError()
|
||||||
assert.Equal(t, int64(30), val)
|
assert.Equal(t, int64(30), val)
|
||||||
|
|
||||||
boltDB.addAndroidSuccess(40)
|
boltDB.AddAndroidSuccess(40)
|
||||||
val = boltDB.getAndroidSuccess()
|
val = boltDB.GetAndroidSuccess()
|
||||||
assert.Equal(t, int64(40), val)
|
assert.Equal(t, int64(40), val)
|
||||||
|
|
||||||
boltDB.addAndroidError(50)
|
boltDB.AddAndroidError(50)
|
||||||
val = boltDB.getAndroidError()
|
val = boltDB.GetAndroidError()
|
||||||
assert.Equal(t, int64(50), val)
|
assert.Equal(t, int64(50), val)
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ func New(config config.ConfYaml) *Storage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRedisInt64Result(key string, count *int64) {
|
func getInt64(key string, count *int64) {
|
||||||
val, _ := RedisClient.Get(key).Result()
|
val, _ := RedisClient.Get(key).Result()
|
||||||
*count, _ = strconv.ParseInt(val, 10, 64)
|
*count, _ = strconv.ParseInt(val, 10, 64)
|
||||||
}
|
}
|
||||||
|
@ -88,35 +88,35 @@ func (s *Storage) AddAndroidError(count int64) {
|
||||||
|
|
||||||
func (s *Storage) GetTotalCount() int64 {
|
func (s *Storage) GetTotalCount() int64 {
|
||||||
var count int64
|
var count int64
|
||||||
getRedisInt64Result(TotalCountKey, &count)
|
getInt64(TotalCountKey, &count)
|
||||||
|
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) GetIosSuccess() int64 {
|
func (s *Storage) GetIosSuccess() int64 {
|
||||||
var count int64
|
var count int64
|
||||||
getRedisInt64Result(IosSuccessKey, &count)
|
getInt64(IosSuccessKey, &count)
|
||||||
|
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) GetIosError() int64 {
|
func (s *Storage) GetIosError() int64 {
|
||||||
var count int64
|
var count int64
|
||||||
getRedisInt64Result(IosErrorKey, &count)
|
getInt64(IosErrorKey, &count)
|
||||||
|
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) GetAndroidSuccess() int64 {
|
func (s *Storage) GetAndroidSuccess() int64 {
|
||||||
var count int64
|
var count int64
|
||||||
getRedisInt64Result(AndroidSuccessKey, &count)
|
getInt64(AndroidSuccessKey, &count)
|
||||||
|
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) GetAndroidError() int64 {
|
func (s *Storage) GetAndroidError() int64 {
|
||||||
var count int64
|
var count int64
|
||||||
getRedisInt64Result(AndroidErrorKey, &count)
|
getInt64(AndroidErrorKey, &count)
|
||||||
|
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue