- 调整所有Go文件中的import语句顺序,使其符合标准库、第三方库、内部库的分类- 统一import语句的格式,去除多余的空行 - 确保所有文件中的import语句按照字母顺序和逻辑分组排列-修复部分文件中import语句缺失或重复的问题 -优化import语句的可读性和维护性
45 lines
782 B
Go
45 lines
782 B
Go
package limiter
|
|
|
|
import (
|
|
"context"
|
|
"kami/utility/cache"
|
|
"kami/utility/utils"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/os/gmutex"
|
|
)
|
|
|
|
type SimpleKeyLimiter struct {
|
|
mu *gmutex.Mutex
|
|
}
|
|
|
|
func (l *SimpleKeyLimiter) Allow(ctx context.Context, value string, cap int, timeout time.Duration) (ok bool) {
|
|
ok = false
|
|
l.mu.LockFunc(func() {
|
|
num := cache.NewCache().GetPrefixKeyNum(ctx, value)
|
|
if num > cap {
|
|
return
|
|
}
|
|
_ = cache.NewCache().Set(ctx, value+utils.GenerateRandomUUID(), 1, timeout)
|
|
ok = true
|
|
})
|
|
return
|
|
}
|
|
|
|
var (
|
|
simpleKeyLimiterInstance *SimpleKeyLimiter
|
|
)
|
|
|
|
func init() {
|
|
sync.OnceFunc(func() {
|
|
simpleKeyLimiterInstance = &SimpleKeyLimiter{
|
|
mu: &gmutex.Mutex{},
|
|
}
|
|
})
|
|
}
|
|
|
|
func GetSimpleKeyLimiter() *SimpleKeyLimiter {
|
|
return simpleKeyLimiterInstance
|
|
}
|