Feature #61 support redis engine.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-22 17:54:00 +08:00
parent b690825cd4
commit d95cb2abb5
5 changed files with 194 additions and 15 deletions

View File

@ -36,4 +36,4 @@ stat:
redis: redis:
addr: "localhost:6379" addr: "localhost:6379"
password: "" password: ""
db: "0" db: 0

View File

@ -69,7 +69,7 @@ type SectionStat struct {
type SectionRedis struct { type SectionRedis struct {
Addr string `yaml:"addr"` Addr string `yaml:"addr"`
Password string `yaml:"password"` Password string `yaml:"password"`
DB string `yaml:"db"` DB int64 `yaml:"db"`
} }
// BuildDefaultPushConf is default config setting. // BuildDefaultPushConf is default config setting.
@ -112,7 +112,7 @@ func BuildDefaultPushConf() ConfYaml {
conf.Stat.Engine = "memory" conf.Stat.Engine = "memory"
conf.Stat.Redis.Addr = "localhost:6379" conf.Stat.Redis.Addr = "localhost:6379"
conf.Stat.Redis.Password = "" conf.Stat.Redis.Password = ""
conf.Stat.Redis.DB = "0" conf.Stat.Redis.DB = 0
return conf return conf
} }

View File

@ -4,6 +4,7 @@ import (
"crypto/tls" "crypto/tls"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
apns "github.com/sideshow/apns2" apns "github.com/sideshow/apns2"
"gopkg.in/redis.v3"
) )
var ( var (
@ -21,4 +22,6 @@ var (
LogError *logrus.Logger LogError *logrus.Logger
// RushStatus is notification status // RushStatus is notification status
RushStatus StatusApp RushStatus StatusApp
// Redis
RedisClient *redis.Client
) )

View File

@ -2,7 +2,9 @@ package gorush
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gopkg.in/redis.v3"
"net/http" "net/http"
"strconv"
"sync/atomic" "sync/atomic"
) )
@ -27,8 +29,7 @@ type IosStatus struct {
PushError int64 `json:"push_error"` PushError int64 `json:"push_error"`
} }
// InitAppStatus for initialize app status func initApp() {
func InitAppStatus() {
RushStatus.TotalCount = 0 RushStatus.TotalCount = 0
RushStatus.Ios.PushSuccess = 0 RushStatus.Ios.PushSuccess = 0
RushStatus.Ios.PushError = 0 RushStatus.Ios.PushError = 0
@ -36,24 +37,152 @@ func InitAppStatus() {
RushStatus.Android.PushError = 0 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) { 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) { 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) { 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) { 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) { 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) { func appStatusHandler(c *gin.Context) {
@ -61,11 +190,11 @@ func appStatusHandler(c *gin.Context) {
result.QueueMax = cap(QueueNotification) result.QueueMax = cap(QueueNotification)
result.QueueUsage = len(QueueNotification) result.QueueUsage = len(QueueNotification)
result.TotalCount = atomic.LoadInt64(&RushStatus.TotalCount) result.TotalCount = getTotalCount()
result.Ios.PushSuccess = atomic.LoadInt64(&RushStatus.Ios.PushSuccess) result.Ios.PushSuccess = getIosSuccess()
result.Ios.PushError = atomic.LoadInt64(&RushStatus.Ios.PushError) result.Ios.PushError = getIosError()
result.Android.PushSuccess = atomic.LoadInt64(&RushStatus.Android.PushSuccess) result.Android.PushSuccess = getAndroidSuccess()
result.Android.PushError = atomic.LoadInt64(&RushStatus.Android.PushError) result.Android.PushError = getAndroidError()
c.JSON(http.StatusOK, result) c.JSON(http.StatusOK, result)
} }

View File

@ -50,3 +50,50 @@ func TestAddAndroidError(t *testing.T) {
assert.Equal(t, int64(1000), val) 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)
}