fix golint issues in core/stores (#527)

This commit is contained in:
Kevin Wan
2021-02-28 23:02:49 +08:00
committed by GitHub
parent 490241d639
commit c566b5ff82
35 changed files with 348 additions and 82 deletions

View File

@@ -11,6 +11,7 @@ import (
)
type (
// Cache interface is used to define the cache implementation.
Cache interface {
Del(keys ...string) error
Get(key string, v interface{}) error
@@ -27,7 +28,8 @@ type (
}
)
func New(c ClusterConf, barrier syncx.SharedCalls, st *CacheStat, errNotFound error,
// New returns a Cache.
func New(c ClusterConf, barrier syncx.SharedCalls, st *Stat, errNotFound error,
opts ...Option) Cache {
if len(c) == 0 || TotalWeights(c) <= 0 {
log.Fatal("no cache nodes")

View File

@@ -102,7 +102,7 @@ func TestCache_SetDel(t *testing.T) {
Weight: 100,
},
}
c := New(conf, syncx.NewSharedCalls(), NewCacheStat("mock"), errPlaceholder)
c := New(conf, syncx.NewSharedCalls(), NewStat("mock"), errPlaceholder)
for i := 0; i < total; i++ {
if i%2 == 0 {
assert.Nil(t, c.Set(fmt.Sprintf("key/%d", i), i))
@@ -140,7 +140,7 @@ func TestCache_OneNode(t *testing.T) {
Weight: 100,
},
}
c := New(conf, syncx.NewSharedCalls(), NewCacheStat("mock"), errPlaceholder)
c := New(conf, syncx.NewSharedCalls(), NewStat("mock"), errPlaceholder)
for i := 0; i < total; i++ {
if i%2 == 0 {
assert.Nil(t, c.Set(fmt.Sprintf("key/%d", i), i))

View File

@@ -1,3 +1,4 @@
package cache
// CacheConf is an alias of ClusterConf.
type CacheConf = ClusterConf

View File

@@ -33,7 +33,7 @@ type cacheNode struct {
r *rand.Rand
lock *sync.Mutex
unstableExpiry mathx.Unstable
stat *CacheStat
stat *Stat
errNotFound error
}
@@ -43,7 +43,7 @@ type cacheNode struct {
// st is used to stat the cache.
// errNotFound defines the error that returned on cache not found.
// opts are the options that customize the cacheNode.
func NewNode(rds *redis.Redis, barrier syncx.SharedCalls, st *CacheStat,
func NewNode(rds *redis.Redis, barrier syncx.SharedCalls, st *Stat,
errNotFound error, opts ...Option) Cache {
o := newOptions(opts...)
return cacheNode{

View File

@@ -36,7 +36,7 @@ func TestCacheNode_DelCache(t *testing.T) {
r: rand.New(rand.NewSource(time.Now().UnixNano())),
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewCacheStat("any"),
stat: NewStat("any"),
errNotFound: errTestNotFound,
}
assert.Nil(t, cn.Del())
@@ -59,7 +59,7 @@ func TestCacheNode_InvalidCache(t *testing.T) {
r: rand.New(rand.NewSource(time.Now().UnixNano())),
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewCacheStat("any"),
stat: NewStat("any"),
errNotFound: errTestNotFound,
}
s.Set("any", "value")
@@ -81,7 +81,7 @@ func TestCacheNode_Take(t *testing.T) {
barrier: syncx.NewSharedCalls(),
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewCacheStat("any"),
stat: NewStat("any"),
errNotFound: errTestNotFound,
}
var str string
@@ -108,7 +108,7 @@ func TestCacheNode_TakeNotFound(t *testing.T) {
barrier: syncx.NewSharedCalls(),
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewCacheStat("any"),
stat: NewStat("any"),
errNotFound: errTestNotFound,
}
var str string
@@ -147,7 +147,7 @@ func TestCacheNode_TakeWithExpire(t *testing.T) {
barrier: syncx.NewSharedCalls(),
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewCacheStat("any"),
stat: NewStat("any"),
errNotFound: errors.New("any"),
}
var str string
@@ -174,7 +174,7 @@ func TestCacheNode_String(t *testing.T) {
barrier: syncx.NewSharedCalls(),
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewCacheStat("any"),
stat: NewStat("any"),
errNotFound: errors.New("any"),
}
assert.Equal(t, store.Addr, cn.String())
@@ -191,7 +191,7 @@ func TestCacheValueWithBigInt(t *testing.T) {
barrier: syncx.NewSharedCalls(),
lock: new(sync.Mutex),
unstableExpiry: mathx.NewUnstable(expiryDeviation),
stat: NewCacheStat("any"),
stat: NewStat("any"),
errNotFound: errors.New("any"),
}

View File

@@ -8,11 +8,13 @@ const (
)
type (
// An Options is used to store the cache options.
Options struct {
Expiry time.Duration
NotFoundExpiry time.Duration
}
// Option defines the method to customize an Options.
Option func(o *Options)
)
@@ -32,12 +34,14 @@ func newOptions(opts ...Option) Options {
return o
}
// WithExpiry returns a func to customize a Options with given expiry.
func WithExpiry(expiry time.Duration) Option {
return func(o *Options) {
o.Expiry = expiry
}
}
// WithNotFoundExpiry returns a func to customize a Options with given not found expiry.
func WithNotFoundExpiry(expiry time.Duration) Option {
return func(o *Options) {
o.NotFoundExpiry = expiry

View File

@@ -9,7 +9,8 @@ import (
const statInterval = time.Minute
type CacheStat struct {
// A Stat is used to stat the cache.
type Stat struct {
name string
// export the fields to let the unit tests working,
// reside in internal package, doesn't matter.
@@ -19,8 +20,9 @@ type CacheStat struct {
DbFails uint64
}
func NewCacheStat(name string) *CacheStat {
ret := &CacheStat{
// NewStat returns a Stat.
func NewStat(name string) *Stat {
ret := &Stat{
name: name,
}
go ret.statLoop()
@@ -28,37 +30,41 @@ func NewCacheStat(name string) *CacheStat {
return ret
}
func (cs *CacheStat) IncrementTotal() {
atomic.AddUint64(&cs.Total, 1)
// IncrementTotal increments the total count.
func (s *Stat) IncrementTotal() {
atomic.AddUint64(&s.Total, 1)
}
func (cs *CacheStat) IncrementHit() {
atomic.AddUint64(&cs.Hit, 1)
// IncrementHit increments the hit count.
func (s *Stat) IncrementHit() {
atomic.AddUint64(&s.Hit, 1)
}
func (cs *CacheStat) IncrementMiss() {
atomic.AddUint64(&cs.Miss, 1)
// IncrementMiss increments the miss count.
func (s *Stat) IncrementMiss() {
atomic.AddUint64(&s.Miss, 1)
}
func (cs *CacheStat) IncrementDbFails() {
atomic.AddUint64(&cs.DbFails, 1)
// IncrementDbFails increments the db fail count.
func (s *Stat) IncrementDbFails() {
atomic.AddUint64(&s.DbFails, 1)
}
func (cs *CacheStat) statLoop() {
func (s *Stat) statLoop() {
ticker := time.NewTicker(statInterval)
defer ticker.Stop()
for range ticker.C {
total := atomic.SwapUint64(&cs.Total, 0)
total := atomic.SwapUint64(&s.Total, 0)
if total == 0 {
continue
}
hit := atomic.SwapUint64(&cs.Hit, 0)
hit := atomic.SwapUint64(&s.Hit, 0)
percent := 100 * float32(hit) / float32(total)
miss := atomic.SwapUint64(&cs.Miss, 0)
dbf := atomic.SwapUint64(&cs.DbFails, 0)
miss := atomic.SwapUint64(&s.Miss, 0)
dbf := atomic.SwapUint64(&s.DbFails, 0)
logx.Statf("dbcache(%s) - qpm: %d, hit_ratio: %.1f%%, hit: %d, miss: %d, db_fails: %d",
cs.name, total, percent, hit, miss, dbf)
s.name, total, percent, hit, miss, dbf)
}
}

View File

@@ -39,6 +39,7 @@ func init() {
})
}
// AddCleanTask adds a clean task on given keys.
func AddCleanTask(task func() error, keys ...string) {
timingWheel.SetTimer(stringx.Randn(taskKeyLen), delayTask{
delay: time.Second,

View File

@@ -3,8 +3,10 @@ package cache
import "github.com/tal-tech/go-zero/core/stores/redis"
type (
// A ClusterConf is the config of a redis cluster that used as cache.
ClusterConf []NodeConf
// A NodeConf is the config of a redis node that used as cache.
NodeConf struct {
redis.RedisConf
Weight int `json:",default=100"`

View File

@@ -4,6 +4,7 @@ import "strings"
const keySeparator = ","
// TotalWeights returns the total weights of given nodes.
func TotalWeights(c []NodeConf) int {
var weights int