Files
Enterprise-Knowledge-Base/internal/im/ratelimit.go
T
nullkey fff9efb4f5 feat(im): integrate command dispatch, QA queue, rate limiting, and streaming improvements
- Wire slash-command registry into Service and HandleMessage dispatch
- Add bounded QA worker pool (qaQueue) with per-user backpressure
- Add sliding-window rate limiter to prevent message abuse
- Support /stop cancellation for queued and in-flight QA requests
- Fix WeCom group chat replies via appchat API with user fallback
- Fix WeCom longconn nil map panic and stream buffer memory leak
- Add Feishu stream reaper to clean orphaned streams (memory leak fix)
- Show "正在思考..." placeholder during Feishu streaming
- Handle empty-stream fallback when no visible content is produced
- Derive WebSearchEnabled from agent config for IM channels
2026-03-20 15:49:59 +08:00

107 lines
2.5 KiB
Go

package im
import (
"sync"
"time"
)
const (
// rateLimitWindow is the sliding window duration for rate limiting.
rateLimitWindow = 60 * time.Second
// rateLimitMaxRequests is the maximum number of requests allowed per window per key.
rateLimitMaxRequests = 10
// rateLimitCleanupInterval is how often stale entries are purged.
rateLimitCleanupInterval = 1 * time.Minute
)
// rateLimitEntry holds the request timestamps for a single key.
type rateLimitEntry struct {
mu sync.Mutex
timestamps []time.Time
deleted bool // marked true when removed from the map by cleanupLoop
}
// slidingWindowLimiter implements per-key sliding window rate limiting.
type slidingWindowLimiter struct {
window time.Duration
maxRequests int
entries sync.Map // key -> *rateLimitEntry
}
func newSlidingWindowLimiter(window time.Duration, maxRequests int) *slidingWindowLimiter {
return &slidingWindowLimiter{
window: window,
maxRequests: maxRequests,
}
}
// Allow checks if the request for the given key is within the rate limit.
// Returns true if allowed, false if rate limited.
func (l *slidingWindowLimiter) Allow(key string) bool {
now := time.Now()
cutoff := now.Add(-l.window)
for {
val, _ := l.entries.LoadOrStore(key, &rateLimitEntry{})
entry := val.(*rateLimitEntry)
entry.mu.Lock()
// If the entry was concurrently deleted by cleanupLoop, retry with a fresh one.
if entry.deleted {
entry.mu.Unlock()
l.entries.Delete(key) // ensure stale entry is gone
continue
}
// Remove expired timestamps
valid := entry.timestamps[:0]
for _, t := range entry.timestamps {
if t.After(cutoff) {
valid = append(valid, t)
}
}
entry.timestamps = valid
if len(entry.timestamps) >= l.maxRequests {
entry.mu.Unlock()
return false
}
entry.timestamps = append(entry.timestamps, now)
entry.mu.Unlock()
return true
}
}
// cleanupLoop periodically removes stale entries from the limiter.
func (l *slidingWindowLimiter) cleanupLoop(stopCh <-chan struct{}) {
ticker := time.NewTicker(rateLimitCleanupInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
cutoff := time.Now().Add(-l.window)
l.entries.Range(func(key, val interface{}) bool {
entry := val.(*rateLimitEntry)
entry.mu.Lock()
allExpired := true
for _, t := range entry.timestamps {
if t.After(cutoff) {
allExpired = false
break
}
}
if allExpired {
entry.deleted = true
l.entries.Delete(key)
}
entry.mu.Unlock()
return true
})
case <-stopCh:
return
}
}
}