chore: coding style (#4082)

This commit is contained in:
Kevin Wan
2024-04-17 23:37:35 +08:00
committed by GitHub
parent 62c88a84d1
commit e9dc96af17
10 changed files with 24 additions and 30 deletions

View File

@@ -30,9 +30,8 @@ var (
ErrUnknownCode = errors.New("unknown status code")
//go:embed periodscript.lua
periodScript string
scriptPeriod = redis.NewScript(periodScript)
periodLuaScript string
periodScript = redis.NewScript(periodLuaScript)
)
type (
@@ -73,7 +72,7 @@ func (h *PeriodLimit) Take(key string) (int, error) {
// TakeCtx requests a permit with context, it returns the permit state.
func (h *PeriodLimit) TakeCtx(ctx context.Context, key string) (int, error) {
resp, err := h.limitStore.ScriptRunCtx(ctx, scriptPeriod, []string{h.keyPrefix + key}, []string{
resp, err := h.limitStore.ScriptRunCtx(ctx, periodScript, []string{h.keyPrefix + key}, []string{
strconv.Itoa(h.quota),
strconv.Itoa(h.calcExpireSeconds()),
})

View File

@@ -11,4 +11,4 @@ elseif current == limit then
return 2
else
return 0
end
end

View File

@@ -23,9 +23,8 @@ const (
var (
//go:embed tokenscript.lua
tokenScript string
scriptToken = redis.NewScript(tokenScript)
tokenLuaScript string
tokenScript = redis.NewScript(tokenLuaScript)
)
// A TokenLimiter controls how frequently events are allowed to happen with in one second.
@@ -88,7 +87,7 @@ func (lim *TokenLimiter) reserveN(ctx context.Context, now time.Time, n int) boo
}
resp, err := lim.store.ScriptRunCtx(ctx,
scriptToken,
tokenScript,
[]string{
lim.tokenKey,
lim.timestampKey,

View File

@@ -28,4 +28,4 @@ end
redis.call("setex", KEYS[1], ttl, new_tokens)
redis.call("setex", KEYS[2], ttl, now)
return allowed
return allowed