Skip to content

Commit f3629af

Browse files
committed
auto-retry packet import and export
1 parent 38f4176 commit f3629af

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

packages/core/src/v3/utils/ioSerialization.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { SemanticInternalAttributes } from "../semanticInternalAttributes.js";
44
import { TriggerTracer } from "../tracer.js";
55
import { flattenAttributes } from "./flattenAttributes.js";
66
import { apiClientManager } from "../apiClientManager-api.js";
7+
import { zodfetch } from "../zodfetch.js";
8+
import { z } from "zod";
9+
import type { RetryOptions } from "../schemas/index.js";
710

811
export type IOPacket = {
912
data?: string | undefined;
@@ -107,19 +110,34 @@ export function packetRequiresOffloading(
107110
};
108111
}
109112

113+
const ioRetryOptions = {
114+
minTimeoutInMs: 500,
115+
maxTimeoutInMs: 5000,
116+
maxAttempts: 5,
117+
factor: 2,
118+
randomize: true,
119+
} satisfies RetryOptions;
120+
110121
async function exportPacket(packet: IOPacket, pathPrefix: string): Promise<IOPacket> {
111122
// Offload the output
112123
const filename = `${pathPrefix}.${getPacketExtension(packet.dataType)}`;
113124

114125
const presignedResponse = await apiClientManager.client!.createUploadPayloadUrl(filename);
115126

116-
const uploadResponse = await fetch(presignedResponse.presignedUrl, {
117-
method: "PUT",
118-
headers: {
119-
"Content-Type": packet.dataType,
127+
const uploadResponse = await zodfetch(
128+
z.any(),
129+
presignedResponse.presignedUrl,
130+
{
131+
method: "PUT",
132+
headers: {
133+
"Content-Type": packet.dataType,
134+
},
135+
body: packet.data,
120136
},
121-
body: packet.data,
122-
});
137+
{
138+
retry: ioRetryOptions,
139+
}
140+
).asResponse();
123141

124142
if (!uploadResponse.ok) {
125143
throw new Error(
@@ -196,7 +214,9 @@ async function importPacket(packet: IOPacket, span?: Span): Promise<IOPacket> {
196214

197215
const presignedResponse = await apiClientManager.client.getPayloadUrl(packet.data);
198216

199-
const response = await fetch(presignedResponse.presignedUrl);
217+
const response = await zodfetch(z.any(), presignedResponse.presignedUrl, undefined, {
218+
retry: ioRetryOptions,
219+
}).asResponse();
200220

201221
if (!response.ok) {
202222
throw new Error(

0 commit comments

Comments
 (0)