Skip to content

Commit

Permalink
add lru cache to gql token prices and fix indexer error response (#4197)
Browse files Browse the repository at this point in the history
  • Loading branch information
callensm authored Jun 20, 2023
1 parent a3f93f1 commit c6cea0e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { RESTDataSource } from "@apollo/datasource-rest";
import { LRUCache } from "lru-cache";

type CoinGeckoIndexerOptions = {
apiKey: string;
};

const IN_MEM_PRICE_DATA_CACHE = new LRUCache<string, CoinGeckoPriceData>({
allowStale: false,
max: 1000,
ttl: 1000 * 60, // 1 minute TTL
ttlAutopurge: true,
});

/**
* Custom GraphQL REST data source class abstraction for CoinGecko.
* @export
Expand All @@ -22,19 +30,34 @@ export class CoinGeckoIndexer extends RESTDataSource {

/**
* Fetches the market price data for the argued asset IDs.
* @template I
* @param {I[]} ids
* @returns {Promise<CoinGeckoGetPricesResponse<I>>}
* @param {string[]} ids
* @returns {Promise<CoinGeckoGetPricesResponse>}
* @memberof CoinGecko
*/
async getPrices(ids: string[]): Promise<CoinGeckoGetPricesResponse> {
const resp: CoinGeckoPriceData[] = await this.get("/", {
params: {
ids: ids.join(","),
},
});
const data: CoinGeckoPriceData[] = [];

const notInCache: string[] = [];
for (const i of ids) {
if (IN_MEM_PRICE_DATA_CACHE.has(i)) {
data.push(IN_MEM_PRICE_DATA_CACHE.get(i)!);
} else {
notInCache.push(i);
}
}

if (notInCache.length > 0) {
const resp: CoinGeckoPriceData[] = await this.get("/", {
params: {
ids: notInCache.join(","),
},
});

data.push(...resp);
}

return resp.reduce<CoinGeckoGetPricesResponse>((acc, curr) => {
return data.reduce<CoinGeckoGetPricesResponse>((acc, curr) => {
IN_MEM_PRICE_DATA_CACHE.set(curr.id, curr);
acc[curr.id] = curr;
return acc;
}, {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const IN_MEM_COLLECTION_DATA_CACHE = new LRUCache<
allowStale: false,
max: 1000,
ttl: 1000 * 60 * 30, // 30 minute TTL
ttlAutopurge: true,
});

type HeliusOptions = {
Expand Down
1 change: 1 addition & 0 deletions backend/native/backpack-api/src/routes/graphql/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const IN_MEM_JWT_CACHE = new LRUCache<string, string>({
allowStale: false,
max: 1000,
ttl: 1000 * 60 * 5, // 5 minute TTL
ttlAutopurge: true,
});

export interface ApiContext {
Expand Down
6 changes: 2 additions & 4 deletions backend/workers/price-indexer/src/fetched.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const fetchHandler: ExportedHandlerFetchHandler<Environment> = async (
if (!idsParameter) {
return new Response(
JSON.stringify({ error: "no asset ids found in url" }),
{ status: 400 }
{ headers: { "Content-Type": "application/json" }, status: 400 }
);
}

Expand All @@ -24,9 +24,7 @@ export const fetchHandler: ExportedHandlerFetchHandler<Environment> = async (
).filter((x) => x) as CoinGeckoPriceData[];

return new Response(JSON.stringify(values), {
headers: {
"Content-Type": "application/json",
},
headers: { "Content-Type": "application/json" },
status: 200,
});
};

1 comment on commit c6cea0e

@vercel
Copy link

@vercel vercel bot commented on c6cea0e Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.