Skip to content

Commit

Permalink
Add in-memory did cache to Ozone backend (bluesky-social#2635)
Browse files Browse the repository at this point in the history
* add ozone in-memory did cache

* changeset
  • Loading branch information
dholms authored Jul 10, 2024
1 parent 7761a46 commit 2f40203
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/two-wombats-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto/ozone": patch
---

Add in-memory did cachee
5 changes: 5 additions & 0 deletions packages/ozone/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert'
import { OzoneEnvironment } from './env'
import { DAY, HOUR } from '@atproto/common'

// off-config but still from env:
// logging: LOG_LEVEL, LOG_SYSTEMS, LOG_ENABLED, LOG_DESTINATION
Expand Down Expand Up @@ -60,6 +61,8 @@ export const envToCfg = (env: OzoneEnvironment): OzoneConfig => {
assert(env.didPlcUrl, 'didPlcUrl is required')
const identityCfg: OzoneConfig['identity'] = {
plcUrl: env.didPlcUrl,
cacheMaxTTL: env.didCacheMaxTTL ?? DAY,
cacheStaleTTL: env.didCacheStaleTTL ?? HOUR,
}

const blobDivertServiceCfg =
Expand Down Expand Up @@ -143,6 +146,8 @@ export type CdnConfig = {

export type IdentityConfig = {
plcUrl: string
cacheStaleTTL: number
cacheMaxTTL: number
}

export type AccessConfig = {
Expand Down
4 changes: 4 additions & 0 deletions packages/ozone/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const readEnv = (): OzoneEnvironment => {
dbPoolMaxUses: envInt('OZONE_DB_POOL_MAX_USES'),
dbPoolIdleTimeoutMs: envInt('OZONE_DB_POOL_IDLE_TIMEOUT_MS'),
didPlcUrl: envStr('OZONE_DID_PLC_URL'),
didCacheStaleTTL: envInt('OZONE_DID_CACHE_STALE_TTL'),
didCacheMaxTTL: envInt('OZONE_DID_CACHE_MAX_TTL'),
cdnPaths: envList('OZONE_CDN_PATHS'),
adminDids: envList('OZONE_ADMIN_DIDS'),
moderatorDids: envList('OZONE_MODERATOR_DIDS'),
Expand Down Expand Up @@ -52,6 +54,8 @@ export type OzoneEnvironment = {
dbPoolMaxUses?: number
dbPoolIdleTimeoutMs?: number
didPlcUrl?: string
didCacheStaleTTL?: number
didCacheMaxTTL?: number
cdnPaths?: string[]
adminDids: string[]
moderatorDids: string[]
Expand Down
13 changes: 12 additions & 1 deletion packages/ozone/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express'
import * as plc from '@did-plc/lib'
import { IdResolver } from '@atproto/identity'
import { DidCache, IdResolver, MemoryCache } from '@atproto/identity'
import { AtpAgent } from '@atproto/api'
import { Keypair, Secp256k1Keypair } from '@atproto/crypto'
import { createServiceAuthHeaders } from '@atproto/xrpc-server'
Expand Down Expand Up @@ -39,6 +39,7 @@ export type AppContextOptions = {
blobDiverter?: BlobDiverter
signingKey: Keypair
signingKeyId: number
didCache: DidCache
idResolver: IdResolver
imgInvalidator?: ImageInvalidator
backgroundQueue: BackgroundQueue
Expand Down Expand Up @@ -74,8 +75,13 @@ export class AppContext {
? new AtpAgent({ service: cfg.chat.url })
: undefined

const didCache = new MemoryCache(
cfg.identity.cacheStaleTTL,
cfg.identity.cacheMaxTTL,
)
const idResolver = new IdResolver({
plcUrl: cfg.identity.plcUrl,
didCache,
})

const createAuthHeaders = (aud: string) =>
Expand Down Expand Up @@ -131,6 +137,7 @@ export class AppContext {
chatAgent,
signingKey,
signingKeyId,
didCache,
idResolver,
backgroundQueue,
sequencer,
Expand Down Expand Up @@ -198,6 +205,10 @@ export class AppContext {
return new plc.Client(this.cfg.identity.plcUrl)
}

get didCache(): DidCache {
return this.opts.didCache
}

get idResolver(): IdResolver {
return this.opts.idResolver
}
Expand Down

0 comments on commit 2f40203

Please sign in to comment.