Initial cloud-services repo - gateway service + pkg modules

This commit is contained in:
Chris Rai
2026-01-30 23:14:52 -05:00
commit fbb820d7b3
1037 changed files with 171318 additions and 0 deletions

45
pkg/logger/limiter.go Normal file
View File

@@ -0,0 +1,45 @@
package logger
import (
"sync"
"time"
"github.com/ReneKroon/ttlcache/v2"
)
func NewLogLimiter(duration time.Duration, limit int) *LogLimiter {
return &LogLimiter{
duration: duration,
limit: limit,
}
}
type LogLimiter struct {
duration time.Duration
limit int
cache *ttlcache.Cache
onceCache sync.Once
}
func (l *LogLimiter) Check(message string) bool {
_, err := l.logCache().Get(message)
if err == ttlcache.ErrNotFound {
l.logCache().Set(message, true)
return true
}
return false
}
func (l *LogLimiter) logCache() *ttlcache.Cache {
l.onceCache.Do(func() {
if l.cache == nil {
cache := ttlcache.NewCache()
cache.SetTTL(l.duration)
cache.SetCacheSizeLimit(l.limit)
l.cache = cache
}
})
return l.cache
}