Skip to content

Commit

Permalink
improve(api): increase highWaterMark for tunnel stream
Browse files Browse the repository at this point in the history
  • Loading branch information
sdnts committed Oct 7, 2023
1 parent 46e5253 commit 82f332f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
26 changes: 21 additions & 5 deletions packages/api/src/tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ export class Tunnel implements DurableObject {
constructor(private state: DurableObjectState) {}

async fetch(request: Request): Promise<Response> {
try {
return this.handler(request);
} catch (e) {
console.error({ error: (e as Error).message }, "Uncaught error");
return error(500, "Internal error");
}
}

async handler(request: Request): Promise<Response> {
const url = new URL(request.url);

const tunnelId = this.state.id.toString();
Expand Down Expand Up @@ -108,7 +117,7 @@ export class Tunnel implements DurableObject {
// isn't trivial, and I'd rather have _something_ working.
// Returning a Promise here is how we signal to the ReadableStream to
// only have one running `pull` at a time.
return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
const listeners = new AbortController();

uploader.addEventListener(
Expand All @@ -119,6 +128,7 @@ export class Tunnel implements DurableObject {
"Blob transfer complete"
);
controller.close();

listeners.abort();
return resolve();
},
Expand All @@ -137,7 +147,6 @@ export class Tunnel implements DurableObject {

console.debug("Enqueueing", e.data.byteLength);
controller.enqueue(new Uint8Array(e.data));
console.debug("Enqueued", e.data.byteLength);

listeners.abort();
return resolve();
Expand All @@ -146,8 +155,15 @@ export class Tunnel implements DurableObject {
);

// Request a chunk
console.debug("Requesting chunk");
uploader.send("");
}).catch((e) => {
console.error(
{ error: (e as Error).message },
"Pull Promise rejection"
);

controller.error(1001);
return Promise.reject(1001);
});
},
cancel(reason) {
Expand All @@ -158,7 +174,7 @@ export class Tunnel implements DurableObject {
{
// Queue upto 10 chunks in memory if the downloader is
// being slow. This roughly translates to 10MiB
highWaterMark: 1,
highWaterMark: 10,
size: (chunk) => chunk.byteLength,
}
);
Expand All @@ -183,7 +199,7 @@ export class Tunnel implements DurableObject {
"Content-Type": type,
// Technically always incorrect because of gzip, but Chromium treats this
// as a hint to show a real progress bar for this download
"Content-Length": size,
// "Content-Length": size,
"Content-Disposition": `attachment; filename=\"${filename}\"`,
"Content-Encoding": "gzip",
},
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/TunnelPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const TunnelPage = () => {
console.log("Files dropped", files);
// TODO: Should I create a thread per upload?
files.map((f) => tunnel().then((tunnelId) => upload(tunnelId, f)));
fileInput.current!.files = null;
};

useEffect(() => {
Expand Down

0 comments on commit 82f332f

Please sign in to comment.