History

Add OpenAPI response headers timeout

Rhys Sullivan committed 4 months ago

Commit e431b7b1 parent c1893c55 files · +376 −48Browse files

Unified · Split

5 files
  1. packages/plugins/openapi/src/sdk/backing.ts +28 −3
  2. packages/plugins/openapi/src/sdk/index.ts +8 −1
  3. packages/plugins/openapi/src/sdk/invoke.ts +130 −44
  4. packages/plugins/openapi/src/sdk/plugin.ts +3 −0
  5. packages/plugins/openapi/src/sdk/response-headers-timeout.test.ts +207 −0
packages/plugins/openapi/src/sdk/backing.ts+28−3
@@ -30,7 +30,7 @@ import { · 29 unmodified lines
3030 requiredTemplateVariables,
3131 type OpenApiIntegrationConfig,
3232 } from "./config";
33−import { OpenApiExtractionError, OpenApiParseError } from "./errors";
33+import { OpenApiExtractionError, OpenApiInvocationError, OpenApiParseError } from "./errors";
3434 import {
3535 buildInputSchema,
3636 extract,
@@ -38,7 +38,13 @@ import { · 1 unmodified line
3838 streamOperationBindingsFromStructure,
3939 } from "./extract";
4040 import { compileToolDefinitions, type ToolDefinition } from "./definitions";
41−import { annotationsForOperation, buildRequest, invokeWithLayer, REQUIRE_APPROVAL } from "./invoke";
41+import {
42+ annotationsForOperation,
43+ buildRequest,
44+ invokeWithLayer,
45+ REQUIRE_APPROVAL,
46+ type InvokeOptions,
47+} from "./invoke";
4248 import { parse, type ParsedDocument } from "./parse";
4349 import { parseEntry, structuralSplit, type KeepPathItem, type SpecStructure } from "./split";
4450 import { type OpenapiStore, type StoredOperation } from "./store";
@@ -609,6 +615,7 @@ export const invokeOpenApiBackedTool = (input: { · 564 unmodified lines
609615 readonly credential: ToolInvocationCredential;
610616 readonly args: unknown;
611617 readonly httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>;
618+ readonly invokeOptions?: InvokeOptions;
612619 }) =>
613620 Effect.gen(function* () {
614621 const integration = input.toolRow.integration;
@@ -671,15 +678,33 @@ export const invokeOpenApiBackedTool = (input: { · 56 unmodified lines
671678 Object.assign(queryParams, rendered.queryParams);
672679 }
673680
674− const result = yield* invokeWithLayer(
681+ const invocation = yield* invokeWithLayer(
675682 binding,
676683 (input.args ?? {}) as Record<string, unknown>,
677684 config?.baseUrl ?? "",
678685 headers,
679686 queryParams,
680687 input.httpClientLayer,
688+ input.invokeOptions,
689+ ).pipe(
690+ Effect.map((result) => ({ ok: true as const, result })),
691+ Effect.catchTag("OpenApiInvocationError", (error: OpenApiInvocationError) =>
692+ error.message.startsWith("Upstream returned no response headers within ")
693+ ? Effect.succeed({
694+ ok: false as const,
695+ failure: ToolResult.fail({
696+ code: "upstream_http_error",
697+ message: error.message,
698+ details: error.cause ?? error,
699+ }),
700+ })
701+ : Effect.fail(error),
702+ ),
681703 );
682704
705+ if (!invocation.ok) return invocation.failure;
706+
707+ const result = invocation.result;
683708 const ok = result.status >= 200 && result.status < 300;
684709 if (!ok) {
685710 if (result.status === 401 || result.status === 403) {
packages/plugins/openapi/src/sdk/index.ts+8−1
@@ -12,7 +12,14 @@ export { · 11 unmodified lines
1212 type ByteRange,
1313 type KeepPathItem,
1414 } from "./split";
15−export { invoke, invokeWithLayer, buildRequest, annotationsForOperation } from "./invoke";
15+export {
16+ invoke,
17+ invokeWithLayer,
18+ buildRequest,
19+ annotationsForOperation,
20+ RESPONSE_HEADERS_TIMEOUT_MS,
21+ type InvokeOptions,
22+} from "./invoke";
1623 export {
1724 buildDefsJsonStreaming,
1825 checkHealthOpenApi,
packages/plugins/openapi/src/sdk/invoke.ts+130−44
@@ -1,5 +1,5 @@
1−import { Effect, Exit, Layer, Option, Schema, Stream } from "effect";
2−import { HttpClient, HttpClientRequest } from "effect/unstable/http";
1+import { Effect, Exit, Fiber, Layer, Option, Schema, Stream } from "effect";
2+import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http";
33 import type { ToolFileValue } from "@executor-js/sdk/core";
44
55 import { OpenApiInvocationError } from "./errors";
@@ -153,6 +153,7 @@ const normalizeContentType = (ct: string | null | undefined): string => · 147 unmodified lines
153153
154154 export const STREAM_MAX_BYTES = 1_000_000;
155155 export const STREAM_MAX_MS = 10_000;
156+export const RESPONSE_HEADERS_TIMEOUT_MS = 60_000;
156157
157158 class StreamCapReached extends Error {
158159 readonly _tag = "StreamCapReached";
@@ -163,6 +164,16 @@ export interface StreamingResponseCaps { · 4 unmodified lines
163164 readonly maxMs?: number;
164165 }
165166
167+export interface InvokeOptions {
168+ readonly responseHeadersTimeoutMs?: number;
169+}
170+
171+const formatTimeout = (timeoutMs: number): string =>
172+ timeoutMs % 1_000 === 0 ? `${timeoutMs / 1_000}s` : `${timeoutMs}ms`;
173+
174+const responseHeadersTimeoutMessage = (timeoutMs: number): string =>
175+ `Upstream returned no response headers within ${formatTimeout(timeoutMs)}. The endpoint may be a live stream with no data to send (for example, runtime logs of an idle deployment); the request was aborted. Retry when the resource has activity, or use a non-streaming endpoint.`;
176+
166177 const STREAMING_RESPONSE_CONTENT_TYPES = new Set([
167178 "application/stream+json",
168179 "application/x-ndjson",
@@ -260,39 +271,71 @@ export const collectStreamingBody = ( · 91 unmodified lines
260271 let truncated = false;
261272 const startedAt = Date.now();
262273
263− const completed = yield* stream.pipe(
264− Stream.timeout(maxMs),
265− Stream.runForEach((chunk) =>
266− Effect.gen(function* () {
267− const remaining = maxBytes - bytes;
268− if (remaining <= 0) {
269− truncated = true;
270− return yield* Effect.fail(new StreamCapReached());
271− }
272−
273− if (chunk.byteLength > remaining) {
274− chunks.push(chunk.subarray(0, remaining));
275− bytes += remaining;
276− truncated = true;
277− return yield* Effect.fail(new StreamCapReached());
278− }
279−
280− chunks.push(chunk);
281− bytes += chunk.byteLength;
282− if (bytes >= maxBytes) {
283− truncated = true;
284− return yield* Effect.fail(new StreamCapReached());
285− }
286− }),
287− ),
288− Effect.timeoutOption(maxMs + 1_000),
289− Effect.catch((error: unknown) =>
290− error instanceof StreamCapReached
291− ? Effect.succeed(Option.some(undefined))
292− : Effect.fail(error),
293− ),
274+ const runFork = Effect.runForkWith(yield* Effect.context<never>());
275+ const completedExitOption = yield* Effect.callback<Option.Option<Exit.Exit<void, unknown>>>(
276+ (resume, signal) => {
277+ let settled = false;
278+ const collectEffect = stream.pipe(
279+ Stream.timeout(maxMs),
280+ Stream.runForEach((chunk) =>
281+ Effect.gen(function* () {
282+ const remaining = maxBytes - bytes;
283+ if (remaining <= 0) {
284+ truncated = true;
285+ return yield* Effect.fail(new StreamCapReached());
286+ }
287+
288+ if (chunk.byteLength > remaining) {
289+ chunks.push(chunk.subarray(0, remaining));
290+ bytes += remaining;
291+ truncated = true;
292+ return yield* Effect.fail(new StreamCapReached());
293+ }
294+
295+ chunks.push(chunk);
296+ bytes += chunk.byteLength;
297+ if (bytes >= maxBytes) {
298+ truncated = true;
299+ return yield* Effect.fail(new StreamCapReached());
300+ }
301+ }),
302+ ),
303+ Effect.catch((error: unknown) =>
304+ error instanceof StreamCapReached ? Effect.void : Effect.fail(error),
305+ ),
306+ Effect.exit,
307+ Effect.tap((exit) =>
308+ Effect.sync(() => {
309+ if (settled) return;
310+ settled = true;
311+ clearTimeout(timer);
312+ resume(Effect.succeed(Option.some(exit)));
313+ }),
314+ ),
315+ );
316+ const fiber = runFork(collectEffect);
317+ const interrupt = () => {
318+ runFork(Fiber.interrupt(fiber));
319+ };
320+ const timer = setTimeout(() => {
321+ if (settled) return;
322+ settled = true;
323+ truncated = true;
324+ interrupt();
325+ resume(Effect.succeed(Option.none()));
326+ }, maxMs + 1_000);
327+ signal.addEventListener("abort", interrupt, { once: true });
328+ return Effect.sync(() => {
329+ clearTimeout(timer);
330+ signal.removeEventListener("abort", interrupt);
331+ if (!settled) interrupt();
332+ });
333+ },
294334 );
295− if (Option.isNone(completed)) truncated = true;
335+ if (Option.isSome(completedExitOption) && Exit.isFailure(completedExitOption.value)) {
336+ return yield* Effect.failCause(completedExitOption.value.cause);
337+ }
338+ if (Option.isNone(completedExitOption)) truncated = true;
296339
297340 const durationMs = Date.now() - startedAt;
298341 if (durationMs >= maxMs) truncated = true;
@@ -952,6 +995,7 @@ export const invoke = Effect.fn("OpenApi.invoke")(function* ( · 653 unmodified lines
952995 args: Record<string, unknown>,
953996 resolvedHeaders: Record<string, string>,
954997 sourceQueryParams: Record<string, string> = {},
998+ options: InvokeOptions = {},
955999 ) {
9561000 const client = yield* HttpClient.HttpClient;
9571001
@@ -965,16 +1009,57 @@ export const invoke = Effect.fn("OpenApi.invoke")(function* ( · 7 unmodified lines
9651009
9661010 const request = yield* buildRequest(operation, args, resolvedHeaders, sourceQueryParams);
9671011
968− const response = yield* client.execute(request).pipe(
969− Effect.mapError(
970− (err) =>
971− new OpenApiInvocationError({
972− message: "HTTP request failed",
973− statusCode: Option.none(),
974− cause: err,
1012+ const responseHeadersTimeoutMs = options.responseHeadersTimeoutMs ?? RESPONSE_HEADERS_TIMEOUT_MS;
1013+ const runFork = Effect.runForkWith(yield* Effect.context<never>());
1014+ const responseExitOption = yield* Effect.callback<
1015+ Option.Option<Exit.Exit<HttpClientResponse.HttpClientResponse, OpenApiInvocationError>>
1016+ >((resume, signal) => {
1017+ let settled = false;
1018+ const responseEffect = client.execute(request).pipe(
1019+ Effect.mapError(
1020+ (err) =>
1021+ new OpenApiInvocationError({
1022+ message: "HTTP request failed",
1023+ statusCode: Option.none(),
1024+ cause: err,
1025+ }),
1026+ ),
1027+ Effect.exit,
1028+ Effect.tap((exit) =>
1029+ Effect.sync(() => {
1030+ if (settled) return;
1031+ settled = true;
1032+ clearTimeout(timer);
1033+ resume(Effect.succeed(Option.some(exit)));
9751034 }),
976− ),
977− );
1035+ ),
1036+ );
1037+ const fiber = runFork(responseEffect);
1038+ const interrupt = () => {
1039+ runFork(Fiber.interrupt(fiber));
1040+ };
1041+ const timer = setTimeout(() => {
1042+ if (settled) return;
1043+ settled = true;
1044+ interrupt();
1045+ resume(Effect.succeed(Option.none()));
1046+ }, responseHeadersTimeoutMs);
1047+ signal.addEventListener("abort", interrupt, { once: true });
1048+ return Effect.sync(() => {
1049+ clearTimeout(timer);
1050+ signal.removeEventListener("abort", interrupt);
1051+ if (!settled) interrupt();
1052+ });
1053+ });
1054+ if (Option.isNone(responseExitOption)) {
1055+ return yield* new OpenApiInvocationError({
1056+ message: responseHeadersTimeoutMessage(responseHeadersTimeoutMs),
1057+ statusCode: Option.none(),
1058+ });
1059+ }
1060+ const responseExit = responseExitOption.value;
1061+ if (Exit.isFailure(responseExit)) return yield* Effect.failCause(responseExit.cause);
1062+ const response = responseExit.value;
9781063
9791064 const status = response.status;
9801065 yield* Effect.annotateCurrentSpan({
@@ -1073,6 +1158,7 @@ export const invokeWithLayer = ( · 92 unmodified lines
10731158 resolvedHeaders: Record<string, string>,
10741159 sourceQueryParams: Record<string, string>,
10751160 httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>,
1161+ options: InvokeOptions = {},
10761162 ) => {
10771163 const effectiveBaseUrl = resolveRequestHost(operation.servers ?? [], args.server, baseUrl);
10781164 const clientWithBaseUrl = effectiveBaseUrl
@@ -1085,7 +1171,7 @@ export const invokeWithLayer = ( · 6 unmodified lines
10851171 ).pipe(Layer.provide(httpClientLayer))
10861172 : httpClientLayer;
10871173
1088− return invoke(operation, args, resolvedHeaders, sourceQueryParams).pipe(
1174+ return invoke(operation, args, resolvedHeaders, sourceQueryParams, options).pipe(
10891175 Effect.provide(clientWithBaseUrl),
10901176 // `invoke` annotates http.status_code on ITS span (`OpenApi.invoke`,
10911177 // via Effect.fn) — annotateCurrentSpan inside it never reaches this
packages/plugins/openapi/src/sdk/plugin.ts+3−0
@@ -48,6 +48,7 @@ import { · 47 unmodified lines
4848 resolveOpenApiBackedTools,
4949 validateOpenApiBackedToolArgs,
5050 } from "./backing";
51+import type { InvokeOptions } from "./invoke";
5152 import { resolveServerUrl } from "./openapi-utils";
5253
5354 // ---------------------------------------------------------------------------
@@ -563,6 +564,7 @@ export const describeOpenApiIntegrationDisplay = ( · 509 unmodified lines
563564
564565 export interface OpenApiPluginOptions {
565566 readonly httpClientLayer?: Layer.Layer<HttpClient.HttpClient, never, never>;
567+ readonly invokeOptions?: InvokeOptions;
566568 }
567569
568570 export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => {
@@ -1044,6 +1046,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 475 unmodified lines
10441046 credential,
10451047 args,
10461048 httpClientLayer,
1049+ invokeOptions: options?.invokeOptions,
10471050 });
10481051 },
10491052
packages/plugins/openapi/src/sdk/response-headers-timeout.test.ts+207−0
@@ -0,0 +1,207 @@
1+import { describe, expect, it } from "@effect/vitest";
2+import { Deferred, Effect, Option, Schema } from "effect";
3+import { FetchHttpClient } from "effect/unstable/http";
4+import { HttpApi, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi";
5+import { createServer, type ServerResponse } from "node:http";
6+import type { AddressInfo, Socket } from "node:net";
7+
8+import {
9+ createExecutor,
10+ AuthTemplateSlug,
11+ ConnectionName,
12+ IntegrationSlug,
13+ ToolAddress,
14+} from "@executor-js/sdk";
15+import { makeTestConfig, memoryCredentialsPlugin } from "@executor-js/sdk/testing";
16+import { makeOpenApiHttpApiTestSourceConfig, unwrapInvocation } from "../testing";
17+
18+import { invokeWithLayer } from "./invoke";
19+import { openApiPlugin } from "./plugin";
20+import type { OperationBinding } from "./types";
21+
22+const RESPONSE_HEADERS_TIMEOUT_MS = 100;
23+const STREAM_TOOL = "logs.getLogs";
24+const encoder = new TextEncoder();
25+
26+const LogsGroup = HttpApiGroup.make("logs").add(
27+ HttpApiEndpoint.get("getLogs", "/logs", {
28+ success: Schema.Unknown,
29+ }),
30+);
31+
32+const TimeoutApi = HttpApi.make("responseHeadersTimeoutTest")
33+ .add(LogsGroup)
34+ .annotateMerge(OpenApi.annotations({ title: "ResponseHeadersTimeoutTest", version: "1.0.0" }));
35+
36+const testPlugins = () =>
37+ [
38+ openApiPlugin({
39+ httpClientLayer: FetchHttpClient.layer,
40+ invokeOptions: { responseHeadersTimeoutMs: RESPONSE_HEADERS_TIMEOUT_MS },
41+ }),
42+ memoryCredentialsPlugin(),
43+ ] as const;
44+
45+const buildExecutor = (baseUrl: string) =>
46+ Effect.gen(function* () {
47+ const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
48+ yield* executor.openapi.addSpec(
49+ makeOpenApiHttpApiTestSourceConfig(TimeoutApi, { slug: "headers_timeout", baseUrl }),
50+ );
51+ yield* executor.connections.create({
52+ owner: "org",
53+ name: ConnectionName.make("main"),
54+ integration: IntegrationSlug.make("headers_timeout"),
55+ template: AuthTemplateSlug.make("apiKey"),
56+ value: "token",
57+ });
58+ return {
59+ executor,
60+ address: ToolAddress.make(`tools.headers_timeout.org.main.${STREAM_TOOL}`),
61+ };
62+ });
63+
64+const startRawServer = (handler: (res: ServerResponse) => void) =>
65+ Effect.acquireRelease(
66+ Effect.callback<{ baseUrl: string; close: () => void }>((resume) => {
67+ const sockets = new Set<Socket>();
68+ const timers = new Set<ReturnType<typeof setTimeout>>();
69+ const server = createServer((req, res) => {
70+ if (req.url?.split("?")[0] !== "/logs") {
71+ res.writeHead(200, { "content-type": "application/json" });
72+ res.end(JSON.stringify({ ok: true }));
73+ return;
74+ }
75+ handler(res);
76+ });
77+ server.on("connection", (socket) => {
78+ sockets.add(socket);
79+ socket.on("close", () => sockets.delete(socket));
80+ });
81+ server.listen(0, "127.0.0.1", () => {
82+ const port = (server.address() as AddressInfo).port;
83+ resume(
84+ Effect.succeed({
85+ baseUrl: `http://127.0.0.1:${port}`,
86+ close: () => {
87+ for (const timer of timers) clearTimeout(timer);
88+ for (const socket of sockets) socket.destroy();
89+ server.close();
90+ },
91+ }),
92+ );
93+ });
94+ }),
95+ (server) => Effect.sync(() => server.close()),
96+ );
97+
98+const startHeaderlessServer = (closed: Deferred.Deferred<void>) =>
99+ Effect.acquireRelease(
100+ Effect.callback<{ baseUrl: string; close: () => void }>((resume) => {
101+ const sockets = new Set<Socket>();
102+ const server = createServer((req, res) => {
103+ if (req.url?.split("?")[0] === "/logs") return;
104+ res.writeHead(200, { "content-type": "application/json" });
105+ res.end(JSON.stringify({ ok: true }));
106+ });
107+ server.on("connection", (socket) => {
108+ sockets.add(socket);
109+ socket.on("close", () => {
110+ sockets.delete(socket);
111+ Effect.runFork(Deferred.succeed(closed, undefined));
112+ });
113+ });
114+ server.listen(0, "127.0.0.1", () => {
115+ const port = (server.address() as AddressInfo).port;
116+ resume(
117+ Effect.succeed({
118+ baseUrl: `http://127.0.0.1:${port}`,
119+ close: () => {
120+ for (const socket of sockets) socket.destroy();
121+ server.close();
122+ },
123+ }),
124+ );
125+ });
126+ }),
127+ (server) => Effect.sync(() => server.close()),
128+ );
129+
130+const operation: OperationBinding = {
131+ method: "get",
132+ pathTemplate: "/logs",
133+ parameters: [],
134+ requestBody: Option.none(),
135+ responseBody: Option.none(),
136+ servers: [],
137+};
138+
139+describe("OpenAPI response headers timeout", () => {
140+ it.effect("aborts a headerless upstream and returns an actionable tool failure", () =>
141+ Effect.gen(function* () {
142+ const closed = yield* Deferred.make<void>();
143+ const server = yield* startHeaderlessServer(closed);
144+ const { executor, address } = yield* buildExecutor(server.baseUrl);
145+ const startedAt = Date.now();
146+
147+ const result = yield* executor.execute(address, {});
148+ const elapsedMs = Date.now() - startedAt;
149+ const socketClosed = yield* Deferred.await(closed).pipe(Effect.timeoutOption(1_000));
150+
151+ expect(elapsedMs).toBeGreaterThanOrEqual(RESPONSE_HEADERS_TIMEOUT_MS - 25);
152+ expect(elapsedMs).toBeLessThan(2_000);
153+ expect(Option.isSome(socketClosed)).toBe(true);
154+ expect(result).toMatchObject({
155+ ok: false,
156+ error: {
157+ code: "upstream_http_error",
158+ message: expect.stringContaining("Upstream returned no response headers within 100ms"),
159+ },
160+ });
161+ }),
162+ );
163+
164+ it.effect("leaves normal fast JSON responses unaffected", () =>
165+ Effect.gen(function* () {
166+ const server = yield* startRawServer((res) => {
167+ res.writeHead(200, { "content-type": "application/json" });
168+ res.end(JSON.stringify({ ok: true }));
169+ });
170+ const { executor, address } = yield* buildExecutor(server.baseUrl);
171+
172+ const result = unwrapInvocation<Record<string, unknown>>(
173+ yield* executor.execute(address, {}),
174+ );
175+
176+ expect(result.status).toBe(200);
177+ expect(result.data).toEqual({ ok: true });
178+ }),
179+ );
180+
181+ it.effect("does not apply the headers timeout after streaming response headers arrive", () =>
182+ Effect.gen(function* () {
183+ const server = yield* startRawServer((res) => {
184+ res.writeHead(200, { "content-type": "application/jsonl" });
185+ res.write(encoder.encode(`${JSON.stringify({ id: 1 })}\n`));
186+ setTimeout(() => {
187+ res.write(encoder.encode(`${JSON.stringify({ id: 2 })}\n`));
188+ res.end();
189+ }, RESPONSE_HEADERS_TIMEOUT_MS + 100);
190+ });
191+
192+ const result = yield* invokeWithLayer(
193+ operation,
194+ {},
195+ server.baseUrl,
196+ {},
197+ {},
198+ FetchHttpClient.layer,
199+ { responseHeadersTimeoutMs: RESPONSE_HEADERS_TIMEOUT_MS },
200+ );
201+
202+ expect(result.status).toBe(200);
203+ expect(result.data).toEqual([{ id: 1 }, { id: 2 }]);
204+ expect(result.headers["x-executor-stream"]).toBe("complete");
205+ }),
206+ );
207+});

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t