fix golint issues in core/limit (#494)
This commit is contained in:
@@ -27,9 +27,13 @@ end`
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// Unknown means not initialized state.
|
||||||
Unknown = iota
|
Unknown = iota
|
||||||
|
// Allowed means allowed state.
|
||||||
Allowed
|
Allowed
|
||||||
|
// HitQuota means this request exactly hit the quota.
|
||||||
HitQuota
|
HitQuota
|
||||||
|
// OverQuota means passed the quota.
|
||||||
OverQuota
|
OverQuota
|
||||||
|
|
||||||
internalOverQuota = 0
|
internalOverQuota = 0
|
||||||
@@ -37,11 +41,14 @@ const (
|
|||||||
internalHitQuota = 2
|
internalHitQuota = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrUnknownCode is an error that represents unknown status code.
|
||||||
var ErrUnknownCode = errors.New("unknown status code")
|
var ErrUnknownCode = errors.New("unknown status code")
|
||||||
|
|
||||||
type (
|
type (
|
||||||
LimitOption func(l *PeriodLimit)
|
// PeriodOption defines the method to customize a PeriodLimit.
|
||||||
|
PeriodOption func(l *PeriodLimit)
|
||||||
|
|
||||||
|
// A PeriodLimit is used to limit requests during a period of time.
|
||||||
PeriodLimit struct {
|
PeriodLimit struct {
|
||||||
period int
|
period int
|
||||||
quota int
|
quota int
|
||||||
@@ -51,8 +58,9 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NewPeriodLimit returns a PeriodLimit with given parameters.
|
||||||
func NewPeriodLimit(period, quota int, limitStore *redis.Redis, keyPrefix string,
|
func NewPeriodLimit(period, quota int, limitStore *redis.Redis, keyPrefix string,
|
||||||
opts ...LimitOption) *PeriodLimit {
|
opts ...PeriodOption) *PeriodLimit {
|
||||||
limiter := &PeriodLimit{
|
limiter := &PeriodLimit{
|
||||||
period: period,
|
period: period,
|
||||||
quota: quota,
|
quota: quota,
|
||||||
@@ -67,6 +75,7 @@ func NewPeriodLimit(period, quota int, limitStore *redis.Redis, keyPrefix string
|
|||||||
return limiter
|
return limiter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Take requests a permit, it returns the permit state.
|
||||||
func (h *PeriodLimit) Take(key string) (int, error) {
|
func (h *PeriodLimit) Take(key string) (int, error) {
|
||||||
resp, err := h.limitStore.Eval(periodScript, []string{h.keyPrefix + key}, []string{
|
resp, err := h.limitStore.Eval(periodScript, []string{h.keyPrefix + key}, []string{
|
||||||
strconv.Itoa(h.quota),
|
strconv.Itoa(h.quota),
|
||||||
@@ -102,7 +111,8 @@ func (h *PeriodLimit) calcExpireSeconds() int {
|
|||||||
return h.period
|
return h.period
|
||||||
}
|
}
|
||||||
|
|
||||||
func Align() LimitOption {
|
// Align returns a func to customize a PeriodLimit with alignment.
|
||||||
|
func Align() PeriodOption {
|
||||||
return func(l *PeriodLimit) {
|
return func(l *PeriodLimit) {
|
||||||
l.align = true
|
l.align = true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func TestPeriodLimit_RedisUnavailable(t *testing.T) {
|
|||||||
assert.Equal(t, 0, val)
|
assert.Equal(t, 0, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPeriodLimit(t *testing.T, opts ...LimitOption) {
|
func testPeriodLimit(t *testing.T, opts ...PeriodOption) {
|
||||||
store, clean, err := redistest.CreateRedis()
|
store, clean, err := redistest.CreateRedis()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
defer clean()
|
defer clean()
|
||||||
|
|||||||
Reference in New Issue
Block a user