Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: end and reconnect pool after error #681

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/middleware/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ const {

const replicaConnections = JSON.parse(INDEXER_DB_REPLICAS);
const indexerConnection = replicaConnections[(new Date()).valueOf() % replicaConnections.length];
const pool = new Pool({ connectionString: indexerConnection, maxUses: 1000 });
let pool = new Pool({ connectionString: indexerConnection, maxUses: 1000 });

pool.on('error', (err) => {
console.error('Postgres pool error: ', err);
pool.end()
.then(() => {
pool = new Pool({ connectionString: indexerConnection, maxUses: 1000 });
})
.catch(() => {
console.error('failed to drain PG connection pool');
});
});

const poolMatch = NEAR_WALLET_ENV.startsWith('mainnet')
? JSON.stringify(['%.poolv1.near', '%.pool.near']).replace(/"/g, '\'')
: JSON.stringify(['%.pool.%.m0', '%.factory01.littlefarm.testnet', '%.factory.colorpalette.testnet']).replace(/"/g, '\'');

const findLastBlockByTimestamp = async () => {
const { rows: [ lastBlock ] } = await pool.query('select block_timestamp FROM blocks ORDER BY block_timestamp DESC LIMIT 1');
const { rows: [lastBlock] } = await pool.query('select block_timestamp FROM blocks ORDER BY block_timestamp DESC LIMIT 1');
return lastBlock;
};

Expand Down Expand Up @@ -127,7 +134,7 @@ const findReceivers = async (ctx) => {
};

const likelyTokensFromBlock = async ({ fromBlockTimestamp, accountId }) => {
const { block_timestamp: lastBlockTimestamp } = await findLastBlockByTimestamp();
const { block_timestamp: lastBlockTimestamp } = await findLastBlockByTimestamp();

const received = `
select distinct receipt_receiver_account_id as receiver_account_id
Expand Down Expand Up @@ -168,7 +175,7 @@ const likelyTokensFromBlock = async ({ fromBlockTimestamp, accountId }) => {
};

const likelyNFTsFromBlock = async ({ fromBlockTimestamp, accountId }) => {
const { block_timestamp: lastBlockTimestamp } = await findLastBlockByTimestamp();
const { block_timestamp: lastBlockTimestamp } = await findLastBlockByTimestamp();

const ownershipChangeFunctionCalls = `
select distinct receipt_receiver_account_id as receiver_account_id
Expand Down Expand Up @@ -208,7 +215,7 @@ const findLikelyTokensFromBlock = async (ctx) => {
fromBlockTimestamp,
accountId
});

ctx.body = {
version: '1.0.0',
lastBlockTimestamp,
Expand Down
Loading