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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 24 additions and 30 deletions

View File

@ -19,14 +19,12 @@ var (
ErrTooLargeOffset = errors.New("too large offset") ErrTooLargeOffset = errors.New("too large offset")
//go:embed setscript.lua //go:embed setscript.lua
setScript string setLuaScript string
setScript = redis.NewScript(setLuaScript)
scriptSet = redis.NewScript(setScript)
//go:embed testscript.lua //go:embed testscript.lua
testScript string testLuaScript string
testScript = redis.NewScript(testLuaScript)
scriptTest = redis.NewScript(testScript)
) )
type ( type (
@ -126,7 +124,7 @@ func (r *redisBitSet) check(ctx context.Context, offsets []uint) (bool, error) {
return false, err return false, err
} }
resp, err := r.store.ScriptRunCtx(ctx, scriptTest, []string{r.key}, args) resp, err := r.store.ScriptRunCtx(ctx, testScript, []string{r.key}, args)
if errors.Is(err, redis.Nil) { if errors.Is(err, redis.Nil) {
return false, nil return false, nil
} else if err != nil { } else if err != nil {
@ -158,7 +156,7 @@ func (r *redisBitSet) set(ctx context.Context, offsets []uint) error {
return err return err
} }
_, err = r.store.ScriptRunCtx(ctx, scriptSet, []string{r.key}, args) _, err = r.store.ScriptRunCtx(ctx, setScript, []string{r.key}, args)
if errors.Is(err, redis.Nil) { if errors.Is(err, redis.Nil) {
return nil return nil
} }

View File

@ -1,3 +1,3 @@
for _, offset in ipairs(ARGV) do for _, offset in ipairs(ARGV) do
redis.call("setbit", KEYS[1], offset, 1) redis.call("setbit", KEYS[1], offset, 1)
end end

View File

@ -3,4 +3,4 @@ for _, offset in ipairs(ARGV) do
return false return false
end end
end end
return true return true

View File

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

View File

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

View File

@ -23,9 +23,8 @@ const (
var ( var (
//go:embed tokenscript.lua //go:embed tokenscript.lua
tokenScript string tokenLuaScript string
tokenScript = redis.NewScript(tokenLuaScript)
scriptToken = redis.NewScript(tokenScript)
) )
// A TokenLimiter controls how frequently events are allowed to happen with in one second. // 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, resp, err := lim.store.ScriptRunCtx(ctx,
scriptToken, tokenScript,
[]string{ []string{
lim.tokenKey, lim.tokenKey,
lim.timestampKey, lim.timestampKey,

View File

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

View File

@ -2,4 +2,4 @@ if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("DEL", KEYS[1]) return redis.call("DEL", KEYS[1])
else else
return 0 return 0
end end

View File

@ -3,4 +3,4 @@ if redis.call("GET", KEYS[1]) == ARGV[1] then
return "OK" return "OK"
else else
return redis.call("SET", KEYS[1], ARGV[1], "NX", "PX", ARGV[2]) return redis.call("SET", KEYS[1], ARGV[1], "NX", "PX", ARGV[2])
end end

View File

@ -22,14 +22,12 @@ const (
var ( var (
//go:embed lockscript.lua //go:embed lockscript.lua
lockScript string lockLuaScript string
lockScript = NewScript(lockLuaScript)
scriptLock = NewScript(lockScript)
//go:embed delscript.lua //go:embed delscript.lua
delScript string delLuaScript string
delScript = NewScript(delLuaScript)
scriptDel = NewScript(delScript)
) )
// A RedisLock is a redis lock. // A RedisLock is a redis lock.
@ -61,7 +59,7 @@ func (rl *RedisLock) Acquire() (bool, error) {
// AcquireCtx acquires the lock with the given ctx. // AcquireCtx acquires the lock with the given ctx.
func (rl *RedisLock) AcquireCtx(ctx context.Context) (bool, error) { func (rl *RedisLock) AcquireCtx(ctx context.Context) (bool, error) {
seconds := atomic.LoadUint32(&rl.seconds) seconds := atomic.LoadUint32(&rl.seconds)
resp, err := rl.store.ScriptRunCtx(ctx, scriptLock, []string{rl.key}, []string{ resp, err := rl.store.ScriptRunCtx(ctx, lockScript, []string{rl.key}, []string{
rl.id, strconv.Itoa(int(seconds)*millisPerSecond + tolerance), rl.id, strconv.Itoa(int(seconds)*millisPerSecond + tolerance),
}) })
if errors.Is(err, red.Nil) { if errors.Is(err, red.Nil) {
@ -89,7 +87,7 @@ func (rl *RedisLock) Release() (bool, error) {
// ReleaseCtx releases the lock with the given ctx. // ReleaseCtx releases the lock with the given ctx.
func (rl *RedisLock) ReleaseCtx(ctx context.Context) (bool, error) { func (rl *RedisLock) ReleaseCtx(ctx context.Context) (bool, error) {
resp, err := rl.store.ScriptRunCtx(ctx, scriptDel, []string{rl.key}, []string{rl.id}) resp, err := rl.store.ScriptRunCtx(ctx, delScript, []string{rl.key}, []string{rl.id})
if err != nil { if err != nil {
return false, err return false, err
} }