feat(middleware): add per-client FIFO request queue with burst metrics

ClientQueue limits concurrent requests per client (X-Client-Id, then
Authorization, session, IP) and queues the rest first-in-first-out, with
bounded depth, max wait and idle eviction. Chain composes it with auth for
the Setup*Routes middleware slot. Exports burst, wait and depth metrics.
The test server uses it with a limit of 10.
This commit is contained in:
Hein
2026-09-30 15:46:42 +02:00
parent 178d40587d
commit 467dbd66c8
4 changed files with 831 additions and 3 deletions
+7 -1
View File
@@ -10,6 +10,7 @@ import (
"github.com/bitechdev/ResolveSpec/pkg/config"
"github.com/bitechdev/ResolveSpec/pkg/dbmanager"
"github.com/bitechdev/ResolveSpec/pkg/logger"
"github.com/bitechdev/ResolveSpec/pkg/middleware"
"github.com/bitechdev/ResolveSpec/pkg/modelregistry"
"github.com/bitechdev/ResolveSpec/pkg/server"
"github.com/bitechdev/ResolveSpec/pkg/testmodels"
@@ -67,8 +68,13 @@ func main() {
handler.RegisterModel("public", modelNames[i], model)
}
// Queue requests per client (X-Client-Id, Authorization, session, then IP)
// so a burst such as a page load cannot flood the database pool.
queue := middleware.NewClientQueue(middleware.ClientQueueConfig{MaxConcurrent: 10})
defer queue.Close()
// Setup routes using new SetupMuxRoutes function (without authentication)
resolvespec.SetupMuxRoutes(r, handler, nil)
resolvespec.SetupMuxRoutes(r, handler, middleware.Chain(queue.Middleware))
// Create server manager
mgr := server.NewManager()