From d95cb2abb53fc99cbfa1647eacd6c27d700179a1 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 22 Apr 2016 17:54:00 +0800 Subject: [PATCH] Feature #61 support redis engine. Signed-off-by: Bo-Yi Wu --- config/config.yml | 2 +- gorush/config.go | 4 +- gorush/global.go | 3 + gorush/status.go | 153 ++++++++++++++++++++++++++++++++++++++---- gorush/status_test.go | 47 +++++++++++++ 5 files changed, 194 insertions(+), 15 deletions(-) diff --git a/config/config.yml b/config/config.yml index 6d90481..dd96f17 100644 --- a/config/config.yml +++ b/config/config.yml @@ -36,4 +36,4 @@ stat: redis: addr: "localhost:6379" password: "" - db: "0" + db: 0 diff --git a/gorush/config.go b/gorush/config.go index 5af31a7..29a8899 100644 --- a/gorush/config.go +++ b/gorush/config.go @@ -69,7 +69,7 @@ type SectionStat struct { type SectionRedis struct { Addr string `yaml:"addr"` Password string `yaml:"password"` - DB string `yaml:"db"` + DB int64 `yaml:"db"` } // BuildDefaultPushConf is default config setting. @@ -112,7 +112,7 @@ func BuildDefaultPushConf() ConfYaml { conf.Stat.Engine = "memory" conf.Stat.Redis.Addr = "localhost:6379" conf.Stat.Redis.Password = "" - conf.Stat.Redis.DB = "0" + conf.Stat.Redis.DB = 0 return conf } diff --git a/gorush/global.go b/gorush/global.go index 234604f..f5a5008 100644 --- a/gorush/global.go +++ b/gorush/global.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "github.com/Sirupsen/logrus" apns "github.com/sideshow/apns2" + "gopkg.in/redis.v3" ) var ( @@ -21,4 +22,6 @@ var ( LogError *logrus.Logger // RushStatus is notification status RushStatus StatusApp + // Redis + RedisClient *redis.Client ) diff --git a/gorush/status.go b/gorush/status.go index b4dc55c..6d5f1ca 100644 --- a/gorush/status.go +++ b/gorush/status.go @@ -2,7 +2,9 @@ package gorush import ( "github.com/gin-gonic/gin" + "gopkg.in/redis.v3" "net/http" + "strconv" "sync/atomic" ) @@ -27,8 +29,7 @@ type IosStatus struct { PushError int64 `json:"push_error"` } -// InitAppStatus for initialize app status -func InitAppStatus() { +func initApp() { RushStatus.TotalCount = 0 RushStatus.Ios.PushSuccess = 0 RushStatus.Ios.PushError = 0 @@ -36,24 +37,152 @@ func InitAppStatus() { RushStatus.Android.PushError = 0 } +// InitAppStatus for initialize app status +func InitAppStatus() { + switch PushConf.Stat.Engine { + case "memory": + initApp() + case "redis": + RedisClient = redis.NewClient(&redis.Options{ + Addr: PushConf.Stat.Redis.Addr, + Password: PushConf.Stat.Redis.Password, + DB: PushConf.Stat.Redis.DB, + }) + default: + initApp() + } + +} + func addTotalCount(count int64) { - atomic.AddInt64(&RushStatus.TotalCount, count) + switch PushConf.Stat.Engine { + case "memory": + atomic.AddInt64(&RushStatus.TotalCount, count) + case "redis": + RedisClient.Set("key1", strconv.Itoa(int(count)), 0) + default: + atomic.AddInt64(&RushStatus.TotalCount, count) + } } func addIosSuccess(count int64) { - atomic.AddInt64(&RushStatus.Ios.PushSuccess, count) + switch PushConf.Stat.Engine { + case "memory": + atomic.AddInt64(&RushStatus.Ios.PushSuccess, count) + case "redis": + RedisClient.Set("key2", strconv.Itoa(int(count)), 0) + default: + atomic.AddInt64(&RushStatus.Ios.PushSuccess, count) + } } func addIosError(count int64) { - atomic.AddInt64(&RushStatus.Ios.PushError, count) + switch PushConf.Stat.Engine { + case "memory": + atomic.AddInt64(&RushStatus.Ios.PushError, count) + case "redis": + RedisClient.Set("key3", strconv.Itoa(int(count)), 0) + default: + atomic.AddInt64(&RushStatus.Ios.PushError, count) + } } func addAndroidSuccess(count int64) { - atomic.AddInt64(&RushStatus.Android.PushSuccess, count) + switch PushConf.Stat.Engine { + case "memory": + atomic.AddInt64(&RushStatus.Android.PushSuccess, count) + case "redis": + + RedisClient.Set("key4", strconv.Itoa(int(count)), 0) + default: + atomic.AddInt64(&RushStatus.Android.PushSuccess, count) + } } func addAndroidError(count int64) { - atomic.AddInt64(&RushStatus.Android.PushError, count) + switch PushConf.Stat.Engine { + case "memory": + atomic.AddInt64(&RushStatus.Android.PushError, count) + case "redis": + RedisClient.Set("key5", strconv.Itoa(int(count)), 0) + default: + atomic.AddInt64(&RushStatus.Android.PushError, count) + } +} + +func getTotalCount() int64 { + var count int64 + switch PushConf.Stat.Engine { + case "memory": + count = atomic.LoadInt64(&RushStatus.TotalCount) + case "redis": + val, _ := RedisClient.Get("key1").Result() + count, _ = strconv.ParseInt(val, 10, 64) + default: + count = atomic.LoadInt64(&RushStatus.TotalCount) + } + + return count +} + +func getIosSuccess() int64 { + var count int64 + switch PushConf.Stat.Engine { + case "memory": + count = atomic.LoadInt64(&RushStatus.Ios.PushSuccess) + case "redis": + val, _ := RedisClient.Get("key2").Result() + count, _ = strconv.ParseInt(val, 10, 64) + default: + count = atomic.LoadInt64(&RushStatus.Ios.PushSuccess) + } + + return count +} + +func getIosError() int64 { + var count int64 + switch PushConf.Stat.Engine { + case "memory": + count = atomic.LoadInt64(&RushStatus.Ios.PushError) + case "redis": + val, _ := RedisClient.Get("key3").Result() + count, _ = strconv.ParseInt(val, 10, 64) + default: + count = atomic.LoadInt64(&RushStatus.Ios.PushError) + } + + return count +} + +func getAndroidSuccess() int64 { + var count int64 + switch PushConf.Stat.Engine { + case "memory": + count = atomic.LoadInt64(&RushStatus.Android.PushSuccess) + case "redis": + val, _ := RedisClient.Get("key4").Result() + count, _ = strconv.ParseInt(val, 10, 64) + default: + count = atomic.LoadInt64(&RushStatus.Android.PushSuccess) + } + + return count +} + +func getAndroidError() int64 { + var count int64 + switch PushConf.Stat.Engine { + case "memory": + count = atomic.LoadInt64(&RushStatus.Android.PushError) + case "redis": + val, _ := RedisClient.Get("key5").Result() + count, _ = strconv.ParseInt(val, 10, 64) + default: + count = atomic.LoadInt64(&RushStatus.Android.PushError) + } + + return count } func appStatusHandler(c *gin.Context) { @@ -61,11 +190,11 @@ func appStatusHandler(c *gin.Context) { result.QueueMax = cap(QueueNotification) result.QueueUsage = len(QueueNotification) - result.TotalCount = atomic.LoadInt64(&RushStatus.TotalCount) - result.Ios.PushSuccess = atomic.LoadInt64(&RushStatus.Ios.PushSuccess) - result.Ios.PushError = atomic.LoadInt64(&RushStatus.Ios.PushError) - result.Android.PushSuccess = atomic.LoadInt64(&RushStatus.Android.PushSuccess) - result.Android.PushError = atomic.LoadInt64(&RushStatus.Android.PushError) + result.TotalCount = getTotalCount() + result.Ios.PushSuccess = getIosSuccess() + result.Ios.PushError = getIosError() + result.Android.PushSuccess = getAndroidSuccess() + result.Android.PushError = getAndroidError() c.JSON(http.StatusOK, result) } diff --git a/gorush/status_test.go b/gorush/status_test.go index 0aa8017..371d6dd 100644 --- a/gorush/status_test.go +++ b/gorush/status_test.go @@ -50,3 +50,50 @@ func TestAddAndroidError(t *testing.T) { assert.Equal(t, int64(1000), val) } + +func TestStatForRedisEngine(t *testing.T) { + var val int64 + PushConf.Stat.Engine = "redis" + PushConf.Stat.Redis.Addr = "localhost:6379" + InitAppStatus() + + addTotalCount(1000) + addIosSuccess(1000) + addIosError(1000) + addAndroidSuccess(1000) + addAndroidError(1000) + + val = getTotalCount() + assert.Equal(t, int64(1000), val) + val = getIosSuccess() + assert.Equal(t, int64(1000), val) + val = getIosError() + assert.Equal(t, int64(1000), val) + val = getAndroidSuccess() + assert.Equal(t, int64(1000), val) + val = getAndroidError() + assert.Equal(t, int64(1000), val) +} + +func TestDefaultEngine(t *testing.T) { + var val int64 + PushConf.Stat.Engine = "test" + InitAppStatus() + + addTotalCount(1000) + addIosSuccess(1000) + addIosError(1000) + addAndroidSuccess(1000) + addAndroidError(1000) + + val = getTotalCount() + assert.Equal(t, int64(1000), val) + val = getIosSuccess() + assert.Equal(t, int64(1000), val) + val = getIosError() + assert.Equal(t, int64(1000), val) + val = getAndroidSuccess() + assert.Equal(t, int64(1000), val) + val = getAndroidError() + assert.Equal(t, int64(1000), val) +}