Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -1,23 +1,8 @@
|
||||
import { REMOTE_RUNTIME_MAX_PREPARED_RPC_BYTES } from './remote-runtime-memory-limits'
|
||||
|
||||
const DEFAULT_REMOTE_RUNTIME_CALL_CONCURRENCY = 8
|
||||
const DEFAULT_REMOTE_RUNTIME_BACKGROUND_CALL_CONCURRENCY = 2
|
||||
export const RUNTIME_RPC_MAX_QUEUED_CALLS_PER_SELECTOR = 256
|
||||
export const RUNTIME_RPC_MAX_QUEUED_CALLS_TOTAL = 2_048
|
||||
export const RUNTIME_RPC_QUEUE_OVERLOAD_CODE = 'runtime_rpc_queue_overloaded'
|
||||
|
||||
export class RuntimeRpcCallQueueOverloadError extends Error {
|
||||
readonly code = RUNTIME_RPC_QUEUE_OVERLOAD_CODE
|
||||
|
||||
constructor(readonly scope: 'selector' | 'global' | 'memory') {
|
||||
super('Remote runtime call queue is full; retry after current calls finish.')
|
||||
this.name = 'RuntimeRpcCallQueueOverloadError'
|
||||
}
|
||||
}
|
||||
|
||||
type QueuedRuntimeCall<T> = {
|
||||
background: boolean
|
||||
retainedBytes: number
|
||||
run: () => Promise<T>
|
||||
resolve: (value: T) => void
|
||||
reject: (error: unknown) => void
|
||||
@@ -49,51 +34,23 @@ export function isBackgroundRuntimeMethod(method: string): boolean {
|
||||
|
||||
export class RuntimeRpcCallQueuePool {
|
||||
private readonly queues = new Map<string, RuntimeCallQueue>()
|
||||
private queuedCallCount = 0
|
||||
private retainedCallBytes = 0
|
||||
|
||||
constructor(
|
||||
private readonly concurrency = DEFAULT_REMOTE_RUNTIME_CALL_CONCURRENCY,
|
||||
private readonly backgroundConcurrency = DEFAULT_REMOTE_RUNTIME_BACKGROUND_CALL_CONCURRENCY,
|
||||
private readonly maxQueuedPerSelector = RUNTIME_RPC_MAX_QUEUED_CALLS_PER_SELECTOR,
|
||||
private readonly maxQueuedTotal = RUNTIME_RPC_MAX_QUEUED_CALLS_TOTAL,
|
||||
private readonly maxRetainedBytes = REMOTE_RUNTIME_MAX_PREPARED_RPC_BYTES
|
||||
private readonly backgroundConcurrency = DEFAULT_REMOTE_RUNTIME_BACKGROUND_CALL_CONCURRENCY
|
||||
) {}
|
||||
|
||||
enqueue<T>(
|
||||
selector: string,
|
||||
method: string,
|
||||
run: () => Promise<T>,
|
||||
retainedBytes = 0
|
||||
): Promise<T> {
|
||||
if (this.queuedCallCount >= this.maxQueuedTotal) {
|
||||
return Promise.reject(new RuntimeRpcCallQueueOverloadError('global'))
|
||||
}
|
||||
const existingQueue = this.queues.get(selector)
|
||||
if (existingQueue && this.queuedCount(existingQueue) >= this.maxQueuedPerSelector) {
|
||||
return Promise.reject(new RuntimeRpcCallQueueOverloadError('selector'))
|
||||
}
|
||||
if (
|
||||
!Number.isSafeInteger(retainedBytes) ||
|
||||
retainedBytes < 0 ||
|
||||
this.retainedCallBytes + retainedBytes > this.maxRetainedBytes
|
||||
) {
|
||||
return Promise.reject(new RuntimeRpcCallQueueOverloadError('memory'))
|
||||
}
|
||||
|
||||
enqueue<T>(selector: string, method: string, run: () => Promise<T>): Promise<T> {
|
||||
const queue = this.getQueue(selector)
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const call: QueuedRuntimeCall<T> = {
|
||||
background: isBackgroundRuntimeMethod(method),
|
||||
retainedBytes,
|
||||
run,
|
||||
resolve,
|
||||
reject
|
||||
}
|
||||
const targetQueue = call.background ? queue.background : queue.foreground
|
||||
targetQueue.push(call as QueuedRuntimeCall<unknown>)
|
||||
this.queuedCallCount += 1
|
||||
this.retainedCallBytes += retainedBytes
|
||||
this.pump(selector, queue)
|
||||
})
|
||||
}
|
||||
@@ -139,7 +96,6 @@ export class RuntimeRpcCallQueuePool {
|
||||
runPromise = Promise.reject(error)
|
||||
}
|
||||
void runPromise.then(call.resolve, call.reject).finally(() => {
|
||||
this.retainedCallBytes = Math.max(0, this.retainedCallBytes - call.retainedBytes)
|
||||
queue.active = Math.max(0, queue.active - 1)
|
||||
if (call.background) {
|
||||
queue.backgroundActive = Math.max(0, queue.backgroundActive - 1)
|
||||
@@ -159,7 +115,6 @@ export class RuntimeRpcCallQueuePool {
|
||||
}
|
||||
const call = queue.foreground[queue.foregroundHead]
|
||||
queue.foregroundHead += 1
|
||||
this.queuedCallCount = Math.max(0, this.queuedCallCount - 1)
|
||||
this.compactForeground(queue)
|
||||
return call
|
||||
}
|
||||
@@ -170,7 +125,6 @@ export class RuntimeRpcCallQueuePool {
|
||||
}
|
||||
const call = queue.background[queue.backgroundHead]
|
||||
queue.backgroundHead += 1
|
||||
this.queuedCallCount = Math.max(0, this.queuedCallCount - 1)
|
||||
this.compactBackground(queue)
|
||||
return call
|
||||
}
|
||||
@@ -199,13 +153,4 @@ export class RuntimeRpcCallQueuePool {
|
||||
queue.backgroundHead >= queue.background.length
|
||||
)
|
||||
}
|
||||
|
||||
private queuedCount(queue: RuntimeCallQueue): number {
|
||||
return (
|
||||
queue.foreground.length -
|
||||
queue.foregroundHead +
|
||||
queue.background.length -
|
||||
queue.backgroundHead
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user