feat(ratelimit): wire per-identity inbound rate limiting into the gateway
internal/ratelimit was fully built (sliding-window limiter, ticker-driven eviction) but had no consumer, leaving multi-identity deployments with no inbound request budget on any channel (archie-core-6wxb / GH #797). Adds Message.SenderID (Telegram numeric user ID, SMTP from address, or webhook route path -- the closest thing webhook has to a caller identity), threads it into Router.Route/RouteStream via a shared checkRateLimit, and wires an opt-in [chat.rate_limit] config section through bootstrap with the documented EvictStale ticker.
This commit is contained in:
14 files changed
+420
-54
No files matched your search
@@ -166,6 +166,14 @@ class = "ollama"
|
|||||||
# inbound HTTP concerns that happen to share the word "webhook".
|
# inbound HTTP concerns that happen to share the word "webhook".
|
||||||
# webhook_addr = "0.0.0.0:8646" # under [chat], not [chat.telegram]
|
# webhook_addr = "0.0.0.0:8646" # under [chat], not [chat.telegram]
|
||||||
|
|
||||||
|
# Per-(channel, sender) inbound rate limiting, shared by every chat channel:
|
||||||
|
# Telegram keys by numeric user ID, email by SMTP from address, webhook by
|
||||||
|
# its configured route path. Off by default -- both fields must be set to a
|
||||||
|
# positive value.
|
||||||
|
# [chat.rate_limit]
|
||||||
|
# window = "1m"
|
||||||
|
# max_requests = 20
|
||||||
|
|
||||||
# External MCP tools are launched and managed by Archie's MCP client. A
|
# External MCP tools are launched and managed by Archie's MCP client. A
|
||||||
# command of "npx" gets a persistent package cache under work_dir
|
# command of "npx" gets a persistent package cache under work_dir
|
||||||
# (top of this file) automatically, so a daemon restart reuses it instead
|
# (top of this file) automatically, so a daemon restart reuses it instead
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ import (
|
|||||||
"github.com/samcharles93/archie-core/internal/memory"
|
"github.com/samcharles93/archie-core/internal/memory"
|
||||||
"github.com/samcharles93/archie-core/internal/plugin"
|
"github.com/samcharles93/archie-core/internal/plugin"
|
||||||
"github.com/samcharles93/archie-core/internal/plugin/pluginextract"
|
"github.com/samcharles93/archie-core/internal/plugin/pluginextract"
|
||||||
|
"github.com/samcharles93/archie-core/internal/ratelimit"
|
||||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||||
"github.com/samcharles93/archie-core/internal/secret"
|
"github.com/samcharles93/archie-core/internal/secret"
|
||||||
"github.com/samcharles93/archie-core/internal/skill"
|
"github.com/samcharles93/archie-core/internal/skill"
|
||||||
@@ -124,6 +125,10 @@ type boot struct {
|
|||||||
bus *events.Bus
|
bus *events.Bus
|
||||||
restartTelegram func() error
|
restartTelegram func() error
|
||||||
channelManager *status.Manager
|
channelManager *status.Manager
|
||||||
|
// rateLimiter is the shared per-(channel, sender) inbound budget every
|
||||||
|
// chat Router is given. Nil when [chat.rate_limit] is not configured,
|
||||||
|
// which leaves rate limiting off.
|
||||||
|
rateLimiter *ratelimit.Limiter
|
||||||
// cfgHolder is the daemon's one configuration Holder. Boot owns it and
|
// cfgHolder is the daemon's one configuration Holder. Boot owns it and
|
||||||
// the daemon reads through it, so a reload swaps one snapshot and every
|
// the daemon reads through it, so a reload swaps one snapshot and every
|
||||||
// reader sees it: there is no second holder to keep in step.
|
// reader sees it: there is no second holder to keep in step.
|
||||||
@@ -572,6 +577,34 @@ func (b *boot) setupLLMAndChat() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rateLimiterEvictInterval is how often an active Limiter sweeps entries
|
||||||
|
// whose hits have all aged out of the window, per internal/ratelimit's own
|
||||||
|
// documented ticker contract.
|
||||||
|
const rateLimiterEvictInterval = time.Minute
|
||||||
|
|
||||||
|
// startRateLimiter constructs b.rateLimiter from cfg when configured, and
|
||||||
|
// drives its documented EvictStale ticker for the life of ctx. Leaves
|
||||||
|
// b.rateLimiter nil (rate limiting off) when cfg is not enabled.
|
||||||
|
func (b *boot) startRateLimiter(ctx context.Context, cfg config.RateLimitConfig) {
|
||||||
|
if !cfg.Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.rateLimiter = ratelimit.New(cfg.Window, cfg.MaxRequests)
|
||||||
|
limiter := b.rateLimiter
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(rateLimiterEvictInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
limiter.EvictStale()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// setupGateways assembles the Telegram, email and webhook gateways. It
|
// setupGateways assembles the Telegram, email and webhook gateways. It
|
||||||
// returns false when the Telegram gateway could not start, which the
|
// returns false when the Telegram gateway could not start, which the
|
||||||
// caller treats as a fatal boot error.
|
// caller treats as a fatal boot error.
|
||||||
@@ -579,6 +612,7 @@ func (b *boot) setupLLMAndChat() error {
|
|||||||
// Multi-agent collaboration PRD phase C (docs/prds/multi-agent-collaboration.md).
|
// Multi-agent collaboration PRD phase C (docs/prds/multi-agent-collaboration.md).
|
||||||
func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) bool {
|
func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) bool {
|
||||||
cfg, log := b.cfg, b.log
|
cfg, log := b.cfg, b.log
|
||||||
|
b.startRateLimiter(ctx, cfg.Chat.RateLimit)
|
||||||
start, ok := setupTelegramGateway(ctx, telegramSetup{
|
start, ok := setupTelegramGateway(ctx, telegramSetup{
|
||||||
Cfg: config.NewHolder(cfg), CfgPath: cfgPath, OverlayPath: overlayPath,
|
Cfg: config.NewHolder(cfg), CfgPath: cfgPath, OverlayPath: overlayPath,
|
||||||
St: b.stateStore, LLM: b.llm, ChatModels: b.chatModels, ToolReg: b.toolReg,
|
St: b.stateStore, LLM: b.llm, ChatModels: b.chatModels, ToolReg: b.toolReg,
|
||||||
@@ -602,6 +636,7 @@ func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) b
|
|||||||
Bus: b.bus,
|
Bus: b.bus,
|
||||||
RegisterRestart: func(request func() error) { b.restartTelegram = request }, Log: log,
|
RegisterRestart: func(request func() error) { b.restartTelegram = request }, Log: log,
|
||||||
ChannelManager: b.channelManager, AgentStatus: b.agentStatus,
|
ChannelManager: b.channelManager, AgentStatus: b.agentStatus,
|
||||||
|
RateLimiter: b.rateLimiter,
|
||||||
})
|
})
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
@@ -610,58 +645,70 @@ func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) b
|
|||||||
b.startGateways = append(b.startGateways, start)
|
b.startGateways = append(b.startGateways, start)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Email gateway (optional) ───────────────────────────────────
|
b.setupEmailGateway(ctx, cfg, log)
|
||||||
if cfg.Chat.Email.ListenAddr != "" {
|
b.setupWebhookGateway(ctx, cfg, log)
|
||||||
em := email.New(cfg.Chat.Email.ListenAddr, cfg.Chat.Email.RelayAddr, log)
|
|
||||||
emRouter := gateway.NewRouter(b.stateStore, nil, "email")
|
|
||||||
configureTaskCommands(emRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity)
|
|
||||||
b.startGateways = append(b.startGateways, func() {
|
|
||||||
go func() {
|
|
||||||
lifecycle := gateway.Lifecycle{
|
|
||||||
Starting: func() { b.channelManager.MarkStarting("email") },
|
|
||||||
Running: func() {
|
|
||||||
b.channelManager.MarkRunning("email")
|
|
||||||
log.Info("email gateway started", "addr", cfg.Chat.Email.ListenAddr)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if err := em.Start(ctx, emRouter, lifecycle); err != nil && ctx.Err() == nil {
|
|
||||||
b.channelManager.MarkFailed("email", err.Error())
|
|
||||||
log.Error("email gateway stopped", "err", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Webhook gateway (optional) ─────────────────────────────────
|
|
||||||
// Enabled when chat.webhook is set to a host:port listen address.
|
|
||||||
if cfg.Chat.WebhookAddr != "" {
|
|
||||||
host, port := parseListenAddr(cfg.Chat.WebhookAddr, "0.0.0.0", 8644)
|
|
||||||
wh := webhook.New(
|
|
||||||
host, port,
|
|
||||||
[]webhook.RouteConfig{{Path: "/webhook"}},
|
|
||||||
log,
|
|
||||||
)
|
|
||||||
whRouter := gateway.NewRouter(b.stateStore, nil, "webhook")
|
|
||||||
configureTaskCommands(whRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity)
|
|
||||||
b.startGateways = append(b.startGateways, func() {
|
|
||||||
go func() {
|
|
||||||
lifecycle := gateway.Lifecycle{
|
|
||||||
Starting: func() { b.channelManager.MarkStarting("webhook") },
|
|
||||||
Running: func() {
|
|
||||||
b.channelManager.MarkRunning("webhook")
|
|
||||||
log.Info("webhook gateway started", "addr", fmt.Sprintf("%s:%d", host, port))
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if err := wh.Start(ctx, whRouter, lifecycle); err != nil && ctx.Err() == nil {
|
|
||||||
b.channelManager.MarkFailed("webhook", err.Error())
|
|
||||||
log.Error("webhook gateway stopped", "err", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setupEmailGateway registers the optional inbound email gateway when
|
||||||
|
// chat.email.listen_addr is configured.
|
||||||
|
func (b *boot) setupEmailGateway(ctx context.Context, cfg config.Config, log *slog.Logger) {
|
||||||
|
if cfg.Chat.Email.ListenAddr == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
em := email.New(cfg.Chat.Email.ListenAddr, cfg.Chat.Email.RelayAddr, log)
|
||||||
|
emRouter := gateway.NewRouter(b.stateStore, nil, "email")
|
||||||
|
emRouter.Limiter = b.rateLimiter
|
||||||
|
configureTaskCommands(emRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity)
|
||||||
|
b.startGateways = append(b.startGateways, func() {
|
||||||
|
go func() {
|
||||||
|
lifecycle := gateway.Lifecycle{
|
||||||
|
Starting: func() { b.channelManager.MarkStarting("email") },
|
||||||
|
Running: func() {
|
||||||
|
b.channelManager.MarkRunning("email")
|
||||||
|
log.Info("email gateway started", "addr", cfg.Chat.Email.ListenAddr)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if err := em.Start(ctx, emRouter, lifecycle); err != nil && ctx.Err() == nil {
|
||||||
|
b.channelManager.MarkFailed("email", err.Error())
|
||||||
|
log.Error("email gateway stopped", "err", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// setupWebhookGateway registers the optional inbound webhook gateway when
|
||||||
|
// chat.webhook_addr is configured.
|
||||||
|
func (b *boot) setupWebhookGateway(ctx context.Context, cfg config.Config, log *slog.Logger) {
|
||||||
|
if cfg.Chat.WebhookAddr == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
host, port := parseListenAddr(cfg.Chat.WebhookAddr, "0.0.0.0", 8644)
|
||||||
|
wh := webhook.New(
|
||||||
|
host, port,
|
||||||
|
[]webhook.RouteConfig{{Path: "/webhook"}},
|
||||||
|
log,
|
||||||
|
)
|
||||||
|
whRouter := gateway.NewRouter(b.stateStore, nil, "webhook")
|
||||||
|
whRouter.Limiter = b.rateLimiter
|
||||||
|
configureTaskCommands(whRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity)
|
||||||
|
b.startGateways = append(b.startGateways, func() {
|
||||||
|
go func() {
|
||||||
|
lifecycle := gateway.Lifecycle{
|
||||||
|
Starting: func() { b.channelManager.MarkStarting("webhook") },
|
||||||
|
Running: func() {
|
||||||
|
b.channelManager.MarkRunning("webhook")
|
||||||
|
log.Info("webhook gateway started", "addr", fmt.Sprintf("%s:%d", host, port))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if err := wh.Start(ctx, whRouter, lifecycle); err != nil && ctx.Err() == nil {
|
||||||
|
b.channelManager.MarkFailed("webhook", err.Error())
|
||||||
|
log.Error("webhook gateway stopped", "err", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// loadWorkflows builds the workflow registry from the skill catalog.
|
// loadWorkflows builds the workflow registry from the skill catalog.
|
||||||
// Plugin-defined workflows override built-ins of the same name;
|
// Plugin-defined workflows override built-ins of the same name;
|
||||||
// built-ins fill gaps.
|
// built-ins fill gaps.
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/samcharles93/archie-core/internal/gateway"
|
"github.com/samcharles93/archie-core/internal/gateway"
|
||||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||||
"github.com/samcharles93/archie-core/internal/installtype"
|
"github.com/samcharles93/archie-core/internal/installtype"
|
||||||
|
"github.com/samcharles93/archie-core/internal/ratelimit"
|
||||||
"github.com/samcharles93/archie-core/internal/releaseannounce"
|
"github.com/samcharles93/archie-core/internal/releaseannounce"
|
||||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||||
"github.com/samcharles93/archie-core/internal/secret"
|
"github.com/samcharles93/archie-core/internal/secret"
|
||||||
@@ -65,6 +66,9 @@ type telegramSetup struct {
|
|||||||
// Nil disables the agent component of RunningVersions.
|
// Nil disables the agent component of RunningVersions.
|
||||||
AgentStatus *daemon.AgentStatus
|
AgentStatus *daemon.AgentStatus
|
||||||
Secrets *secret.Registry
|
Secrets *secret.Registry
|
||||||
|
// RateLimiter budgets inbound messages per sender when set. Nil
|
||||||
|
// leaves rate limiting off.
|
||||||
|
RateLimiter *ratelimit.Limiter
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveTelegramToken(cfg config.TelegramConfig, registry *secret.Registry) (string, error) {
|
func resolveTelegramToken(cfg config.TelegramConfig, registry *secret.Registry) (string, error) {
|
||||||
@@ -283,6 +287,7 @@ func buildTelegramRouter(ctx context.Context, tg *telegram.Gateway, s telegramSe
|
|||||||
// a closure -- but this way the dependency is visible rather than
|
// a closure -- but this way the dependency is visible rather than
|
||||||
// captured by reference.
|
// captured by reference.
|
||||||
router := gateway.NewRouter(s.St, nil, "telegram")
|
router := gateway.NewRouter(s.St, nil, "telegram")
|
||||||
|
router.Limiter = s.RateLimiter
|
||||||
router.Models = s.ChatModels
|
router.Models = s.ChatModels
|
||||||
router.Updates = s.Updates
|
router.Updates = s.Updates
|
||||||
router.Personas = s.Personas
|
router.Personas = s.Personas
|
||||||
|
|||||||
@@ -205,6 +205,7 @@ func (g *Gateway) processMessage(ctx context.Context, from, to, raw string) {
|
|||||||
msg := gateway.Inbound{Message: messaging.Message{
|
msg := gateway.Inbound{Message: messaging.Message{
|
||||||
ConversationID: messaging.ConversationID{ChannelID: to},
|
ConversationID: messaging.ConversationID{ChannelID: to},
|
||||||
Sender: from,
|
Sender: from,
|
||||||
|
SenderID: from,
|
||||||
Role: messaging.RoleUser,
|
Role: messaging.RoleUser,
|
||||||
Text: text,
|
Text: text,
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -69,6 +69,9 @@ func TestSMTPReceiveAndRoute(t *testing.T) {
|
|||||||
if in.Message.Sender != "sender@test.com" {
|
if in.Message.Sender != "sender@test.com" {
|
||||||
return "", fmt.Errorf("unexpected sender: %s", in.Message.Sender)
|
return "", fmt.Errorf("unexpected sender: %s", in.Message.Sender)
|
||||||
}
|
}
|
||||||
|
if in.Message.SenderID != "sender@test.com" {
|
||||||
|
return "", fmt.Errorf("unexpected SenderID: %s", in.Message.SenderID)
|
||||||
|
}
|
||||||
return "got it", nil
|
return "got it", nil
|
||||||
}, "email")
|
}, "email")
|
||||||
|
|
||||||
|
|||||||
@@ -616,6 +616,7 @@ func (g *Gateway) submitTurn(ctx context.Context, b *bot.Bot, msg *models.Messag
|
|||||||
SourceID: fmt.Sprintf("%d", msg.ID),
|
SourceID: fmt.Sprintf("%d", msg.ID),
|
||||||
ConversationID: conversationID(msg),
|
ConversationID: conversationID(msg),
|
||||||
Sender: msg.From.Username,
|
Sender: msg.From.Username,
|
||||||
|
SenderID: fmt.Sprintf("%d", msg.From.ID),
|
||||||
Role: messaging.RoleUser,
|
Role: messaging.RoleUser,
|
||||||
Text: msg.Text,
|
Text: msg.Text,
|
||||||
At: time.Unix(int64(msg.Date), 0).UTC(),
|
At: time.Unix(int64(msg.Date), 0).UTC(),
|
||||||
|
|||||||
@@ -173,8 +173,12 @@ func (g *Gateway) handleWebhook(route *RouteConfig) http.HandlerFunc {
|
|||||||
msg := gateway.Inbound{Message: messaging.Message{
|
msg := gateway.Inbound{Message: messaging.Message{
|
||||||
ConversationID: messaging.ConversationID{ChannelID: route.Path},
|
ConversationID: messaging.ConversationID{ChannelID: route.Path},
|
||||||
Sender: "webhook",
|
Sender: "webhook",
|
||||||
Role: messaging.RoleUser,
|
// Webhooks have no per-caller identity; the configured route
|
||||||
Text: text,
|
// is the closest thing to one, since it corresponds to a
|
||||||
|
// single external source.
|
||||||
|
SenderID: route.Path,
|
||||||
|
Role: messaging.RoleUser,
|
||||||
|
Text: text,
|
||||||
}}
|
}}
|
||||||
reply, err := router.Route(r.Context(), msg)
|
reply, err := router.Route(r.Context(), msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -80,7 +80,11 @@ func TestWebhookHandlerNotStarted(t *testing.T) {
|
|||||||
func TestWebhookHandlerRoute(t *testing.T) {
|
func TestWebhookHandlerRoute(t *testing.T) {
|
||||||
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn}))
|
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn}))
|
||||||
g := New("", 0, []RouteConfig{{Path: "/hook"}}, log)
|
g := New("", 0, []RouteConfig{{Path: "/hook"}}, log)
|
||||||
g.router = gateway.NewRouter(nil, nil, "webhook")
|
var gotSenderID string
|
||||||
|
g.router = gateway.NewRouter(nil, func(ctx context.Context, in gateway.Inbound) (string, error) {
|
||||||
|
gotSenderID = in.Message.SenderID
|
||||||
|
return "ok", nil
|
||||||
|
}, "webhook")
|
||||||
|
|
||||||
handler := g.WebhookHandler()
|
handler := g.WebhookHandler()
|
||||||
payload := `{"text":"hello from webhook"}`
|
payload := `{"text":"hello from webhook"}`
|
||||||
@@ -89,6 +93,11 @@ func TestWebhookHandlerRoute(t *testing.T) {
|
|||||||
if rec.Code != http.StatusAccepted {
|
if rec.Code != http.StatusAccepted {
|
||||||
t.Errorf("status = %d, want 202", rec.Code)
|
t.Errorf("status = %d, want 202", rec.Code)
|
||||||
}
|
}
|
||||||
|
// Webhook has no native per-caller identity, so the configured route
|
||||||
|
// path is used to key rate limiting instead (see internal/ratelimit).
|
||||||
|
if gotSenderID != "/hook" {
|
||||||
|
t.Errorf("SenderID = %q, want the route path", gotSenderID)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWebhookHandlerInvalidSignature(t *testing.T) {
|
func TestWebhookHandlerInvalidSignature(t *testing.T) {
|
||||||
|
|||||||
@@ -910,6 +910,30 @@ type ChatConfig struct {
|
|||||||
// Empty disables the webhook gateway.
|
// Empty disables the webhook gateway.
|
||||||
WebhookAddr string `toml:"webhook_addr" yaml:"webhook_addr"`
|
WebhookAddr string `toml:"webhook_addr" yaml:"webhook_addr"`
|
||||||
Telegram TelegramConfig `toml:"telegram" yaml:"telegram"`
|
Telegram TelegramConfig `toml:"telegram" yaml:"telegram"`
|
||||||
|
// RateLimit budgets inbound messages per (channel, sender) across
|
||||||
|
// every chat channel. Zero MaxRequests (the default) leaves rate
|
||||||
|
// limiting off entirely -- it is an opt-in control, not a default
|
||||||
|
// throttle that could surprise an existing deployment.
|
||||||
|
RateLimit RateLimitConfig `toml:"rate_limit" yaml:"rate_limit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RateLimitConfig configures the sliding-window inbound rate limiter
|
||||||
|
// (internal/ratelimit) shared by every chat channel. A sender is
|
||||||
|
// identified per channel: Telegram by numeric user ID, email by the SMTP
|
||||||
|
// from address, webhook by the configured route path. Disabled unless
|
||||||
|
// both fields are set to a positive value.
|
||||||
|
type RateLimitConfig struct {
|
||||||
|
// Window is the rolling interval MaxRequests is budgeted over.
|
||||||
|
Window time.Duration `toml:"window" yaml:"window"`
|
||||||
|
// MaxRequests is the most inbound messages one sender may send in
|
||||||
|
// Window. Zero (the default) disables rate limiting.
|
||||||
|
MaxRequests int `toml:"max_requests" yaml:"max_requests"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enabled reports whether RateLimit is configured to actually limit
|
||||||
|
// anything.
|
||||||
|
func (c RateLimitConfig) Enabled() bool {
|
||||||
|
return c.Window > 0 && c.MaxRequests > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// TelegramConfig configures the Telegram Bot API channel.
|
// TelegramConfig configures the Telegram Bot API channel.
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestRateLimitConfigEnabled pins Enabled as opt-in: the zero value (an
|
||||||
|
// absent [chat.rate_limit] table) must leave rate limiting off, and both
|
||||||
|
// fields must be positive before it counts as configured.
|
||||||
|
func TestRateLimitConfigEnabled(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
cfg RateLimitConfig
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"zero value", RateLimitConfig{}, false},
|
||||||
|
{"window only", RateLimitConfig{Window: time.Minute}, false},
|
||||||
|
{"max requests only", RateLimitConfig{MaxRequests: 20}, false},
|
||||||
|
{"both set", RateLimitConfig{Window: time.Minute, MaxRequests: 20}, true},
|
||||||
|
{"negative max requests", RateLimitConfig{Window: time.Minute, MaxRequests: -1}, false},
|
||||||
|
}
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
if got := tc.cfg.Enabled(); got != tc.want {
|
||||||
|
t.Errorf("Enabled() = %v, want %v", got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -80,6 +80,14 @@ type Message struct {
|
|||||||
// so history search and transcript rendering keep working.
|
// so history search and transcript rendering keep working.
|
||||||
Sender string
|
Sender string
|
||||||
|
|
||||||
|
// SenderID is the channel-native stable identifier for the party that
|
||||||
|
// sent this message (e.g. a Telegram numeric user ID, an SMTP from
|
||||||
|
// address, or a webhook route path), distinct from Sender's
|
||||||
|
// human-readable display form. Empty when the channel has no stable
|
||||||
|
// per-sender identity to offer. Used to key per-identity inbound
|
||||||
|
// controls such as rate limiting.
|
||||||
|
SenderID string
|
||||||
|
|
||||||
Role Role
|
Role Role
|
||||||
Text string
|
Text string
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||||
|
"github.com/samcharles93/archie-core/internal/ratelimit"
|
||||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||||
)
|
)
|
||||||
@@ -231,7 +232,12 @@ type Router struct {
|
|||||||
// Log is the optional logger for the best-effort background title
|
// Log is the optional logger for the best-effort background title
|
||||||
// path. Nil drops those diagnostics silently; nothing on this path is
|
// path. Nil drops those diagnostics silently; nothing on this path is
|
||||||
// important enough to fail the turn over.
|
// important enough to fail the turn over.
|
||||||
Log *slog.Logger
|
Log *slog.Logger
|
||||||
|
// Limiter enforces a per-(gateway, sender) inbound budget when set.
|
||||||
|
// Nil disables rate limiting entirely. Messages with no SenderID
|
||||||
|
// (channels that cannot supply a stable per-sender identity) are
|
||||||
|
// never limited, since there is no key to charge them against.
|
||||||
|
Limiter *ratelimit.Limiter
|
||||||
sessionTracker *sessionTracker
|
sessionTracker *sessionTracker
|
||||||
gatewayName string
|
gatewayName string
|
||||||
// titlingMu guards titling, the set of sessions with a title proposal
|
// titlingMu guards titling, the set of sessions with a title proposal
|
||||||
@@ -260,9 +266,34 @@ func (r *Router) SessionTracker() *sessionTracker {
|
|||||||
return r.sessionTracker
|
return r.sessionTracker
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rateLimitReply is returned to a sender who has exceeded their inbound
|
||||||
|
// budget, in place of normal dispatch.
|
||||||
|
const rateLimitReply = "You're sending messages too quickly. Please wait a moment and try again."
|
||||||
|
|
||||||
|
// checkRateLimit reports whether msg is over its sender's inbound budget.
|
||||||
|
// A nil Limiter (rate limiting not configured) or an empty SenderID
|
||||||
|
// (the channel has no stable per-sender identity to charge) always
|
||||||
|
// allows.
|
||||||
|
func (r *Router) checkRateLimit(msg messaging.Message) (blocked bool) {
|
||||||
|
if r.Limiter == nil || msg.SenderID == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return !r.Limiter.Allow(r.gatewayName, msg.SenderID)
|
||||||
|
}
|
||||||
|
|
||||||
// Route dispatches msg and returns the reply. Gateway-local commands
|
// Route dispatches msg and returns the reply. Gateway-local commands
|
||||||
// are handled directly; everything else goes to the LLM responder.
|
// are handled directly; everything else goes to the LLM responder.
|
||||||
func (r *Router) Route(ctx context.Context, in Inbound) (string, error) {
|
func (r *Router) Route(ctx context.Context, in Inbound) (string, error) {
|
||||||
|
if r.checkRateLimit(in.Message) {
|
||||||
|
return rateLimitReply, nil
|
||||||
|
}
|
||||||
|
return r.route(ctx, in)
|
||||||
|
}
|
||||||
|
|
||||||
|
// route is Route's continuation once the caller has already cleared the
|
||||||
|
// rate-limit check (or is deliberately skipping it, as RouteStream's
|
||||||
|
// local-command fallthrough does to avoid charging one message twice).
|
||||||
|
func (r *Router) route(ctx context.Context, in Inbound) (string, error) {
|
||||||
text := strings.TrimSpace(in.Message.Text)
|
text := strings.TrimSpace(in.Message.Text)
|
||||||
cmd, _ := parseCmd(text, r.gatewayName)
|
cmd, _ := parseCmd(text, r.gatewayName)
|
||||||
|
|
||||||
@@ -449,9 +480,12 @@ func (r *Router) RouteStream(ctx context.Context, in Inbound, stream TurnStream)
|
|||||||
if r.LLMStream == nil || stream == nil {
|
if r.LLMStream == nil || stream == nil {
|
||||||
return r.Route(ctx, in)
|
return r.Route(ctx, in)
|
||||||
}
|
}
|
||||||
|
if r.checkRateLimit(in.Message) {
|
||||||
|
return rateLimitReply, nil
|
||||||
|
}
|
||||||
cmd, _ := parseCmd(strings.TrimSpace(in.Message.Text), r.gatewayName)
|
cmd, _ := parseCmd(strings.TrimSpace(in.Message.Text), r.gatewayName)
|
||||||
if isLocalCommand(cmd) || strings.HasPrefix(cmd, "/") {
|
if isLocalCommand(cmd) || strings.HasPrefix(cmd, "/") {
|
||||||
return r.Route(ctx, in)
|
return r.route(ctx, in)
|
||||||
}
|
}
|
||||||
reply, err := r.LLMStream(ctx, in, stream)
|
reply, err := r.LLMStream(ctx, in, stream)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
package gateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/samcharles93/archie-core/internal/ratelimit"
|
||||||
|
)
|
||||||
|
|
||||||
|
// senderInbound is inbound with SenderID set, the field the rate limiter
|
||||||
|
// keys on.
|
||||||
|
func senderInbound(senderID, text string) Inbound {
|
||||||
|
in := inbound("chat-1", text)
|
||||||
|
in.Message.SenderID = senderID
|
||||||
|
return in
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRouteRateLimitBlocksOverBudget(t *testing.T) {
|
||||||
|
r := NewRouter(nil, fakeLLM, "test")
|
||||||
|
r.Limiter = ratelimit.New(time.Minute, 1)
|
||||||
|
|
||||||
|
reply, err := r.Route(context.Background(), senderInbound("u1", "hello"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Route: %v", err)
|
||||||
|
}
|
||||||
|
if reply != "llm: hello" {
|
||||||
|
t.Fatalf("first message reply = %q, want it to reach the LLM", reply)
|
||||||
|
}
|
||||||
|
|
||||||
|
reply, err = r.Route(context.Background(), senderInbound("u1", "hello again"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Route: %v", err)
|
||||||
|
}
|
||||||
|
if reply != rateLimitReply {
|
||||||
|
t.Fatalf("second message reply = %q, want the rate-limit reply", reply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRouteRateLimitIsPerSender(t *testing.T) {
|
||||||
|
r := NewRouter(nil, fakeLLM, "test")
|
||||||
|
r.Limiter = ratelimit.New(time.Minute, 1)
|
||||||
|
|
||||||
|
if _, err := r.Route(context.Background(), senderInbound("u1", "hi")); err != nil {
|
||||||
|
t.Fatalf("Route: %v", err)
|
||||||
|
}
|
||||||
|
reply, err := r.Route(context.Background(), senderInbound("u2", "hi"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Route: %v", err)
|
||||||
|
}
|
||||||
|
if reply != "llm: hi" {
|
||||||
|
t.Fatalf("a different sender's message reply = %q, want it unaffected by u1's budget", reply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRouteNoLimiterConfiguredNeverBlocks(t *testing.T) {
|
||||||
|
r := NewRouter(nil, fakeLLM, "test")
|
||||||
|
|
||||||
|
for i := range 5 {
|
||||||
|
reply, err := r.Route(context.Background(), senderInbound("u1", "hello"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Route: %v", err)
|
||||||
|
}
|
||||||
|
if reply != "llm: hello" {
|
||||||
|
t.Fatalf("call %d reply = %q, want it to reach the LLM with no limiter configured", i, reply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestRouteEmptySenderIDNeverLimited pins the deliberate fail-open for
|
||||||
|
// channels (webhook routes with no configured path identity, e.g.) that
|
||||||
|
// have no stable per-sender identity to charge: there is no shared key to
|
||||||
|
// throttle everyone under, so they are left unlimited rather than blocked.
|
||||||
|
func TestRouteEmptySenderIDNeverLimited(t *testing.T) {
|
||||||
|
r := NewRouter(nil, fakeLLM, "test")
|
||||||
|
r.Limiter = ratelimit.New(time.Minute, 1)
|
||||||
|
|
||||||
|
for i := range 3 {
|
||||||
|
reply, err := r.Route(context.Background(), inbound("chat-1", "hello"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Route: %v", err)
|
||||||
|
}
|
||||||
|
if reply != "llm: hello" {
|
||||||
|
t.Fatalf("call %d reply = %q, want it to reach the LLM with no SenderID", i, reply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestRouteStreamLocalCommandChargesExactlyOnce pins RouteStream's local
|
||||||
|
// command fallthrough (into route, not Route) against double-charging the
|
||||||
|
// budget: a status query must consume the same one unit whether it goes
|
||||||
|
// through Route or RouteStream.
|
||||||
|
func TestRouteStreamLocalCommandChargesExactlyOnce(t *testing.T) {
|
||||||
|
r := NewRouter(&fakeStore{counts: map[string]int{}}, nil, "test")
|
||||||
|
r.Limiter = ratelimit.New(time.Minute, 2)
|
||||||
|
r.LLMStream = func(context.Context, Inbound, TurnStream) (string, error) {
|
||||||
|
return "streamed", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := r.RouteStream(context.Background(), senderInbound("u1", "/status"), DeltaFunc(func(string) {})); err != nil {
|
||||||
|
t.Fatalf("RouteStream: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Budget was 2; the local command must have consumed exactly one, so
|
||||||
|
// exactly one more free-text message should still get through.
|
||||||
|
reply, err := r.RouteStream(context.Background(), senderInbound("u1", "hello"), DeltaFunc(func(string) {}))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("RouteStream: %v", err)
|
||||||
|
}
|
||||||
|
if reply != "streamed" {
|
||||||
|
t.Fatalf("second call reply = %q, want it to have budget left", reply)
|
||||||
|
}
|
||||||
|
|
||||||
|
reply, err = r.RouteStream(context.Background(), senderInbound("u1", "hello again"), DeltaFunc(func(string) {}))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("RouteStream: %v", err)
|
||||||
|
}
|
||||||
|
if reply != rateLimitReply {
|
||||||
|
t.Fatalf("third call reply = %q, want the budget exhausted after 2 charged calls", reply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRouteStreamRateLimitBlocksStreamedReply(t *testing.T) {
|
||||||
|
r := NewRouter(nil, nil, "test")
|
||||||
|
r.Limiter = ratelimit.New(time.Minute, 1)
|
||||||
|
streamCalls := 0
|
||||||
|
r.LLMStream = func(context.Context, Inbound, TurnStream) (string, error) {
|
||||||
|
streamCalls++
|
||||||
|
return "streamed", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := r.RouteStream(context.Background(), senderInbound("u1", "hello"), DeltaFunc(func(string) {})); err != nil {
|
||||||
|
t.Fatalf("RouteStream: %v", err)
|
||||||
|
}
|
||||||
|
reply, err := r.RouteStream(context.Background(), senderInbound("u1", "hello again"), DeltaFunc(func(string) {}))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("RouteStream: %v", err)
|
||||||
|
}
|
||||||
|
if reply != rateLimitReply {
|
||||||
|
t.Fatalf("reply = %q, want the rate-limit reply", reply)
|
||||||
|
}
|
||||||
|
if streamCalls != 1 {
|
||||||
|
t.Fatalf("LLMStream called %d times, want exactly 1 (the blocked call must not reach it)", streamCalls)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -379,3 +379,50 @@ func TestEvictStaleConcurrentWithAllow(t *testing.T) {
|
|||||||
t.Errorf("hits map has %d entries, want 1 (only the fresh key); stale entries were not evicted", len(l.hits))
|
t.Errorf("hits map has %d entries, want 1 (only the fresh key); stale entries were not evicted", len(l.hits))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestEvictStaleDrivenByTicker exercises the ticker-driven usage the
|
||||||
|
// package doc mandates ("a long-lived limiter should have EvictStale
|
||||||
|
// driven on a ticker") end to end with a real time.Ticker, rather than
|
||||||
|
// every other test's direct call against a frozen fake clock. It is the
|
||||||
|
// only test in the suite that actually starts a ticker goroutine.
|
||||||
|
func TestEvictStaleDrivenByTicker(t *testing.T) {
|
||||||
|
const window = 15 * time.Millisecond
|
||||||
|
l := New(window, 1)
|
||||||
|
|
||||||
|
l.Allow("telegram", "u1")
|
||||||
|
if len(l.hits) != 1 {
|
||||||
|
t.Fatalf("hits map has %d entries after Allow, want 1", len(l.hits))
|
||||||
|
}
|
||||||
|
|
||||||
|
stop := make(chan struct{})
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
ticker := time.NewTicker(5 * time.Millisecond)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stop:
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
l.EvictStale()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
t.Cleanup(func() {
|
||||||
|
close(stop)
|
||||||
|
<-done
|
||||||
|
})
|
||||||
|
|
||||||
|
deadline := time.Now().Add(2 * time.Second)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
l.mu.Lock()
|
||||||
|
n := len(l.hits)
|
||||||
|
l.mu.Unlock()
|
||||||
|
if n == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(5 * time.Millisecond)
|
||||||
|
}
|
||||||
|
t.Fatal("ticker-driven EvictStale never emptied the hits map for a stale entry")
|
||||||
|
}
|
||||||
Reference in new issue
Block a user