maxbittker
09/21/2021, 5:48 PMkenton
09/21/2021, 7:03 PMmaxbittker
09/21/2021, 7:05 PMtoinbis
09/21/2021, 7:42 PMHardAtWork
09/21/2021, 10:44 PMjohn.spurlock
09/21/2021, 10:59 PMtypescript
import { DurableObjectStub } from './deps.ts';
import { sleep } from './util.ts';
export type RetryListener = (e: Error, retries: number, maxRetries: number) => Promise<void>;
export async function durableObjectFetchWithRetries(opts: { obj: DurableObjectStub, maxRetries?: number, listener?: RetryListener }, url: RequestInfo, init?: RequestInit): Promise<Response> {
const { obj, listener } = opts;
const maxRetries = opts.maxRetries || 3;
let retries = 0;
while (true) {
try {
if (retries > 0) {
const waitMillis = retries * 1000;
await sleep(waitMillis);
}
return await obj.fetch(url, init);
} catch (e) {
if (isRetryable(e)) {
if (retries >= maxRetries) {
throw new Error(`fetchWithRetries: Out of retries (max=${maxRetries}): ${e.stack || e}`);
}
if (listener) {
await listener(e, retries, maxRetries);
}
retries++;
} else {
throw e;
}
}
}
}
//
function isRetryable(e: Error): boolean {
const error = `${e.stack || e}`;
if (error.includes('Network connection lost')) return true; // Error: Network connection lost.
return false;
}
HardAtWork
09/21/2021, 11:03 PMronan(wighawag)
09/22/2021, 8:41 AMjohn.spurlock
09/22/2021, 11:56 AMronan(wighawag)
09/22/2021, 12:30 PMronan(wighawag)
09/22/2021, 12:36 PMalbert
09/22/2021, 12:41 PMwrangler tail
. Turns out making a fetch()
request at any point while handling the request will cause logs to not appear...
js
export default {
async fetch(req, env) {
return env.LOGGER.get(env.LOGGER.newUniqueId()).fetch(req)
}
}
export class Logger {
async fetch(req) {
const url = new URL(req.url)
console.log(url.pathname)
if (url.pathname === '/fetch') {
// This causes logs to not appear
await fetch('https://www.cloudflare.com/')
}
return new Response('Hello, World!')
}
}
zifera
09/22/2021, 2:08 PMzifera
09/22/2021, 2:09 PMjohn.spurlock
09/22/2021, 2:13 PMjs
const res = await fetch('https://www.cloudflare.com/')
const txt = await res.text();
john.spurlock
09/22/2021, 2:23 PMnamespace.idFromName(new Date(timestampInMillis).toISOString().substring(0, 15))
to get a canonical name for a 10-minute period.HardAtWork
09/22/2021, 2:42 PMeidam | SuperSaaS
09/22/2021, 2:52 PMzifera
09/22/2021, 2:52 PMzifera
09/22/2021, 2:53 PMHardAtWork
09/22/2021, 2:54 PMzifera
09/22/2021, 2:55 PMHardAtWork
09/22/2021, 2:55 PMzifera
09/22/2021, 2:56 PMzifera
09/22/2021, 2:56 PMHardAtWork
09/22/2021, 2:57 PMMyDo.fetch
. Without access to MyDo
, which no one else can get, they can't access your DO.zifera
09/22/2021, 2:58 PMHardAtWork
09/22/2021, 2:58 PMzifera
09/22/2021, 2:58 PMconst id = env.DURABLE_OBJECT.idFromName('IDNAME')
const obj = env.DURABLE_OBJECT.get(id)
let resp = await obj.fetch('/internal-only')
zifera
09/22/2021, 2:59 PM