add cluster round robining to avoid 429s

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2021-09-17 13:21:53 +02:00
parent 62c543ce35
commit f8bac6f730
1 changed files with 21 additions and 0 deletions

View File

@ -39,6 +39,27 @@ class MangoSimpleClient {
public owner: Account,
public mangoAccount: MangoAccount
) {
setInterval(this.roundRobinClusterUrl, 20_000);
}
private roundRobinClusterUrl() {
let clusterUrl =
process.env.CLUSTER_URL || "https://api.mainnet-beta.solana.com";
if (clusterUrl.includes("devnet")) {
return;
}
let possibleClustersUrls = [
"https://api.mainnet-beta.solana.com",
"https://lokidfxnwlabdq.main.genesysgo.net:8899/",
"https://solana-api.projectserum.com/"
];
clusterUrl =
possibleClustersUrls[Math.floor(Math.random() * possibleClustersUrls.length)];
logger.info(`switching to rpc node - ${clusterUrl}...`);
this.connection = new Connection(clusterUrl, "processed" as Commitment);
}
static async create() {