feat(taskbuffer): add sliding-window rate limiting and result-sharing to TaskConstraintGroup and integrate with TaskManager
This commit is contained in:
@@ -143,7 +143,14 @@ export class TaskManager {
|
||||
}
|
||||
|
||||
try {
|
||||
return await task.trigger(input);
|
||||
const result = await task.trigger(input);
|
||||
// Record result for groups with result sharing (only on true success, not caught errors)
|
||||
if (!task.lastError) {
|
||||
for (const { group, key } of groups) {
|
||||
group.recordResult(key, result);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
// Release slots
|
||||
for (const { group, key } of groups) {
|
||||
@@ -181,6 +188,18 @@ export class TaskManager {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check result sharing — if any applicable group has a shared result, resolve immediately
|
||||
const sharingGroups = applicableGroups.filter(({ group }) => group.hasResultSharing());
|
||||
if (sharingGroups.length > 0) {
|
||||
const groupWithResult = sharingGroups.find(({ group, key }) =>
|
||||
group.getLastResult(key) !== undefined
|
||||
);
|
||||
if (groupWithResult) {
|
||||
entry.deferred.resolve(groupWithResult.group.getLastResult(groupWithResult.key)!.result);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const allCanRun = applicableGroups.every(({ group, key }) => group.canRun(key));
|
||||
if (allCanRun) {
|
||||
// executeWithConstraintTracking handles shouldExecute check internally
|
||||
@@ -190,9 +209,9 @@ export class TaskManager {
|
||||
);
|
||||
} else {
|
||||
stillQueued.push(entry);
|
||||
// Track shortest cooldown for timer scheduling
|
||||
// Track shortest delay for timer scheduling (cooldown + rate limit)
|
||||
for (const { group, key } of applicableGroups) {
|
||||
const remaining = group.getCooldownRemaining(key);
|
||||
const remaining = group.getNextAvailableDelay(key);
|
||||
if (remaining > 0 && remaining < shortestCooldown) {
|
||||
shortestCooldown = remaining;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user