Fix Redis stats storage concurrency issues (#478)
This commit is contained in:
parent
cbab088ed4
commit
b58386704c
|
@ -46,47 +46,44 @@ func (s *Storage) Init() error {
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Set initial values
|
||||||
|
s.Reset()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset Client storage.
|
// Reset Client storage.
|
||||||
func (s *Storage) Reset() {
|
func (s *Storage) Reset() {
|
||||||
redisClient.Set(storage.TotalCountKey, strconv.Itoa(0), 0)
|
redisClient.Set(storage.TotalCountKey, int64(0), 0)
|
||||||
redisClient.Set(storage.IosSuccessKey, strconv.Itoa(0), 0)
|
redisClient.Set(storage.IosSuccessKey, int64(0), 0)
|
||||||
redisClient.Set(storage.IosErrorKey, strconv.Itoa(0), 0)
|
redisClient.Set(storage.IosErrorKey, int64(0), 0)
|
||||||
redisClient.Set(storage.AndroidSuccessKey, strconv.Itoa(0), 0)
|
redisClient.Set(storage.AndroidSuccessKey, int64(0), 0)
|
||||||
redisClient.Set(storage.AndroidErrorKey, strconv.Itoa(0), 0)
|
redisClient.Set(storage.AndroidErrorKey, int64(0), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddTotalCount record push notification count.
|
// AddTotalCount record push notification count.
|
||||||
func (s *Storage) AddTotalCount(count int64) {
|
func (s *Storage) AddTotalCount(count int64) {
|
||||||
total := s.GetTotalCount() + count
|
redisClient.IncrBy(storage.TotalCountKey, count)
|
||||||
redisClient.Set(storage.TotalCountKey, strconv.Itoa(int(total)), 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddIosSuccess record counts of success iOS push notification.
|
// AddIosSuccess record counts of success iOS push notification.
|
||||||
func (s *Storage) AddIosSuccess(count int64) {
|
func (s *Storage) AddIosSuccess(count int64) {
|
||||||
total := s.GetIosSuccess() + count
|
redisClient.IncrBy(storage.IosSuccessKey, count)
|
||||||
redisClient.Set(storage.IosSuccessKey, strconv.Itoa(int(total)), 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddIosError record counts of error iOS push notification.
|
// AddIosError record counts of error iOS push notification.
|
||||||
func (s *Storage) AddIosError(count int64) {
|
func (s *Storage) AddIosError(count int64) {
|
||||||
total := s.GetIosError() + count
|
redisClient.IncrBy(storage.IosErrorKey, count)
|
||||||
redisClient.Set(storage.IosErrorKey, strconv.Itoa(int(total)), 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddAndroidSuccess record counts of success Android push notification.
|
// AddAndroidSuccess record counts of success Android push notification.
|
||||||
func (s *Storage) AddAndroidSuccess(count int64) {
|
func (s *Storage) AddAndroidSuccess(count int64) {
|
||||||
total := s.GetAndroidSuccess() + count
|
redisClient.IncrBy(storage.AndroidSuccessKey, count)
|
||||||
redisClient.Set(storage.AndroidSuccessKey, strconv.Itoa(int(total)), 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddAndroidError record counts of error Android push notification.
|
// AddAndroidError record counts of error Android push notification.
|
||||||
func (s *Storage) AddAndroidError(count int64) {
|
func (s *Storage) AddAndroidError(count int64) {
|
||||||
total := s.GetAndroidError() + count
|
redisClient.IncrBy(storage.AndroidErrorKey, count)
|
||||||
redisClient.Set(storage.AndroidErrorKey, strconv.Itoa(int(total)), 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTotalCount show counts of all notification.
|
// GetTotalCount show counts of all notification.
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package redis
|
package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
c "github.com/appleboy/gorush/config"
|
c "github.com/appleboy/gorush/config"
|
||||||
|
@ -55,4 +56,17 @@ func TestRedisEngine(t *testing.T) {
|
||||||
redis.Reset()
|
redis.Reset()
|
||||||
val = redis.GetAndroidError()
|
val = redis.GetAndroidError()
|
||||||
assert.Equal(t, int64(0), val)
|
assert.Equal(t, int64(0), val)
|
||||||
|
|
||||||
|
// test concurrency issues
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
redis.AddTotalCount(1)
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
val = redis.GetTotalCount()
|
||||||
|
assert.Equal(t, int64(10), val)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue