mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
chore: coding style (#4082)
This commit is contained in:
parent
62c88a84d1
commit
e9dc96af17
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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()),
|
||||||
})
|
})
|
||||||
|
@ -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,
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user