Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
chore: testing an updating promise cache that does not away on puts
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Sep 19, 2023
1 parent 6c3d913 commit 3798611
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 58 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"prepare": "husky install"
},
"dependencies": {
"@ardrive/ardrive-promise-cache": "^1.1.3",
"@ardrive/ardrive-promise-cache": "file:../../../ardrive/promise-cache",
"@commitlint/config-conventional": "^17.7.0",
"@koa/cors": "^4.0.0",
"@koa/router": "^12.0.0",
Expand Down
71 changes: 18 additions & 53 deletions src/api/warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import {
EvaluationOptions,
Warp,
} from 'warp-contracts';
import {
DEFAULT_EVALUATION_OPTIONS,
EVALUATION_TIMEOUT_MS,
allowedContractTypes,
} from '../constants';
import { EVALUATION_TIMEOUT_MS, allowedContractTypes } from '../constants';
import { ContractType } from '../types';
import * as _ from 'lodash';
import { EvaluationTimeoutError } from '../errors';
Expand Down Expand Up @@ -61,7 +57,7 @@ const contractStateCache: ReadThroughPromiseCache<
cacheCapacity: 100,
cacheTTL: 1000 * 30, // 30 seconds
},
readThroughFunction: (cacheKey) => readThroughToContractState(cacheKey),
readThroughFunction: readThroughToContractState,
});

// Convenience class for read through caching
Expand All @@ -87,7 +83,7 @@ const contractManifestCache: ReadThroughPromiseCache<
cacheCapacity: 1000,
cacheTTL: 1000 * 60 * 60 * 24 * 365, // 365 days - effectively permanent
},
readThroughFunction: (cacheKey) => readThroughToContractManifest(cacheKey),
readThroughFunction: readThroughToContractManifest,
});

function createQueryParamHash(evalOptions: Partial<EvaluationOptions>): string {
Expand Down Expand Up @@ -120,13 +116,13 @@ async function readThroughToContractState(
...cachedValue,
evaluationOptions,
};
} else {
logger?.debug('Evaluating contract state...', {
contractTxId,
cacheKey: cacheKey.toString(),
});
}

logger?.debug('Evaluating contract state...', {
contractTxId,
cacheKey: cacheKey.toString(),
});

// use the combined evaluation options
const contract = warp
.contract(contractTxId)
Expand All @@ -137,14 +133,17 @@ async function readThroughToContractState(
requestMap.set(cacheId, readStatePromise);

readStatePromise
.catch((error) => {
.catch((error: unknown) => {
logger?.debug('Failed to evaluate contract state!', {
contractTxId,
cacheKey: cacheKey.toString(),
error,
error: error instanceof Error ? error.message : 'Unknown error',
});
})
.finally(() => {
logger?.debug('Removing cached rqeuest from barrier map.', {
cacheId,
});
// remove the cached request whether it completes or fails
requestMap.delete(cacheId);
});
Expand All @@ -166,36 +165,20 @@ async function readThroughToContractState(
export async function getContractState({
contractTxId,
warp,
evaluationOptionOverrides = DEFAULT_EVALUATION_OPTIONS,
logger,
}: {
contractTxId: string;
warp: Warp;
evaluationOptionOverrides?: Partial<EvaluationOptions>;
logger: winston.Logger;
}): Promise<EvaluatedContractState> {
try {
// get the contract manifest eval options by default
const { evaluationOptions: contractDefinedEvalOptions } =
await getContractManifest({
contractTxId,
arweave: warp.arweave,
logger,
});
// override any contract manifest eval options with eval options provided
const combinedEvalOptions = {
...contractDefinedEvalOptions,
...evaluationOptionOverrides,
};

const { evaluationOptions } = await contractManifestCache.get(
new ContractManifestCacheKey(contractTxId, warp.arweave, logger),
);
// Awaiting here so that promise rejection can be caught below, wrapped, and propagated
return await contractStateCache.get(
new ContractStateCacheKey(
contractTxId,
combinedEvalOptions,
warp,
logger,
),
new ContractStateCacheKey(contractTxId, evaluationOptions, warp, logger),
);
} catch (error) {
// throw an eval here so we can properly return correct status code
Expand All @@ -215,11 +198,7 @@ async function readThroughToContractManifest({
contractTxId,
arweave,
logger,
}: {
contractTxId: string;
arweave: Arweave;
logger?: winston.Logger;
}): Promise<EvaluationManifest> {
}: ContractManifestCacheKey): Promise<EvaluationManifest> {
logger?.debug('Reading through to contract manifest...', {
contractTxId,
});
Expand All @@ -231,20 +210,6 @@ async function readThroughToContractManifest({
return contractManifest;
}

export async function getContractManifest({
contractTxId,
arweave,
logger,
}: {
contractTxId: string;
arweave: Arweave;
logger?: winston.Logger;
}): Promise<EvaluationManifest> {
return contractManifestCache.get(
new ContractManifestCacheKey(contractTxId, arweave, logger),
);
}

export function tagsToObject(tags: Tag[]): {
[x: string]: string;
} {
Expand Down
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const errorCounter = new promClient.Counter({

// catch any floating errors, swallow them and increment prometheus counter
process.on('uncaughtException', (err) => {
logger.error(err);
logger.error('Uncaught exception!', err);
errorCounter.inc();
});

Expand Down
4 changes: 1 addition & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,8 @@
dependencies:
xss "^1.0.8"

"@ardrive/ardrive-promise-cache@^1.1.3":
"@ardrive/ardrive-promise-cache@file:../../../ardrive/promise-cache":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@ardrive/ardrive-promise-cache/-/ardrive-promise-cache-1.1.3.tgz#93dc04f54bcccfc90b97d9aeb1095bb6374eacc8"
integrity sha512-jPFlnVUlHcW+LEptyUyXl2oZ6JZeuWTycZ/tfOWxXL9go+0icUHUWibIvJAuqeR4EjBFsT953BOibplPsZ/b1w==
dependencies:
"@alexsasharegan/simple-cache" "^3.3.3"

Expand Down

0 comments on commit 3798611

Please sign in to comment.