Skip to content

Commit

Permalink
feat(arns): allow operator to override arns ttls and force refreshes …
Browse files Browse the repository at this point in the history
…based on env variable

Intrdocues `ARNS_RESOLVER_OVERRIDE_TTL_SECONDS` env variable which can be used to override when to refresh arns names. If set to `0` the resolver will always attempt to fetch the latest resolution data from the resolvers.
  • Loading branch information
dtfiedler committed Sep 26, 2024
1 parent e902291 commit 9c8d533
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ export const ARNS_CACHE_TTL_SECONDS = +env.varOrDefault(
`${60 * 60}`, // 1 hour
);

export const ARNS_RESOLVER_OVERRIDE_TTL_SECONDS_STRING = env.varOrUndefined(
'ARNS_RESOLVER_OVERRIDE_TTL_SECONDS',
);

export const ARNS_RESOLVER_OVERRIDE_TTL_SECONDS =
ARNS_RESOLVER_OVERRIDE_TTL_SECONDS_STRING !== undefined
? +ARNS_RESOLVER_OVERRIDE_TTL_SECONDS_STRING
: undefined;

export const ARNS_CACHE_MAX_KEYS = +env.varOrDefault(
'ARNS_CACHE_MAX_KEYS',
'10000',
Expand Down
5 changes: 5 additions & 0 deletions src/init/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ export const createArNSResolver = ({
resolutionOrder,
trustedGatewayUrl,
networkProcess,
overrides,
}: {
log: Logger;
cache: KvArnsStore;
resolutionOrder: (ArNSResolverType | string)[];
trustedGatewayUrl?: string;
networkProcess?: AoIORead;
overrides?: {
ttlSeconds?: number;
};
}): NameResolver => {
const resolverMap: Record<ArNSResolverType, NameResolver | undefined> = {
'on-demand': new OnDemandArNSResolver({
Expand Down Expand Up @@ -113,5 +117,6 @@ export const createArNSResolver = ({
log,
resolvers,
cache,
overrides,
});
};
19 changes: 16 additions & 3 deletions src/resolution/composite-arns-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,34 @@ export class CompositeArNSResolver implements NameResolver {
private log: winston.Logger;
private resolvers: NameResolver[];
private cache: KvArnsStore;
private overrides:
| {
ttlSeconds?: number;
// TODO: other overrides like fallback txId if not found in resolution
}
| undefined;

constructor({
log,
resolvers,
cache,
overrides,
}: {
log: winston.Logger;
resolvers: NameResolver[];
cache: KvArnsStore;
overrides?: {
ttlSeconds?: number;
};
}) {
this.log = log.child({ class: this.constructor.name });
this.resolvers = resolvers;
this.cache = cache;
this.overrides = overrides;
}

async resolve(name: string): Promise<NameResolution> {
this.log.info('Resolving name...', { name });
this.log.info('Resolving name...', { name, overrides: this.overrides });
let resolution: NameResolution | undefined;

try {
Expand All @@ -50,11 +61,13 @@ export class CompositeArNSResolver implements NameResolver {
cachedResolutionBuffer.toString(),
);
resolution = cachedResolution; // hold on to this in case we need it
// use the override ttl if it exists, otherwise use the cached resolution ttl
const ttlSeconds = this.overrides?.ttlSeconds ?? cachedResolution.ttl;
if (
cachedResolution !== undefined &&
cachedResolution.resolvedAt !== undefined &&
cachedResolution.ttl !== undefined &&
cachedResolution.resolvedAt + cachedResolution.ttl * 1000 > Date.now()
ttlSeconds !== undefined &&
cachedResolution.resolvedAt + ttlSeconds * 1000 > Date.now()
) {
metrics.arnsCacheHitCounter.inc();
this.log.info('Cache hit for arns name', { name });
Expand Down
4 changes: 4 additions & 0 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,10 @@ export const nameResolver = createArNSResolver({
resolutionOrder: config.ARNS_RESOLVER_PRIORITY_ORDER,
networkProcess: arIO,
cache: arnsResolverCache,
overrides: {
ttlSeconds: config.ARNS_RESOLVER_OVERRIDE_TTL_SECONDS,
// TODO: other overrides like fallback txId if not found in resolution
},
});

const webhookEmitter = new WebhookEmitter({
Expand Down

0 comments on commit 9c8d533

Please sign in to comment.