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.
feat(redis): implement redis cache for arns name resolutions
- Loading branch information
dtfiedler
committed
Sep 4, 2024
1 parent
7fe02ec
commit 3e4ddaa
Showing
18 changed files
with
491 additions
and
293 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
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,35 @@ | ||
services: | ||
resolver: | ||
image: ghcr.io/ar-io/arns-resolver:${RESOLVER_IMAGE_TAG:-7fe02ecda2027e504248d3f3716579f60b561de5} | ||
build: | ||
context: . | ||
restart: on-failure | ||
ports: | ||
- ${HOST_PORT:-6000}:${CONTAINER_PORT:-6000} | ||
environment: | ||
- PORT=${CONTAINER_PORT:-6000} | ||
- LOG_LEVEL=${LOG_LEVEL:-info} | ||
- IO_PROCESS_ID=${IO_PROCESS_ID:-} | ||
- RUN_RESOLVER=${RUN_RESOLVER:-true} | ||
- EVALUATION_INTERVAL_MS=${EVALUATION_INTERVAL_MS:-} | ||
- ARNS_CACHE_TTL_MS=${RESOLVER_CACHE_TTL_MS:-} | ||
- ARNS_CACHE_PATH=${ARNS_CACHE_PATH:-./data/arns} | ||
- ARNS_CACHE_TYPE=${ARNS_CACHE_TYPE:-redis} | ||
- REDIS_CACHE_URL=${REDIS_CACHE_URL:-redis://redis:6379} | ||
- AO_CU_URL=${AO_CU_URL:-} | ||
- AO_MU_URL=${AO_MU_URL:-} | ||
- AO_GATEWAY_URL=${AO_GATEWAY_URL:-} | ||
- AO_GRAPHQL_URL=${AO_GRAPHQL_URL:-} | ||
volumes: | ||
- ${ARNS_CACHE_PATH:-./data/arns}:/app/data/arns | ||
depends_on: | ||
- redis | ||
|
||
redis: | ||
image: redis:${REDIS_IMAGE_TAG:-7} | ||
restart: on-failure | ||
ports: | ||
- 6379:6379 | ||
volumes: | ||
- ${REDIS_DATA_PATH:-./data/redis}:/data | ||
|
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
import { register } from 'node:module'; | ||
import { pathToFileURL } from 'node:url'; | ||
|
||
register('ts-node/esm', pathToFileURL('./')); |
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,69 @@ | ||
/** | ||
* 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 winston from 'winston'; | ||
|
||
import { KVBufferStore } from '../types.js'; | ||
|
||
export class ArNSStore implements KVBufferStore { | ||
private log: winston.Logger; | ||
private prefix: string; | ||
private kvStore: KVBufferStore; | ||
|
||
constructor({ | ||
log, | ||
kvStore, | ||
prefix = 'ArNS', | ||
}: { | ||
log: winston.Logger; | ||
kvStore: KVBufferStore; | ||
prefix?: string; | ||
}) { | ||
this.log = log.child({ class: this.constructor.name }); | ||
this.kvStore = kvStore; | ||
this.prefix = prefix; | ||
this.log.info('ArNSStore initialized', { | ||
prefix, | ||
kvStore: kvStore.constructor.name, | ||
}); | ||
} | ||
|
||
// avoid collisions with other redis keys | ||
private hashKey(key: string): string { | ||
return `${this.prefix}|${key}`; | ||
} | ||
|
||
async get(key: string): Promise<Buffer | undefined> { | ||
return this.kvStore.get(this.hashKey(key)); | ||
} | ||
|
||
async set(key: string, value: Buffer, ttlSeconds?: number): Promise<void> { | ||
return this.kvStore.set(this.hashKey(key), value, ttlSeconds); | ||
} | ||
|
||
async del(key: string): Promise<void> { | ||
return this.kvStore.del(this.hashKey(key)); | ||
} | ||
|
||
async has(key: string): Promise<boolean> { | ||
return this.kvStore.has(this.hashKey(key)); | ||
} | ||
|
||
async close(): Promise<void> { | ||
return this.kvStore.close(); | ||
} | ||
} |
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,80 @@ | ||
/** | ||
* 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 { RedisClientType, commandOptions, createClient } from 'redis'; | ||
import winston from 'winston'; | ||
|
||
import { KVBufferStore } from '../types.js'; | ||
|
||
export class RedisKvStore implements KVBufferStore { | ||
private client: RedisClientType; | ||
private log: winston.Logger; | ||
private defaultTtlSeconds?: number; | ||
|
||
constructor({ log, redisUrl }: { log: winston.Logger; redisUrl: string }) { | ||
this.log = log.child({ class: this.constructor.name }); | ||
this.client = createClient({ | ||
url: redisUrl, | ||
}); | ||
this.client.on('error', (error: any) => { | ||
this.log.error(`Redis error`, { | ||
message: error.message, | ||
stack: error.stack, | ||
}); | ||
// TODO: add prometheus metric for redis error | ||
}); | ||
this.client.connect().catch((error: any) => { | ||
this.log.error(`Redis connection error`, { | ||
message: error.message, | ||
stack: error.stack, | ||
}); | ||
// TODO: add prometheus metric for redis connection error | ||
}); | ||
} | ||
|
||
async close() { | ||
await this.client.quit(); | ||
} | ||
|
||
async get(key: string): Promise<Buffer | undefined> { | ||
const value = await this.client.get( | ||
commandOptions({ returnBuffers: true }), | ||
key, | ||
); | ||
return value ?? undefined; | ||
} | ||
|
||
async has(key: string): Promise<boolean> { | ||
return (await this.client.exists(key)) === 1; | ||
} | ||
|
||
async del(key: string): Promise<void> { | ||
if (await this.has(key)) { | ||
await this.client.del(key); | ||
} | ||
} | ||
|
||
async set(key: string, buffer: Buffer, ttlSeconds?: number): Promise<void> { | ||
if (ttlSeconds !== undefined) { | ||
await this.client.set(key, buffer, { | ||
EX: ttlSeconds ?? this.defaultTtlSeconds, | ||
}); | ||
} else { | ||
await this.client.set(key, buffer); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.