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")
//go:embed setscript.lua
setScript string
scriptSet = redis.NewScript(setScript)
setLuaScript string
setScript = redis.NewScript(setLuaScript)
//go:embed testscript.lua
testScript string
scriptTest = redis.NewScript(testScript)
testLuaScript string
testScript = redis.NewScript(testLuaScript)
)
type (
@ -126,7 +124,7 @@ func (r *redisBitSet) check(ctx context.Context, offsets []uint) (bool, error) {
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) {
return false, nil
} else if err != nil {
@ -158,7 +156,7 @@ func (r *redisBitSet) set(ctx context.Context, offsets []uint) error {
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) {
return nil
}

View File

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

View File

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

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

View File

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

View File

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

View File

@ -22,14 +22,12 @@ const (
var (
//go:embed lockscript.lua
lockScript string
scriptLock = NewScript(lockScript)
lockLuaScript string
lockScript = NewScript(lockLuaScript)
//go:embed delscript.lua
delScript string
scriptDel = NewScript(delScript)
delLuaScript string
delScript = NewScript(delLuaScript)
)
// A RedisLock is a redis lock.
@ -61,7 +59,7 @@ func (rl *RedisLock) Acquire() (bool, error) {
// AcquireCtx acquires the lock with the given ctx.
func (rl *RedisLock) AcquireCtx(ctx context.Context) (bool, error) {
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),
})
if errors.Is(err, red.Nil) {
@ -89,7 +87,7 @@ func (rl *RedisLock) Release() (bool, error) {
// ReleaseCtx releases the lock with the given ctx.
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 {
return false, err
}