Skip to content

Commit

Permalink
Avoid un-necessarily loading the keypair when creating an ActorStoreR…
Browse files Browse the repository at this point in the history
…eader (#2319)

perf(pds): avoid un-necessarily loading the keypair when creating an ActorStoreReader
  • Loading branch information
matthieusieben authored Mar 20, 2024
1 parent faa8d2f commit c8a5fb4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
2 changes: 2 additions & 0 deletions packages/pds/src/actor-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ const createActorReader = (
repo: new RepoReader(db, blobstore(did)),
record: new RecordReader(db),
pref: new PreferenceReader(db),
keypair: getKeypair,
transact: async <T>(fn: ActorStoreTransactFn<T>): Promise<T> => {
const keypair = await getKeypair()
return db.transaction((dbTxn) => {
Expand All @@ -276,6 +277,7 @@ export type ActorStoreReader = {
repo: RepoReader
record: RecordReader
pref: PreferenceReader
keypair: () => Promise<Keypair>
transact: <T>(fn: ActorStoreTransactFn<T>) => Promise<T>
}

Expand Down
3 changes: 1 addition & 2 deletions packages/pds/src/api/app/bsky/feed/getPostThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ export default function (server: Server, ctx: AppContext) {
} catch (err) {
if (err instanceof XRPCError && err.error === 'NotFound') {
const headers = err.headers
const keypair = await ctx.actorStore.keypair(requester)
const local = await ctx.actorStore.read(requester, (store) => {
const localViewer = ctx.localViewer(store, keypair)
const localViewer = ctx.localViewer(store)
return readAfterWriteNotFound(
ctx,
localViewer,
Expand Down
14 changes: 4 additions & 10 deletions packages/pds/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ import { DidSqliteCache } from './did-cache'
import { Crawlers } from './crawlers'
import { DiskBlobStore } from './disk-blobstore'
import { getRedisClient } from './redis'
import { ActorStore, ActorStoreReader } from './actor-store'
import { LocalViewer } from './read-after-write/viewer'
import { ActorStore } from './actor-store'
import { LocalViewer, LocalViewerCreator } from './read-after-write/viewer'

export type AppContextOptions = {
actorStore: ActorStore
blobstore: (did: string) => BlobStore
localViewer: (
actorStore: ActorStoreReader,
actorKey: crypto.Keypair,
) => LocalViewer
localViewer: LocalViewerCreator
mailer: ServerMailer
moderationMailer: ModerationMailer
didCache: DidSqliteCache
Expand All @@ -55,10 +52,7 @@ export type AppContextOptions = {
export class AppContext {
public actorStore: ActorStore
public blobstore: (did: string) => BlobStore
public localViewer: (
actorStore: ActorStoreReader,
actorKey: crypto.Keypair,
) => LocalViewer
public localViewer: LocalViewerCreator
public mailer: ServerMailer
public moderationMailer: ModerationMailer
public didCache: DidSqliteCache
Expand Down
3 changes: 1 addition & 2 deletions packages/pds/src/read-after-write/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ export const readAfterWriteInternal = async <T>(
if (local.count === 0) {
return res
}
const keypair = await ctx.actorStore.keypair(requester)
const localViewer = ctx.localViewer(store, keypair)
const localViewer = ctx.localViewer(store)
const parsedRes = parseRes<T>(nsid, res)
const data = await munge(localViewer, parsedRes, local, requester)
return formatMungedResponse(data, getLocalLag(local))
Expand Down
16 changes: 8 additions & 8 deletions packages/pds/src/read-after-write/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { CID } from 'multiformats/cid'
import { AtUri, INVALID_HANDLE } from '@atproto/syntax'
import { cborToLexRecord } from '@atproto/repo'
import { AtpAgent } from '@atproto/api'
import { Keypair } from '@atproto/crypto'
import { createServiceAuthHeaders } from '@atproto/xrpc-server'
import { Record as PostRecord } from '../lexicon/types/app/bsky/feed/post'
import { Record as ProfileRecord } from '../lexicon/types/app/bsky/actor/profile'
Expand Down Expand Up @@ -43,10 +42,11 @@ import { AccountManager } from '../account-manager'

type CommonSignedUris = 'avatar' | 'banner' | 'feed_thumbnail' | 'feed_fullsize'

export type LocalViewerCreator = (actorStore: ActorStoreReader) => LocalViewer

export class LocalViewer {
did: string
actorStore: ActorStoreReader
actorKey: Keypair
accountManager: AccountManager
pdsHostname: string
appViewAgent?: AtpAgent
Expand All @@ -55,7 +55,6 @@ export class LocalViewer {

constructor(params: {
actorStore: ActorStoreReader
actorKey: Keypair
accountManager: AccountManager
pdsHostname: string
appViewAgent?: AtpAgent
Expand All @@ -64,7 +63,6 @@ export class LocalViewer {
}) {
this.did = params.actorStore.did
this.actorStore = params.actorStore
this.actorKey = params.actorKey
this.accountManager = params.accountManager
this.pdsHostname = params.pdsHostname
this.appViewAgent = params.appViewAgent
Expand All @@ -78,9 +76,9 @@ export class LocalViewer {
appViewAgent?: AtpAgent
appviewDid?: string
appviewCdnUrlPattern?: string
}) {
return (actorStore: ActorStoreReader, actorKey: Keypair) => {
return new LocalViewer({ ...params, actorStore, actorKey })
}): LocalViewerCreator {
return (actorStore) => {
return new LocalViewer({ ...params, actorStore })
}
}

Expand All @@ -95,10 +93,12 @@ export class LocalViewer {
if (!this.appviewDid) {
throw new Error('Could not find bsky appview did')
}
const keypair = await this.actorStore.keypair()

return createServiceAuthHeaders({
iss: did,
aud: this.appviewDid,
keypair: this.actorKey,
keypair,
})
}

Expand Down

0 comments on commit c8a5fb4

Please sign in to comment.