integrate redis engine.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-05-02 19:42:21 +08:00
parent 8df5c63860
commit 03ab8eeac7
12 changed files with 131 additions and 112 deletions

View File

@ -1,4 +1,4 @@
package gorush
package config
import (
"gopkg.in/yaml.v2"

View File

@ -1,4 +1,4 @@
package gorush
package config
import (
"github.com/stretchr/testify/assert"

View File

@ -3,13 +3,14 @@ package gorush
import (
"crypto/tls"
"github.com/Sirupsen/logrus"
"github.com/appleboy/gorush/gorush/config"
apns "github.com/sideshow/apns2"
"gopkg.in/redis.v3"
)
var (
// PushConf is gorush config
PushConf ConfYaml
PushConf config.ConfYaml
// QueueNotification is chan type
QueueNotification chan PushNotification
// CertificatePemIos is ios certificate file

View File

@ -2,6 +2,7 @@ package gorush
import (
"github.com/Sirupsen/logrus"
"github.com/appleboy/gorush/gorush/config"
"github.com/stretchr/testify/assert"
"testing"
)
@ -34,7 +35,7 @@ func TestSetLogOut(t *testing.T) {
}
func TestInitDefaultLog(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
// no errors on default config
assert.Nil(t, InitLog())
@ -45,7 +46,7 @@ func TestInitDefaultLog(t *testing.T) {
}
func TestAccessLevel(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Log.AccessLevel = "invalid"
@ -53,7 +54,7 @@ func TestAccessLevel(t *testing.T) {
}
func TestErrorLevel(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Log.ErrorLevel = "invalid"
@ -61,7 +62,7 @@ func TestErrorLevel(t *testing.T) {
}
func TestAccessLogPath(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Log.AccessLog = "logs/access.log"
@ -69,7 +70,7 @@ func TestAccessLogPath(t *testing.T) {
}
func TestErrorLogPath(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Log.ErrorLog = "logs/error.log"

View File

@ -2,6 +2,7 @@ package gorush
import (
"encoding/json"
"github.com/appleboy/gorush/gorush/config"
"github.com/buger/jsonparser"
"github.com/google/go-gcm"
"github.com/sideshow/apns2"
@ -13,7 +14,7 @@ import (
)
func TestDisabledAndroidIosConf(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
err := CheckPushConf()
@ -22,7 +23,7 @@ func TestDisabledAndroidIosConf(t *testing.T) {
}
func TestMissingIOSCertificate(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Ios.Enabled = true
PushConf.Ios.PemKeyPath = ""
@ -34,7 +35,7 @@ func TestMissingIOSCertificate(t *testing.T) {
}
func TestMissingAndroidAPIKey(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = ""
@ -46,7 +47,7 @@ func TestMissingAndroidAPIKey(t *testing.T) {
}
func TestCorrectConf(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = "xxxxx"
@ -217,7 +218,7 @@ func TestAndroidNotificationStructure(t *testing.T) {
}
func TestPushToIOS(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Ios.Enabled = true
PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem"
@ -235,7 +236,7 @@ func TestPushToIOS(t *testing.T) {
}
func TestPushToAndroidWrongAPIKey(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") + "a"
@ -251,7 +252,7 @@ func TestPushToAndroidWrongAPIKey(t *testing.T) {
}
func TestPushToAndroidWrongToken(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
@ -267,7 +268,7 @@ func TestPushToAndroidWrongToken(t *testing.T) {
}
func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
@ -287,7 +288,7 @@ func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
}
func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
@ -305,7 +306,7 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
}
func TestOverwriteAndroidAPIKey(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
@ -325,7 +326,7 @@ func TestOverwriteAndroidAPIKey(t *testing.T) {
}
func TestSenMultipleNotifications(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
InitWorkers(2, 2)
@ -360,7 +361,7 @@ func TestSenMultipleNotifications(t *testing.T) {
}
func TestDisabledAndroidNotifications(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Ios.Enabled = true
PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem"
@ -393,7 +394,7 @@ func TestDisabledAndroidNotifications(t *testing.T) {
}
func TestDisabledIosNotifications(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Ios.Enabled = false
PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem"
@ -426,7 +427,7 @@ func TestDisabledIosNotifications(t *testing.T) {
}
func TestMissingIosCertificate(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Ios.Enabled = true
PushConf.Ios.PemKeyPath = "test"
@ -436,7 +437,7 @@ func TestMissingIosCertificate(t *testing.T) {
}
func TestAPNSClientDevHost(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Ios.Enabled = true
PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem"
@ -446,7 +447,7 @@ func TestAPNSClientDevHost(t *testing.T) {
}
func TestAPNSClientProdHost(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Ios.Enabled = true
PushConf.Ios.Production = true
@ -521,7 +522,7 @@ func TestGCMMessage(t *testing.T) {
}
func TestCheckAndroidMessage(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")

View File

@ -2,6 +2,7 @@ package gorush
import (
"github.com/appleboy/gofight"
"github.com/appleboy/gorush/gorush/config"
"github.com/buger/jsonparser"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
@ -15,7 +16,7 @@ import (
var goVersion = runtime.Version()
func initTest() {
PushConf = BuildDefaultPushConf()
PushConf = config.BuildDefaultPushConf()
PushConf.Core.Mode = "test"
}

View File

@ -2,6 +2,7 @@ package gorush
import (
"github.com/appleboy/gorush/gorush/storage/memory"
"github.com/appleboy/gorush/gorush/storage/redis"
"github.com/gin-gonic/gin"
"net/http"
)
@ -32,8 +33,9 @@ func InitAppStatus() {
switch PushConf.Stat.Engine {
case "memory":
StatStorage = memory.New()
// case "redis":
// initRedis()
case "redis":
StatStorage = redis.New(PushConf)
StatStorage.Init()
// case "boltdb":
// initBoltDB()
default:

View File

@ -90,29 +90,32 @@ func TestStatForMemoryEngine(t *testing.T) {
// assert.Error(t, err)
// }
// func TestStatForRedisEngine(t *testing.T) {
// var val int64
// PushConf.Stat.Engine = "redis"
// PushConf.Stat.Redis.Addr = "localhost:6379"
// InitAppStatus()
func TestStatForRedisEngine(t *testing.T) {
var val int64
PushConf.Stat.Engine = "redis"
PushConf.Stat.Redis.Addr = "localhost:6379"
InitAppStatus()
// addTotalCount(10)
// addIosSuccess(20)
// addIosError(30)
// addAndroidSuccess(40)
// addAndroidError(50)
StatStorage.Init()
StatStorage.Reset()
// val = getTotalCount()
// assert.Equal(t, int64(10), val)
// val = getIosSuccess()
// assert.Equal(t, int64(20), val)
// val = getIosError()
// assert.Equal(t, int64(30), val)
// val = getAndroidSuccess()
// assert.Equal(t, int64(40), val)
// val = getAndroidError()
// assert.Equal(t, int64(50), val)
// }
StatStorage.AddTotalCount(100)
StatStorage.AddIosSuccess(200)
StatStorage.AddIosError(300)
StatStorage.AddAndroidSuccess(400)
StatStorage.AddAndroidError(500)
val = StatStorage.GetTotalCount()
assert.Equal(t, int64(100), val)
val = StatStorage.GetIosSuccess()
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)
}
// func TestDefaultEngine(t *testing.T) {
// var val int64

View File

@ -2,6 +2,8 @@ package gorush
// Storage interface
type Storage interface {
Init() error
Reset()
AddTotalCount(int64)
AddIosSuccess(int64)
AddIosError(int64)

View File

@ -34,6 +34,13 @@ type Storage struct {
stat *statApp
}
func (s *Storage) Init() error {
return nil
}
func (s *Storage) Reset() {
}
func (s *Storage) AddTotalCount(count int64) {
atomic.AddInt64(&s.stat.TotalCount, count)
}

View File

@ -1,18 +1,26 @@
package redis
import (
"github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/gorush/config"
"gopkg.in/redis.v3"
"log"
"strconv"
)
// 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"
)
var RedisClient *redis.Client
// 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{
stat: stat,
config: config,
}
}
@ -23,11 +31,10 @@ func getRedisInt64Result(key string, count *int64) {
}
type Storage struct {
config gorush.ConfYaml
stat gorush.StatusApp
config config.ConfYaml
}
func (s *Storage) initRedis() error {
func (s *Storage) Init() error {
RedisClient = redis.NewClient(&redis.Options{
Addr: s.config.Stat.Redis.Addr,
Password: s.config.Stat.Redis.Password,
@ -43,79 +50,73 @@ func (s *Storage) initRedis() error {
return err
}
s.stat.TotalCount = s.getTotalCount()
s.stat.Ios.PushSuccess = s.getIosSuccess()
s.stat.Ios.PushError = s.getIosError()
s.stat.Android.PushSuccess = s.getAndroidSuccess()
s.stat.Android.PushError = s.getAndroidError()
return nil
}
func (s *Storage) resetRedis() {
RedisClient.Set(gorush.TotalCountKey, strconv.Itoa(0), 0)
RedisClient.Set(gorush.IosSuccessKey, strconv.Itoa(0), 0)
RedisClient.Set(gorush.IosErrorKey, strconv.Itoa(0), 0)
RedisClient.Set(gorush.AndroidSuccessKey, strconv.Itoa(0), 0)
RedisClient.Set(gorush.AndroidErrorKey, strconv.Itoa(0), 0)
func (s *Storage) Reset() {
RedisClient.Set(TotalCountKey, strconv.Itoa(0), 0)
RedisClient.Set(IosSuccessKey, strconv.Itoa(0), 0)
RedisClient.Set(IosErrorKey, strconv.Itoa(0), 0)
RedisClient.Set(AndroidSuccessKey, strconv.Itoa(0), 0)
RedisClient.Set(AndroidErrorKey, strconv.Itoa(0), 0)
}
func (s *Storage) addTotalCount(count int64) {
total := s.getTotalCount() + count
RedisClient.Set(gorush.TotalCountKey, strconv.Itoa(int(total)), 0)
func (s *Storage) AddTotalCount(count int64) {
total := s.GetTotalCount() + count
RedisClient.Set(TotalCountKey, strconv.Itoa(int(total)), 0)
}
func (s *Storage) addIosSuccess(count int64) {
total := s.getIosSuccess() + count
RedisClient.Set(gorush.IosSuccessKey, strconv.Itoa(int(total)), 0)
func (s *Storage) AddIosSuccess(count int64) {
total := s.GetIosSuccess() + count
RedisClient.Set(IosSuccessKey, strconv.Itoa(int(total)), 0)
}
func (s *Storage) addIosError(count int64) {
total := s.getIosError() + count
RedisClient.Set(gorush.IosErrorKey, strconv.Itoa(int(total)), 0)
func (s *Storage) AddIosError(count int64) {
total := s.GetIosError() + count
RedisClient.Set(IosErrorKey, strconv.Itoa(int(total)), 0)
}
func (s *Storage) addAndroidSuccess(count int64) {
total := s.getAndroidSuccess() + count
RedisClient.Set(gorush.AndroidSuccessKey, strconv.Itoa(int(total)), 0)
func (s *Storage) AddAndroidSuccess(count int64) {
total := s.GetAndroidSuccess() + count
RedisClient.Set(AndroidSuccessKey, strconv.Itoa(int(total)), 0)
}
func (s *Storage) addAndroidError(count int64) {
total := s.getAndroidError() + count
RedisClient.Set(gorush.AndroidErrorKey, strconv.Itoa(int(total)), 0)
func (s *Storage) AddAndroidError(count int64) {
total := s.GetAndroidError() + count
RedisClient.Set(AndroidErrorKey, strconv.Itoa(int(total)), 0)
}
func (s *Storage) getTotalCount() int64 {
func (s *Storage) GetTotalCount() int64 {
var count int64
getRedisInt64Result(gorush.TotalCountKey, &count)
getRedisInt64Result(TotalCountKey, &count)
return count
}
func (s *Storage) getIosSuccess() int64 {
func (s *Storage) GetIosSuccess() int64 {
var count int64
getRedisInt64Result(gorush.IosSuccessKey, &count)
getRedisInt64Result(IosSuccessKey, &count)
return count
}
func (s *Storage) getIosError() int64 {
func (s *Storage) GetIosError() int64 {
var count int64
getRedisInt64Result(gorush.IosErrorKey, &count)
getRedisInt64Result(IosErrorKey, &count)
return count
}
func (s *Storage) getAndroidSuccess() int64 {
func (s *Storage) GetAndroidSuccess() int64 {
var count int64
getRedisInt64Result(gorush.AndroidSuccessKey, &count)
getRedisInt64Result(AndroidSuccessKey, &count)
return count
}
func (s *Storage) getAndroidError() int64 {
func (s *Storage) GetAndroidError() int64 {
var count int64
getRedisInt64Result(gorush.AndroidErrorKey, &count)
getRedisInt64Result(AndroidErrorKey, &count)
return count
}

View File

@ -1,17 +1,17 @@
package redis
import (
"github.com/appleboy/gorush/gorush"
c "github.com/appleboy/gorush/gorush/config"
"github.com/stretchr/testify/assert"
"testing"
)
func TestRedisServerError(t *testing.T) {
config := gorush.BuildDefaultPushConf()
config := c.BuildDefaultPushConf()
config.Stat.Redis.Addr = "localhost:6370"
redis := New(config, gorush.StatusApp{})
err := redis.initRedis()
redis := New(config)
err := redis.Init()
assert.Error(t, err)
}
@ -19,32 +19,32 @@ func TestRedisServerError(t *testing.T) {
func TestRedisEngine(t *testing.T) {
var val int64
config := gorush.BuildDefaultPushConf()
config := c.BuildDefaultPushConf()
redis := New(config, gorush.StatusApp{})
redis.initRedis()
redis.resetRedis()
redis := New(config)
redis.Init()
redis.Reset()
redis.addTotalCount(10)
val = redis.getTotalCount()
redis.AddTotalCount(10)
val = redis.GetTotalCount()
assert.Equal(t, int64(10), val)
redis.addTotalCount(10)
val = redis.getTotalCount()
redis.AddTotalCount(10)
val = redis.GetTotalCount()
assert.Equal(t, int64(20), val)
redis.addIosSuccess(20)
val = redis.getIosSuccess()
redis.AddIosSuccess(20)
val = redis.GetIosSuccess()
assert.Equal(t, int64(20), val)
redis.addIosError(30)
val = redis.getIosError()
redis.AddIosError(30)
val = redis.GetIosError()
assert.Equal(t, int64(30), val)
redis.addAndroidSuccess(40)
val = redis.getAndroidSuccess()
redis.AddAndroidSuccess(40)
val = redis.GetAndroidSuccess()
assert.Equal(t, int64(40), val)
redis.addAndroidError(50)
val = redis.getAndroidError()
redis.AddAndroidError(50)
val = redis.GetAndroidError()
assert.Equal(t, int64(50), val)
}