Skip to content

Commit

Permalink
mutex on txinfo to enforce cache hits
Browse files Browse the repository at this point in the history
  • Loading branch information
Strernd committed Oct 30, 2020
1 parent cfb71a9 commit a0c306d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@types/bs58": "^4.0.1",
"apex-logs-winston": "^1.1.0",
"async-mutex": "^0.2.4",
"axios": "^0.20.0",
"bitcoinjs-lib": "^5.1.10",
"bitcoinjs-message": "^2.1.3",
Expand Down
40 changes: 29 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FullServiceHandlers, AssetTransactionData, TransactionInformation } fro
import { BcoinHandlers, BlockcyperHandlers } from './implementations';
import { configureLogger, logger } from './log';
import NodeCache from 'node-cache';
import { Mutex } from 'async-mutex';
require('dotenv').config();

if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'local_with_logger') {
Expand Down Expand Up @@ -40,23 +41,40 @@ switch (process.env.SOURCE) {
logger.info('USING BCOIN');
handlers = BcoinHandlers;
}

const mx = new Mutex();
const txInfoCache = new NodeCache({ stdTTL: 10, checkperiod: 1 });

async function getTransactionInfoResult(reference: string, poolAddress: string) {
const release = await mx.acquire();
try {
const key = `${reference}-${poolAddress}`;
const fromCache: TransactionInformation | undefined = txInfoCache.get(key);
if (fromCache) {
logger.info('delivering from cache', { key });
return fromCache;
} else {
logger.info('no cache hit', { key });
const result = await handlers.oracle.getTransactionInformation(reference as string, poolAddress as string);
txInfoCache.set(key, result);
return result;
}
} catch (error) {
logger.error('Error getTransactionInfoResult', { error });
return null;
} finally {
release();
}
}

app.get('/oracle/transactionInfo', async (req, res) => {
const { reference, poolAddress } = req.query;
logger.info('Called /oracle/transactionInfo', { reference, poolAddress });
if (!reference || !poolAddress) return res.status(400).json({ status: 'MISSING_PARAMS' });
const key = `${reference}-${poolAddress}`;
const fromCache: TransactionInformation | undefined = txInfoCache.get(key);
if (fromCache) {
logger.info('delivering from cache', { key });
return res.json(fromCache);
} else {
logger.info('no cache hit', { key });
const result = await handlers.oracle.getTransactionInformation(reference as string, poolAddress as string);
txInfoCache.set(key, result);
return res.json(result);
const result = await getTransactionInfoResult(reference as string, poolAddress as string);
if (result === null) {
res.status(500);
}
return res.json(result);
});

app.post('/oracle/validateSignature', async (req, res) => {
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,13 @@ async-each@^1.0.1:
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.3.tgz#b727dbf87d7651602f06f4d4ac387f47d91b0cbf"
integrity sha512-z/WhQ5FPySLdvREByI2vZiTWwCnF0moMJ1hK9YQwDTHKh6I7/uSckMetoRGb5UBZPC1z0jlw+n/XCgjeH7y1AQ==

async-mutex@^0.2.4:
version "0.2.4"
resolved "https://registry.yarnpkg.com/async-mutex/-/async-mutex-0.2.4.tgz#f6ea5f9cc73147f395f86fa573a2af039fe63082"
integrity sha512-fcQKOXUKMQc57JlmjBCHtkKNrfGpHyR7vu18RfuLfeTAf4hK9PgOadPR5cDrBQ682zasrLUhJFe7EKAHJOduDg==
dependencies:
tslib "^2.0.0"

[email protected]:
version "0.2.9"
resolved "https://registry.yarnpkg.com/async/-/async-0.2.9.tgz#df63060fbf3d33286a76aaf6d55a2986d9ff8619"
Expand Down Expand Up @@ -3938,6 +3945,11 @@ tsconfig-paths@^3.9.0:
minimist "^1.2.0"
strip-bom "^3.0.0"

tslib@^2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.0.3.tgz#8e0741ac45fc0c226e58a17bfc3e64b9bc6ca61c"
integrity sha512-uZtkfKblCEQtZKBF6EBXVZeQNl82yqtDQdv+eck8u7tdPxjLu2/lp5/uPW+um2tpuxINHWy3GhiccY7QgEaVHQ==

type-check@^0.4.0, type-check@~0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"
Expand Down

0 comments on commit a0c306d

Please sign in to comment.