feat: code cleanup

This commit is contained in:
2026-01-04 12:49:56 +08:00
parent 9f9f62f598
commit ab445203aa
4 changed files with 7 additions and 110 deletions

View File

@@ -9,11 +9,12 @@ package rate_limit
import (
"context"
"sync"
"time"
"git.zhangdeman.cn/zhangdeman/rate_limit/define"
"github.com/go-redis/redis_rate/v10"
redisInstance "github.com/redis/go-redis/v9"
"sync"
"time"
)
var (
@@ -54,97 +55,3 @@ func (r *Redis) AllowN(ctx context.Context, limitCfg *define.LimitConfig, tokenC
func (r *Redis) Reset(ctx context.Context, limitCfg *define.LimitConfig) error {
return r.limiter.Reset(ctx, limitCfg.Key)
}
/*func (r *Redis) Second(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.PerSecond(total), rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Minute(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.PerMinute(total), rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Hour(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.PerHour(total), rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Day(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{
Rate: rate,
Period: 24 * time.Hour,
Burst: total,
}, rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Month(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{
Rate: rate,
Period: 24 * 30 * time.Hour,
Burst: total,
}, rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Year(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{
Rate: rate,
Period: 24 * 30 * 365 * time.Hour,
Burst: total,
}, rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Custom(ctx context.Context, timeInfo int64, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{
Rate: rate,
Period: time.Duration(timeInfo) * time.Second,
Burst: total,
}, rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
*/