@@ -10,6 +10,7 @@ import { getFleetHealth } from './fireworks-health'
1010import {
1111 activeCountsByModel ,
1212 admitFromQueue ,
13+ evictBanned ,
1314 queueDepth ,
1415 sweepExpired ,
1516} from './store'
@@ -20,6 +21,7 @@ import { logger } from '@/util/logger'
2021
2122export interface AdmissionDeps {
2223 sweepExpired : ( now : Date , graceMs : number ) => Promise < number >
24+ evictBanned : ( ) => Promise < number >
2325 queueDepth : ( params : { model : string } ) => Promise < number >
2426 activeCountsByModel : ( ) => Promise < Record < string , number > >
2527 admitFromQueue : ( params : {
@@ -39,6 +41,7 @@ export interface AdmissionDeps {
3941
4042const defaultDeps : AdmissionDeps = {
4143 sweepExpired,
44+ evictBanned,
4245 queueDepth,
4346 activeCountsByModel,
4447 admitFromQueue,
@@ -60,6 +63,8 @@ const defaultDeps: AdmissionDeps = {
6063
6164export interface AdmissionTickResult {
6265 expired : number
66+ /** Free_session rows removed because the user is banned. */
67+ evictedBanned : number
6368 admitted : number
6469 /** Per-model queue depth at the end of the tick. */
6570 queueDepthByModel : Record < string , number >
@@ -86,7 +91,12 @@ export async function runAdmissionTick(
8691 deps : AdmissionDeps = defaultDeps ,
8792) : Promise < AdmissionTickResult > {
8893 const now = ( deps . now ?? ( ( ) => new Date ( ) ) ) ( )
89- const expired = await deps . sweepExpired ( now , deps . graceMs )
94+ // Run eviction before admission so a banned user freed from a slot in this
95+ // tick frees room for a queued user to be admitted in the same tick.
96+ const [ expired , evictedBanned ] = await Promise . all ( [
97+ deps . sweepExpired ( now , deps . graceMs ) ,
98+ deps . evictBanned ( ) ,
99+ ] )
90100
91101 const models = deps . models ?? FREEBUFF_MODELS . map ( ( m ) => m . id )
92102
@@ -122,6 +132,7 @@ export async function runAdmissionTick(
122132
123133 return {
124134 expired,
135+ evictedBanned,
125136 admitted : totalAdmitted ,
126137 queueDepthByModel,
127138 activeCountByModel,
@@ -145,6 +156,7 @@ function runTick() {
145156 metric : 'freebuff_waiting_room' ,
146157 admitted : result . admitted ,
147158 expired : result . expired ,
159+ evictedBanned : result . evictedBanned ,
148160 queueDepthByModel : result . queueDepthByModel ,
149161 activeCountByModel : result . activeCountByModel ,
150162 skipped : result . skipped ,
0 commit comments