chore(storage): storage performance issues (#500)
* chore(storage): storage performance issues 1. close storage connection before shutdown the service 2. update windows image https://github.com/appleboy/gorush/issues/393 Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
@@ -10,9 +9,6 @@ import (
|
||||
"gopkg.in/redis.v5"
|
||||
)
|
||||
|
||||
//
|
||||
var redisClient *redis.Client
|
||||
|
||||
// New func implements the storage interface for gorush (https://github.com/appleboy/gorush)
|
||||
func New(config config.ConfYaml) *Storage {
|
||||
return &Storage{
|
||||
@@ -20,76 +16,76 @@ func New(config config.ConfYaml) *Storage {
|
||||
}
|
||||
}
|
||||
|
||||
func getInt64(key string, count *int64) {
|
||||
val, _ := redisClient.Get(key).Result()
|
||||
func (s *Storage) getInt64(key string, count *int64) {
|
||||
val, _ := s.client.Get(key).Result()
|
||||
*count, _ = strconv.ParseInt(val, 10, 64)
|
||||
}
|
||||
|
||||
// Storage is interface structure
|
||||
type Storage struct {
|
||||
config config.ConfYaml
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
// Init client storage.
|
||||
func (s *Storage) Init() error {
|
||||
redisClient = redis.NewClient(&redis.Options{
|
||||
s.client = redis.NewClient(&redis.Options{
|
||||
Addr: s.config.Stat.Redis.Addr,
|
||||
Password: s.config.Stat.Redis.Password,
|
||||
DB: s.config.Stat.Redis.DB,
|
||||
})
|
||||
_, err := s.client.Ping().Result()
|
||||
|
||||
_, err := redisClient.Ping().Result()
|
||||
return err
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
// redis server error
|
||||
log.Println("Can't connect redis server: " + err.Error())
|
||||
|
||||
return err
|
||||
// Close the storage connection
|
||||
func (s *Storage) Close() error {
|
||||
if s.client == nil {
|
||||
return nil
|
||||
}
|
||||
// Set initial values
|
||||
s.Reset()
|
||||
|
||||
return nil
|
||||
return s.client.Close()
|
||||
}
|
||||
|
||||
// Reset Client storage.
|
||||
func (s *Storage) Reset() {
|
||||
redisClient.Set(storage.TotalCountKey, int64(0), 0)
|
||||
redisClient.Set(storage.IosSuccessKey, int64(0), 0)
|
||||
redisClient.Set(storage.IosErrorKey, int64(0), 0)
|
||||
redisClient.Set(storage.AndroidSuccessKey, int64(0), 0)
|
||||
redisClient.Set(storage.AndroidErrorKey, int64(0), 0)
|
||||
s.client.Set(storage.TotalCountKey, int64(0), 0)
|
||||
s.client.Set(storage.IosSuccessKey, int64(0), 0)
|
||||
s.client.Set(storage.IosErrorKey, int64(0), 0)
|
||||
s.client.Set(storage.AndroidSuccessKey, int64(0), 0)
|
||||
s.client.Set(storage.AndroidErrorKey, int64(0), 0)
|
||||
}
|
||||
|
||||
// AddTotalCount record push notification count.
|
||||
func (s *Storage) AddTotalCount(count int64) {
|
||||
redisClient.IncrBy(storage.TotalCountKey, count)
|
||||
s.client.IncrBy(storage.TotalCountKey, count)
|
||||
}
|
||||
|
||||
// AddIosSuccess record counts of success iOS push notification.
|
||||
func (s *Storage) AddIosSuccess(count int64) {
|
||||
redisClient.IncrBy(storage.IosSuccessKey, count)
|
||||
s.client.IncrBy(storage.IosSuccessKey, count)
|
||||
}
|
||||
|
||||
// AddIosError record counts of error iOS push notification.
|
||||
func (s *Storage) AddIosError(count int64) {
|
||||
redisClient.IncrBy(storage.IosErrorKey, count)
|
||||
s.client.IncrBy(storage.IosErrorKey, count)
|
||||
}
|
||||
|
||||
// AddAndroidSuccess record counts of success Android push notification.
|
||||
func (s *Storage) AddAndroidSuccess(count int64) {
|
||||
redisClient.IncrBy(storage.AndroidSuccessKey, count)
|
||||
s.client.IncrBy(storage.AndroidSuccessKey, count)
|
||||
}
|
||||
|
||||
// AddAndroidError record counts of error Android push notification.
|
||||
func (s *Storage) AddAndroidError(count int64) {
|
||||
redisClient.IncrBy(storage.AndroidErrorKey, count)
|
||||
s.client.IncrBy(storage.AndroidErrorKey, count)
|
||||
}
|
||||
|
||||
// GetTotalCount show counts of all notification.
|
||||
func (s *Storage) GetTotalCount() int64 {
|
||||
var count int64
|
||||
getInt64(storage.TotalCountKey, &count)
|
||||
s.getInt64(storage.TotalCountKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
@@ -97,7 +93,7 @@ func (s *Storage) GetTotalCount() int64 {
|
||||
// GetIosSuccess show success counts of iOS notification.
|
||||
func (s *Storage) GetIosSuccess() int64 {
|
||||
var count int64
|
||||
getInt64(storage.IosSuccessKey, &count)
|
||||
s.getInt64(storage.IosSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
@@ -105,7 +101,7 @@ func (s *Storage) GetIosSuccess() int64 {
|
||||
// GetIosError show error counts of iOS notification.
|
||||
func (s *Storage) GetIosError() int64 {
|
||||
var count int64
|
||||
getInt64(storage.IosErrorKey, &count)
|
||||
s.getInt64(storage.IosErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
@@ -113,7 +109,7 @@ func (s *Storage) GetIosError() int64 {
|
||||
// GetAndroidSuccess show success counts of Android notification.
|
||||
func (s *Storage) GetAndroidSuccess() int64 {
|
||||
var count int64
|
||||
getInt64(storage.AndroidSuccessKey, &count)
|
||||
s.getInt64(storage.AndroidSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
@@ -121,7 +117,7 @@ func (s *Storage) GetAndroidSuccess() int64 {
|
||||
// GetAndroidError show error counts of Android notification.
|
||||
func (s *Storage) GetAndroidError() int64 {
|
||||
var count int64
|
||||
getInt64(storage.AndroidErrorKey, &count)
|
||||
s.getInt64(storage.AndroidErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
@@ -69,4 +69,6 @@ func TestRedisEngine(t *testing.T) {
|
||||
wg.Wait()
|
||||
val = redis.GetTotalCount()
|
||||
assert.Equal(t, int64(10), val)
|
||||
|
||||
assert.NoError(t, redis.Close())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user