Repository navigation
refactor(combo): god-file split D7a — round-robin sticky state to combo/rrState.ts (QG v2 Fase 9 T5) #4196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(combo): god-file split D7a — round-robin sticky state to combo/rrState.ts (QG v2 Fase 9 T5) #4196
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** | ||
| * Round-robin sticky state for combo routing. | ||
| * | ||
| * Holds the two mutable module-level Maps that back round-robin distribution | ||
| * (`rrCounters`) and sticky round-robin target affinity (`rrStickyTargets`), | ||
| * plus the helpers that read/write them. Extracted byte-identically from | ||
| * combo.ts (QG v2 Fase 9 T5 D7a). | ||
| * | ||
| * State cohesion: these two Maps MUST remain single instances. combo.ts imports | ||
| * the same references back and mutates them directly (orderTargetsByResetAwareQuota, | ||
| * orderTargetsByResetWindow, handleRoundRobinCombo) — never duplicate a Map. | ||
| * | ||
| * Pure leaf: this module never imports from the combo barrel. | ||
| */ | ||
|
|
||
| import type { ResolvedComboTarget } from "./types.ts"; | ||
|
|
||
| // In-memory atomic counter per combo for round-robin distribution | ||
| // Resets on server restart (by design — no stale state) | ||
| // Eviction limits to prevent unbounded memory growth | ||
| export const MAX_RR_COUNTERS = 500; | ||
|
|
||
| export const rrCounters = new Map<string, number>(); | ||
| export const rrStickyTargets = new Map< | ||
| string, | ||
| { executionKey: string; successCount: number } | ||
| >(); | ||
|
|
||
| export function clampStickyRoundRobinTargetLimit(value: unknown): number { | ||
| const numericValue = Number(value); | ||
| if (!Number.isFinite(numericValue)) return 1; | ||
| return Math.min(Math.max(Math.floor(numericValue), 1), 1000); | ||
| } | ||
|
|
||
| export function getStickyRoundRobinStartIndex( | ||
| comboName: string, | ||
| targets: ResolvedComboTarget[], | ||
| stickyLimit: number | ||
| ): { startIndex: number; counter: number } { | ||
| const sticky = rrStickyTargets.get(comboName); | ||
| const stickyIndex = sticky | ||
| ? targets.findIndex((target) => target.executionKey === sticky.executionKey) | ||
| : -1; | ||
| if (stickyLimit > 1 && sticky && stickyIndex >= 0 && sticky.successCount < stickyLimit) { | ||
| return { startIndex: stickyIndex, counter: rrCounters.get(comboName) || 0 }; | ||
| } | ||
|
|
||
| const counter = rrCounters.get(comboName) || 0; | ||
| return { startIndex: counter % targets.length, counter }; | ||
| } | ||
|
|
||
| export function recordStickyRoundRobinSuccess( | ||
| comboName: string, | ||
| target: ResolvedComboTarget, | ||
| stickyLimit: number, | ||
| targets: ResolvedComboTarget[] | ||
| ): void { | ||
| const sticky = rrStickyTargets.get(comboName); | ||
| const successCount = sticky?.executionKey === target.executionKey ? sticky.successCount + 1 : 1; | ||
| if (successCount >= stickyLimit) { | ||
| const servedIndex = targets.findIndex((entry) => entry.executionKey === target.executionKey); | ||
| rrCounters.set( | ||
| comboName, | ||
| servedIndex >= 0 ? servedIndex + 1 : (rrCounters.get(comboName) || 0) + 1 | ||
| ); | ||
| rrStickyTargets.delete(comboName); | ||
| return; | ||
| } | ||
|
|
||
| rrStickyTargets.set(comboName, { executionKey: target.executionKey, successCount }); | ||
| } | ||
|
Comment on lines
+52
to
+71
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Para aplicar programação defensiva, devemos adicionar uma verificação para lidar com casos em que export function recordStickyRoundRobinSuccess(
comboName: string,
target: ResolvedComboTarget,
stickyLimit: number,
targets: ResolvedComboTarget[]
): void {
if (!target || !targets || targets.length === 0) return;
const sticky = rrStickyTargets.get(comboName);
const successCount = sticky?.executionKey === target.executionKey ? sticky.successCount + 1 : 1;
if (successCount >= stickyLimit) {
const servedIndex = targets.findIndex((entry) => entry.executionKey === target.executionKey);
rrCounters.set(
comboName,
servedIndex >= 0 ? servedIndex + 1 : (rrCounters.get(comboName) || 0) + 1
);
rrStickyTargets.delete(comboName);
return;
}
rrStickyTargets.set(comboName, { executionKey: target.executionKey, successCount });
} |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Para aplicar programação defensiva e evitar possíveis erros em tempo de execução (como um erro de tipo se
targetsfor nulo/indefinido, ouNaNsetargetsestiver vazio devido à divisão por zero emcounter % targets.length), é recomendável adicionar uma verificação defensiva no início da função.