Skip to content

Commit

Permalink
⚡️ review a bit load balancing
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Jul 9, 2024
1 parent d31d249 commit 6d1f969
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions ponder.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { multiWebAuthNValidatorV2Abi } from "./abis/multiWebAuthNValidatorABI";
*/
export const loadBalance = (_transports: Transport[]): Transport => {
const fallbackTransport = fallback(_transports);
let currentIndex = 0;

return ({ chain, retryCount, timeout }) => {
const fallback = fallbackTransport({ chain, retryCount, timeout });
Expand All @@ -39,21 +40,20 @@ export const loadBalance = (_transports: Transport[]): Transport => {
key: "loadBalance",
name: "Load Balance",
request: async (body) => {
// Random between 0 and transports.length
const index = Math.round(Math.random() * transports.length);
// Increase current index for the next request
currentIndex++;
if (currentIndex === transports.length) currentIndex = 0;

// Perform the request
try {
const response = await transports[index]?.request(body);
const response =
await transports[currentIndex]?.request(body);
// If we got a response return it directly
if (response) {
return response;
}
} catch (e) {
console.error(
"Error when using load balanced transport",
e
);
} finally {
// do nothing
}
// If we arrived here, return a stuff via the fallback transport
return fallback.request(body);
Expand All @@ -66,8 +66,8 @@ export const loadBalance = (_transports: Transport[]): Transport => {
};

const pollingConfig = {
pollingInterval: 30_000,
maxRequestsPerSecond: 4,
pollingInterval: 20_000,
maxRequestsPerSecond: 16,
} as const;

export default createConfig({
Expand Down

0 comments on commit 6d1f969

Please sign in to comment.