Spaces:
Runtime error
Runtime error
File size: 6,610 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | type EnvSource = Record<string, string | undefined>;
type TimeoutLogger = (message: string) => void;
type ReadTimeoutOptions = {
allowZero?: boolean;
logger?: TimeoutLogger;
};
export const DEFAULT_FETCH_TIMEOUT_MS = 600_000;
export const DEFAULT_STREAM_IDLE_TIMEOUT_MS = 600_000;
export const MAX_TIMER_TIMEOUT_MS = 2_147_483_647;
export const DEFAULT_SSE_HEARTBEAT_INTERVAL_MS = 15_000;
export const DEFAULT_STREAM_READINESS_TIMEOUT_MS = 80_000;
export const DEFAULT_FETCH_CONNECT_TIMEOUT_MS = 30_000;
export const DEFAULT_FETCH_KEEPALIVE_TIMEOUT_MS = 4_000;
export const DEFAULT_API_BRIDGE_PROXY_TIMEOUT_MS = 600_000;
export const DEFAULT_API_BRIDGE_SERVER_REQUEST_TIMEOUT_MS = 300_000;
export const DEFAULT_API_BRIDGE_SERVER_HEADERS_TIMEOUT_MS = 60_000;
export const DEFAULT_API_BRIDGE_SERVER_KEEPALIVE_TIMEOUT_MS = 5_000;
export const DEFAULT_API_BRIDGE_SERVER_SOCKET_TIMEOUT_MS = 0;
function hasEnvValue(env: EnvSource, name: string): boolean {
const raw = env[name];
return raw != null && raw.trim() !== "";
}
export type UpstreamTimeoutConfig = {
fetchTimeoutMs: number;
streamIdleTimeoutMs: number;
sseHeartbeatIntervalMs: number;
streamReadinessTimeoutMs: number;
fetchHeadersTimeoutMs: number;
fetchBodyTimeoutMs: number;
fetchConnectTimeoutMs: number;
fetchKeepAliveTimeoutMs: number;
};
export type TlsClientTimeoutConfig = {
timeoutMs: number;
};
export type ApiBridgeTimeoutConfig = {
proxyTimeoutMs: number;
serverRequestTimeoutMs: number;
serverHeadersTimeoutMs: number;
serverKeepAliveTimeoutMs: number;
serverSocketTimeoutMs: number;
};
function readTimeoutMs(
env: EnvSource,
name: string,
defaultValue: number,
options: ReadTimeoutOptions = {}
): number {
const raw = env[name];
if (raw == null || raw.trim() === "") return defaultValue;
const parsed = Number(raw);
const isValid = Number.isFinite(parsed) && (options.allowZero ? parsed >= 0 : parsed > 0);
if (!isValid) {
options.logger?.(`Invalid ${name}="${raw}". Using default ${defaultValue}ms.`);
return defaultValue;
}
return Math.floor(parsed);
}
export function getUpstreamTimeoutConfig(
env: EnvSource = process.env,
logger?: TimeoutLogger
): UpstreamTimeoutConfig {
const sharedRequestTimeoutMs = hasEnvValue(env, "REQUEST_TIMEOUT_MS")
? readTimeoutMs(env, "REQUEST_TIMEOUT_MS", DEFAULT_FETCH_TIMEOUT_MS, {
allowZero: true,
logger,
})
: undefined;
const fetchTimeoutMs = readTimeoutMs(
env,
"FETCH_TIMEOUT_MS",
sharedRequestTimeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS,
{
allowZero: true,
logger,
}
);
const streamIdleTimeoutMs = readTimeoutMs(
env,
"STREAM_IDLE_TIMEOUT_MS",
sharedRequestTimeoutMs ?? DEFAULT_STREAM_IDLE_TIMEOUT_MS,
{
allowZero: true,
logger,
}
);
const streamReadinessTimeoutMs = readTimeoutMs(
env,
"STREAM_READINESS_TIMEOUT_MS",
sharedRequestTimeoutMs ?? DEFAULT_STREAM_READINESS_TIMEOUT_MS,
{
allowZero: true,
logger,
}
);
const sseHeartbeatIntervalMs = readTimeoutMs(
env,
"SSE_HEARTBEAT_INTERVAL_MS",
DEFAULT_SSE_HEARTBEAT_INTERVAL_MS,
{
allowZero: true,
logger,
}
);
return {
fetchTimeoutMs,
streamIdleTimeoutMs,
streamReadinessTimeoutMs,
sseHeartbeatIntervalMs,
fetchHeadersTimeoutMs: readTimeoutMs(env, "FETCH_HEADERS_TIMEOUT_MS", fetchTimeoutMs, {
allowZero: true,
logger,
}),
fetchBodyTimeoutMs: readTimeoutMs(env, "FETCH_BODY_TIMEOUT_MS", fetchTimeoutMs, {
allowZero: true,
logger,
}),
fetchConnectTimeoutMs: readTimeoutMs(
env,
"FETCH_CONNECT_TIMEOUT_MS",
DEFAULT_FETCH_CONNECT_TIMEOUT_MS,
{
allowZero: true,
logger,
}
),
fetchKeepAliveTimeoutMs: readTimeoutMs(
env,
"FETCH_KEEPALIVE_TIMEOUT_MS",
DEFAULT_FETCH_KEEPALIVE_TIMEOUT_MS,
{
logger,
}
),
};
}
export function getStainlessTimeoutSeconds(
env: EnvSource = process.env,
logger?: TimeoutLogger
): number {
const { fetchTimeoutMs } = getUpstreamTimeoutConfig(env, logger);
return Math.max(1, Math.ceil(fetchTimeoutMs / 1_000));
}
export function getTlsClientTimeoutConfig(
env: EnvSource = process.env,
logger?: TimeoutLogger
): TlsClientTimeoutConfig {
const upstream = getUpstreamTimeoutConfig(env, logger);
return {
timeoutMs: readTimeoutMs(env, "TLS_CLIENT_TIMEOUT_MS", upstream.fetchTimeoutMs, {
allowZero: true,
logger,
}),
};
}
export function getApiBridgeTimeoutConfig(
env: EnvSource = process.env,
logger?: TimeoutLogger
): ApiBridgeTimeoutConfig {
const sharedRequestTimeoutMs = hasEnvValue(env, "REQUEST_TIMEOUT_MS")
? readTimeoutMs(env, "REQUEST_TIMEOUT_MS", DEFAULT_FETCH_TIMEOUT_MS, {
allowZero: true,
logger,
})
: undefined;
const proxyTimeoutMs = readTimeoutMs(
env,
"API_BRIDGE_PROXY_TIMEOUT_MS",
sharedRequestTimeoutMs ?? DEFAULT_API_BRIDGE_PROXY_TIMEOUT_MS,
{
allowZero: true,
logger,
}
);
const derivedRequestTimeoutMs =
proxyTimeoutMs > 0
? Math.max(proxyTimeoutMs, DEFAULT_API_BRIDGE_SERVER_REQUEST_TIMEOUT_MS)
: DEFAULT_API_BRIDGE_SERVER_REQUEST_TIMEOUT_MS;
const serverRequestDefaultMs =
sharedRequestTimeoutMs !== undefined
? sharedRequestTimeoutMs > 0
? Math.max(sharedRequestTimeoutMs, derivedRequestTimeoutMs)
: 0
: derivedRequestTimeoutMs;
const serverKeepAliveTimeoutMs = readTimeoutMs(
env,
"API_BRIDGE_SERVER_KEEPALIVE_TIMEOUT_MS",
DEFAULT_API_BRIDGE_SERVER_KEEPALIVE_TIMEOUT_MS,
{
allowZero: true,
logger,
}
);
const serverHeadersTimeoutMs = readTimeoutMs(
env,
"API_BRIDGE_SERVER_HEADERS_TIMEOUT_MS",
DEFAULT_API_BRIDGE_SERVER_HEADERS_TIMEOUT_MS,
{
allowZero: true,
logger,
}
);
return {
proxyTimeoutMs,
serverRequestTimeoutMs: readTimeoutMs(
env,
"API_BRIDGE_SERVER_REQUEST_TIMEOUT_MS",
serverRequestDefaultMs,
{
allowZero: true,
logger,
}
),
serverHeadersTimeoutMs:
serverHeadersTimeoutMs > 0 && serverKeepAliveTimeoutMs > 0
? Math.max(serverHeadersTimeoutMs, serverKeepAliveTimeoutMs + 1_000)
: serverHeadersTimeoutMs,
serverKeepAliveTimeoutMs,
serverSocketTimeoutMs: readTimeoutMs(
env,
"API_BRIDGE_SERVER_SOCKET_TIMEOUT_MS",
DEFAULT_API_BRIDGE_SERVER_SOCKET_TIMEOUT_MS,
{
allowZero: true,
logger,
}
),
};
}
|