mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
periodlimit new function TakeWithContext (#1983)
Co-authored-by: mashouyue's m1max <mashouyue@toowow.cn>
This commit is contained in:
parent
2a900e1795
commit
392a390a3f
@ -1,6 +1,7 @@
|
||||
package limit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
@ -74,7 +75,12 @@ func NewPeriodLimit(period, quota int, limitStore *redis.Redis, keyPrefix string
|
||||
|
||||
// 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{
|
||||
return h.TakeWithContext(context.Background(), key)
|
||||
}
|
||||
|
||||
// TakeWithContext requests a permit with context, it returns the permit state.
|
||||
func (h *PeriodLimit) TakeWithContext(ctx context.Context, key string) (int, error) {
|
||||
resp, err := h.limitStore.EvalCtx(ctx, periodScript, []string{h.keyPrefix + key}, []string{
|
||||
strconv.Itoa(h.quota),
|
||||
strconv.Itoa(h.calcExpireSeconds()),
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user