Skip to content

Commit

Permalink
add retry behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jtgi committed Jul 12, 2024
1 parent 989cb75 commit 9c13acf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
41 changes: 41 additions & 0 deletions app/lib/http.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import axiosFactory from "axios";

const retryDelay = 1000;

export const http = axiosFactory.create({
headers: {
"x-agent": "automod",
},
});

http.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
const config = err.config;

console.error({
status: err.response?.status,
data: JSON.stringify(err.response?.data),
});

if (
err.response?.status &&
(err.response.status === 429 || err.response.status >= 500) &&
!config.__retryCount
) {
config.__retryCount = 0;
}

if (config.__retryCount < 3) {
// Max retry limit
config.__retryCount += 1;
const backoffDelay = 2 ** config.__retryCount * retryDelay;
console.warn(`Received HTTP ${err.response.status}, retrying in ${backoffDelay}ms`);

return new Promise((resolve) => {
setTimeout(() => {
resolve(http(config));
}, backoffDelay);
});
}

return Promise.reject(err);
});
6 changes: 5 additions & 1 deletion app/lib/neynar.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { FollowResponseUser, Reaction } from "@neynar/nodejs-sdk/build/neynar-ap
import { CastWithInteractions, Channel, User } from "@neynar/nodejs-sdk/build/neynar-api/v2";
import axios from "axios";
import { getSetCache, getSharedEnv } from "./utils.server";
export const neynar = new NeynarAPIClient(process.env.NEYNAR_API_KEY!);
import { http } from "./http.server";

export const neynar = new NeynarAPIClient(process.env.NEYNAR_API_KEY!, {
axiosInstance: http,
});

export async function registerWebhook({ rootParentUrl }: { rootParentUrl: string }) {
const webhook = await axios.get(
Expand Down
39 changes: 1 addition & 38 deletions app/lib/warpcast.server.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,13 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
import axiosFactory from "axios";
import { Cast } from "@neynar/nodejs-sdk/build/neynar-api/v2";
import { db } from "./db.server";
import { Action, unlike } from "./validations.server";
import { neynar } from "./neynar.server";
import { getSetCache } from "./utils.server";
import { http } from "./http.server";

const token = process.env.WARPCAST_TOKEN!;
export const http = axiosFactory.create({
headers: {
"x-agent": "automod",
},
});

http.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
const config = err.config;

console.error({
status: err.response?.status,
data: JSON.stringify(err.response?.data),
});

if (
err.response?.status &&
(err.response.status === 429 || err.response.status >= 500) &&
!config.__retryCount
) {
config.__retryCount = 0;
}

if (config.__retryCount < 3) {
// Max retry limit
config.__retryCount += 1;
const backoffDelay = 2 ** config.__retryCount * 1000; // Exponential backoff
console.warn(`Received HTTP ${err.response.status}, retrying in ${backoffDelay}ms`);

return new Promise((resolve) => {
setTimeout(() => {
resolve(http(config));
}, backoffDelay);
});
}

return Promise.reject(err);
});

export async function getWarpcastChannelOwner(props: { channel: string }): Promise<number> {
const channel = await getWarpcastChannel(props);
Expand Down
2 changes: 2 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Disallow:

0 comments on commit 9c13acf

Please sign in to comment.