This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(redis): modify caching in redis to protect against failed calls t…
…o AO If we have a cached value for the arns name in redis, and fail to get the updated one from AO, serve that. This should help protect downtime with AO infra between name resolutions. Also introduces prometheus and metrics for cache hits.
- Loading branch information
dtfiedler
committed
Sep 9, 2024
1 parent
1668b7e
commit 844de23
Showing
11 changed files
with
306 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"watch": ["src", "docs", ".env"], | ||
"ext": "ts,yaml,json", | ||
"exec": "ts-node --esm -r dotenv/config --project ./tsconfig.json ./src/service.ts" | ||
"exec": "NODE_OPTIONS=\"--import=./register.js\" ts-node --esm -r dotenv/config --project ./tsconfig.json ./src/service.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* AR.IO ArNS Resolver | ||
* Copyright (C) 2023 Permanent Data Solutions, Inc. All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import * as promClient from 'prom-client'; | ||
|
||
export const metrics = new promClient.Registry(); | ||
|
||
export const arnsCacheHit = new promClient.Counter({ | ||
name: 'arns_cache_hit', | ||
help: 'Number of times the ARNS cache was hit', | ||
labelNames: ['cache_type'], | ||
}); | ||
|
||
export const arnsCacheMiss = new promClient.Counter({ | ||
name: 'arns_cache_miss', | ||
help: 'Number of times the ARNS cache was missed', | ||
labelNames: ['cache_type'], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* AR.IO ArNS Resolver | ||
* Copyright (C) 2023 Permanent Data Solutions, Inc. All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { ANT, AOProcess, AoClient, AoIORead } from '@ar.io/sdk'; | ||
import { connect } from '@permaweb/aoconnect'; | ||
import winston from 'winston'; | ||
|
||
import * as config from '../config.js'; | ||
import { ArNSResolvedData } from '../types.js'; | ||
|
||
export interface NameResolver { | ||
resolve(name: string): Promise<ArNSResolvedData | undefined>; | ||
} | ||
|
||
export class ArNSResolver implements NameResolver { | ||
private ao: AoClient; | ||
private io: AoIORead; | ||
private logger: winston.Logger; | ||
|
||
constructor({ | ||
io, | ||
ao = connect({ | ||
MU_URL: config.AO_MU_URL, | ||
CU_URL: config.AO_CU_URL, | ||
GRAPHQL_URL: config.AO_GRAPHQL_URL, | ||
GATEWAY_URL: config.AO_GATEWAY_URL, | ||
}), | ||
log, | ||
}: { | ||
io: AoIORead; | ||
ao: AoClient; | ||
log: winston.Logger; | ||
}) { | ||
this.ao = ao; | ||
this.io = io; | ||
this.logger = log.child({ class: this.constructor.name }); | ||
} | ||
|
||
async resolve(name: string): Promise<ArNSResolvedData | undefined> { | ||
this.logger.debug('Resolving name', { name }); | ||
const apexName = name.split('_').slice(-1)[0]; | ||
const record = await this.io.getArNSRecord({ name: apexName }); | ||
|
||
if (!record) { | ||
this.logger.debug('No record found', { name, apexName }); | ||
return undefined; | ||
} | ||
|
||
// get the ant id and use that to get the record from the cache | ||
const antId = record.processId; | ||
const ant = ANT.init({ | ||
process: new AOProcess({ | ||
processId: antId, | ||
ao: this.ao, | ||
}), | ||
}); | ||
const undername = name.split('_').slice(0, -1).join('_') || '@'; | ||
const antRecord = await ant.getRecord({ undername }); | ||
if (!antRecord) { | ||
return undefined; | ||
} | ||
|
||
const owner = await ant.getOwner(); | ||
|
||
this.logger.debug('Resolved name', { | ||
name, | ||
antId, | ||
antRecord, | ||
owner, | ||
}); | ||
|
||
return { | ||
ttlSeconds: antRecord.ttlSeconds, | ||
txId: antRecord.transactionId, | ||
processId: antId, | ||
type: record.type, | ||
owner, | ||
}; | ||
} | ||
} |
Oops, something went wrong.