Skip to content

Commit

Permalink
PE-118-retry-with-promise-retry (#20)
Browse files Browse the repository at this point in the history
* faultTollerantResolve

* added retry util using promiseRetry
  • Loading branch information
flavioespinoza authored Mar 8, 2022
1 parent e02e385 commit e890b02
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 25 deletions.
20 changes: 10 additions & 10 deletions faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dotenv/config';
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import { SigningStargateClient, coins } from '@cosmjs/stargate';
import parse from 'parse-duration';
import client from "prom-client";
import client from 'prom-client';

const NETWORK_RPC_NODE = process.env.NETWORK_RPC_NODE;
const FAUCET_MNEMONIC = process.env.FAUCET_MNEMONIC;
Expand All @@ -16,16 +16,16 @@ const FAUCET_GAS = process.env.FAUCET_GAS || 180000;
const FAUCET_MEMO = process.env.FAUCET_MEMO;

const counterGetChainIdCount = new client.Counter({
name: "faucet_getChainId_count",
help: "faucet_getChainId_count is the number of times getChainId is being called",
name: 'faucet_getChainId_count',
help: 'faucet_getChainId_count is the number of times getChainId is being called',
});
const counterGetChainIdCountPost = new client.Counter({
name: "faucet_getChainId_post_count",
help: "faucet_getChainId_post_count is the number of times getChainId is being called",
name: 'faucet_getChainId_post_count',
help: 'faucet_getChainId_post_count is the number of times getChainId is being called',
});
const counterGetChainIdCountRes = new client.Counter({
name: "faucet_getChainId_res_count",
help: "faucet_getChainId_res_count is the number of times getChainId is being called",
name: 'faucet_getChainId_res_count',
help: 'faucet_getChainId_res_count is the number of times getChainId is being called',
});

export const getWallet = () => {
Expand All @@ -49,12 +49,12 @@ export const getDistributionAmount = () => {
export const getChainId = async () => {
counterGetChainIdCount.inc();
const wallet = await getWallet();
const chaiIdClient = await SigningStargateClient.connectWithSigner(
const chainIdClient = await SigningStargateClient.connectWithSigner(
NETWORK_RPC_NODE as any,
wallet
);
counterGetChainIdCountPost.inc();
const res = await chaiIdClient.getChainId();
const res = await chainIdClient.getChainId();
counterGetChainIdCountRes.inc();
return res;
};
Expand All @@ -69,7 +69,7 @@ export const sendTokens = async (recipient: any, amount: any) => {
if (!amount) {
amount = getDistributionAmount();
}

const sendMsg = {
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
value: {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "akash-faucet",
"version": "0.0.0",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./bin/www",
Expand All @@ -11,6 +11,7 @@
"@cosmjs/proto-signing": "^0.26.0",
"@cosmjs/stargate": "^0.26.0",
"@types/cors": "^2.8.12",
"@types/promise-retry": "^1.1.3",
"axios": "^0.26.0",
"cors": "^2.8.5",
"debug": "~2.6.9",
Expand All @@ -31,6 +32,7 @@
"parse-duration": "^1.0.0",
"pg": "^8.7.1",
"point-of-view": "^5.0.0",
"promise-retry": "^2.0.1",
"sequelize": "^6.6.5",
"ts-node": "^10.2.1",
"tsc": "^2.0.3",
Expand Down
17 changes: 6 additions & 11 deletions routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { latestTransactionSince } from "../database";
import * as faucet from "../faucet";
import path from "path";
import client from "prom-client";
import { retry } from "../utils";

const counterPreflight = new client.Counter({
name: "faucet_preflight_count",
Expand All @@ -16,21 +17,15 @@ const INLINE_UI = process.env.INLINE_UI;
/* GET home page. */
router.get("/", async (req: any, res: any, next: any) => {
let unlockDate;

const wallet = await faucet.getWallet();

let chainId;
try {
chainId = await faucet.getChainId();
} catch (error) {
console.log('ERROR: faucet.getChainId()', error)
}

const wallet = await retry(faucet.getWallet, "faucet.getWallet");
const chainId = await retry(faucet.getChainId, "faucet.chainId");

const distributionAmount = faucet.getDistributionAmount();
const distributionDenom = faucet.getDenom();

const [{ address }] = await wallet.getAccounts();

if (req.user && req.user.id) {
let coolDownDate = new Date(
(new Date() as any) - (faucet.getWaitPeriod() as any)
Expand Down
32 changes: 29 additions & 3 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { BlockedAddress, latestTransactionSince } from "./database";
import * as faucet from "./faucet";
import client from "prom-client";
import got from "got";
import log from "ololog";

const promiseRetry = require("promise-retry");

//AUTH0_CLIENTID
//AUTH0_CLIENTSECRET
const authCID = process.env.AUTH0_CLIENTID;
const authSECRET = process.env.AUTH0_CLIENTSECRET;
const DOMAIN = process.env.AUTH0_DOMAIN;
Expand Down Expand Up @@ -112,10 +113,35 @@ export async function blockedAddresses(req: any, res: any, next: any) {
return res.status(403).send(
JSON.stringify({
error:
"This address has been blocked from recieving funds from this faucet.",
"This address has been blocked from receiving funds from this faucet.",
})
);
}
}
next();
}


/**
* Retry
*
* @param {Function} fn - An async function
* @param {string} fname - The name of the async function
* @returns {object} The desired response object or an error
* */
export const retry = async (fn: Function, fname: string) => {
const res = await promiseRetry(
async (retry: any, number: number) => {
if (number > 1) {
log.red(`Retrying ${fname}...${number}`);
}
try {
return await fn();
} catch (error) {
await retry(error);
}
},
{ retries: 5, minTimeout: 1 * 1000, maxTimeout: 10 * 1000 }
);
return res;
};
30 changes: 30 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.52.tgz#03c13be70b9031baaed79481c0c0cfb0045e53f7"
integrity sha512-s3nugnZumCC//n4moGGe6tkNMyYEdaDBitVjwPxXmR5lnMG5dHePinH2EdxkG3Rh1ghFHHixAG4NJhpJW1rthQ==

"@types/promise-retry@^1.1.3":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@types/promise-retry/-/promise-retry-1.1.3.tgz#baab427419da9088a1d2f21bf56249c21b3dd43c"
integrity sha512-LxIlEpEX6frE3co3vCO2EUJfHIta1IOmhDlcAsR4GMMv9hev1iTI9VwberVGkePJAuLZs5rMucrV8CziCfuJMw==
dependencies:
"@types/retry" "*"

"@types/qs@*":
version "6.9.7"
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.7.tgz#63bb7d067db107cc1e457c303bc25d511febf6cb"
Expand All @@ -368,6 +375,11 @@
dependencies:
"@types/node" "*"

"@types/retry@*":
version "0.12.1"
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.1.tgz#d8f1c0d0dc23afad6dc16a9e993a0865774b4065"
integrity sha512-xoDlM2S4ortawSWORYqsdU+2rxdh4LRW9ytc3zmT37RIKQh6IHyKwwtKhKis9ah8ol07DCkZxPt8BBvPjC6v4g==

"@types/serve-static@*":
version "1.13.10"
resolved "https://registry.yarnpkg.com/@types/serve-static/-/serve-static-1.13.10.tgz#f5e0ce8797d2d7cc5ebeda48a52c96c4fa47a8d9"
Expand Down Expand Up @@ -986,6 +998,11 @@ end-of-stream@^1.1.0:
dependencies:
once "^1.4.0"

err-code@^2.0.2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/err-code/-/err-code-2.0.3.tgz#23c2f3b756ffdfc608d30e27c9a941024807e7f9"
integrity sha512-2bmlRpNKBxT/CRmPOlyISQpNj+qSeYvcym/uT0Jx2bMOlKLtSy1ZmLuVxSEKKyor/N5yhvp/ZiG1oE3DEYMSFA==

escape-goat@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/escape-goat/-/escape-goat-2.1.1.tgz#1b2dc77003676c457ec760b2dc68edb648188675"
Expand Down Expand Up @@ -2203,6 +2220,14 @@ prom-client@^13.1.0:
dependencies:
tdigest "^0.1.1"

promise-retry@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/promise-retry/-/promise-retry-2.0.1.tgz#ff747a13620ab57ba688f5fc67855410c370da22"
integrity sha512-y+WKFlBR8BGXnsNlIHFGPZmyDf3DFMoLhaflAnyZgV6rG6xu+JwesTo2Q9R6XwYmtmwAFCkAk3e35jEdoeh/3g==
dependencies:
err-code "^2.0.2"
retry "^0.12.0"

protobufjs@^6.8.8, protobufjs@~6.11.2:
version "6.11.2"
resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-6.11.2.tgz#de39fabd4ed32beaa08e9bb1e30d08544c1edf8b"
Expand Down Expand Up @@ -2402,6 +2427,11 @@ retry-as-promised@^3.2.0:
dependencies:
any-promise "^1.3.0"

retry@^0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b"
integrity sha1-G0KmJmoh8HQh0bC1S33BZ7AcATs=

reusify@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
Expand Down

0 comments on commit e890b02

Please sign in to comment.