From 84d2b6f8f5732a5cb994c2cfa6529f45576e2bad Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 20 Feb 2021 21:55:54 +0800 Subject: [PATCH] fix golint issues in core/limit (#494) --- core/limit/periodlimit.go | 16 +++++++++++++--- core/limit/periodlimit_test.go | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/core/limit/periodlimit.go b/core/limit/periodlimit.go index 92b4d110..21260c96 100644 --- a/core/limit/periodlimit.go +++ b/core/limit/periodlimit.go @@ -27,9 +27,13 @@ end` ) const ( + // Unknown means not initialized state. Unknown = iota + // Allowed means allowed state. Allowed + // HitQuota means this request exactly hit the quota. HitQuota + // OverQuota means passed the quota. OverQuota internalOverQuota = 0 @@ -37,11 +41,14 @@ const ( internalHitQuota = 2 ) +// ErrUnknownCode is an error that represents unknown status code. var ErrUnknownCode = errors.New("unknown status code") 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 { period 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, - opts ...LimitOption) *PeriodLimit { + opts ...PeriodOption) *PeriodLimit { limiter := &PeriodLimit{ period: period, quota: quota, @@ -67,6 +75,7 @@ func NewPeriodLimit(period, quota int, limitStore *redis.Redis, keyPrefix string return limiter } +// Take requests a permit, it returns the permit state. func (h *PeriodLimit) Take(key string) (int, error) { resp, err := h.limitStore.Eval(periodScript, []string{h.keyPrefix + key}, []string{ strconv.Itoa(h.quota), @@ -102,7 +111,8 @@ func (h *PeriodLimit) calcExpireSeconds() int { return h.period } -func Align() LimitOption { +// Align returns a func to customize a PeriodLimit with alignment. +func Align() PeriodOption { return func(l *PeriodLimit) { l.align = true } diff --git a/core/limit/periodlimit_test.go b/core/limit/periodlimit_test.go index 6709ad7f..ba5c7fa8 100644 --- a/core/limit/periodlimit_test.go +++ b/core/limit/periodlimit_test.go @@ -33,7 +33,7 @@ func TestPeriodLimit_RedisUnavailable(t *testing.T) { assert.Equal(t, 0, val) } -func testPeriodLimit(t *testing.T, opts ...LimitOption) { +func testPeriodLimit(t *testing.T, opts ...PeriodOption) { store, clean, err := redistest.CreateRedis() assert.Nil(t, err) defer clean()