Skip to content

Commit

Permalink
Merge pull request #1837 from bluesky-social/no-cache-actor-store
Browse files Browse the repository at this point in the history
No cache actor store
  • Loading branch information
dholms authored Nov 8, 2023
2 parents b586b4a + 5c225a8 commit 786d4e0
Show file tree
Hide file tree
Showing 21 changed files with 238 additions and 255 deletions.
45 changes: 1 addition & 44 deletions packages/bsky/src/util/retry.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
import { AxiosError } from 'axios'
import { wait } from '@atproto/common'
import { XRPCError, ResponseType } from '@atproto/xrpc'

export async function retry<T>(
fn: () => Promise<T>,
opts: RetryOptions = {},
): Promise<T> {
const { max = 3, retryable = () => true } = opts
let retries = 0
let doneError: unknown
while (!doneError) {
try {
if (retries) await backoff(retries)
return await fn()
} catch (err) {
const willRetry = retries < max && retryable(err)
if (!willRetry) doneError = err
retries += 1
}
}
throw doneError
}
import { RetryOptions, retry } from '@atproto/common'

export async function retryHttp<T>(
fn: () => Promise<T>,
Expand All @@ -44,26 +24,3 @@ export function retryableHttp(err: unknown) {
const retryableHttpStatusCodes = new Set([
408, 425, 429, 500, 502, 503, 504, 522, 524,
])

type RetryOptions = {
max?: number
retryable?: (err: unknown) => boolean
}

// Waits exponential backoff with max and jitter: ~50, ~100, ~200, ~400, ~800, ~1000, ~1000, ...
async function backoff(n: number, multiplier = 50, max = 1000) {
const exponentialMs = Math.pow(2, n) * multiplier
const ms = Math.min(exponentialMs, max)
await wait(jitter(ms))
}

// Adds randomness +/-15% of value
function jitter(value: number) {
const delta = value * 0.15
return value + randomRange(-delta, delta)
}

function randomRange(from: number, to: number) {
const rand = Math.random() * (to - from)
return rand + from
}
28 changes: 16 additions & 12 deletions packages/bsky/tests/auto-moderator/takedowns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,15 @@ describe('takedowner', () => {
.executeTakeFirst()
expect(record?.takedownId).toEqual(modAction.id)

const actorDb = await network.pds.ctx.actorStore.db(post.ref.uri.hostname)
const recordPds = await actorDb.db
.selectFrom('record')
.where('uri', '=', post.ref.uriStr)
.select('takedownRef')
.executeTakeFirst()
const recordPds = await network.pds.ctx.actorStore.read(
post.ref.uri.hostname,
(store) =>
store.db.db
.selectFrom('record')
.where('uri', '=', post.ref.uriStr)
.select('takedownRef')
.executeTakeFirst(),
)
expect(recordPds?.takedownRef).toEqual(modAction.id.toString())

expect(testInvalidator.invalidated.length).toBe(1)
Expand Down Expand Up @@ -136,12 +139,13 @@ describe('takedowner', () => {
.executeTakeFirst()
expect(record?.takedownId).toEqual(modAction.id)

const actorDb = await network.pds.ctx.actorStore.db(alice)
const recordPds = await actorDb.db
.selectFrom('record')
.where('uri', '=', res.data.uri)
.select('takedownRef')
.executeTakeFirst()
const recordPds = await network.pds.ctx.actorStore.read(alice, (store) =>
store.db.db
.selectFrom('record')
.where('uri', '=', res.data.uri)
.select('takedownRef')
.executeTakeFirst(),
)
expect(recordPds?.takedownRef).toEqual(modAction.id.toString())

expect(testInvalidator.invalidated.length).toBe(2)
Expand Down
1 change: 1 addition & 0 deletions packages/common-web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './async'
export * from './util'
export * from './tid'
export * from './ipld'
export * from './retry'
export * from './types'
export * from './times'
export * from './strings'
Expand Down
47 changes: 47 additions & 0 deletions packages/common-web/src/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { wait } from './util'

export type RetryOptions = {
maxRetries?: number
backoffMultiplier?: number
backoffMax?: number
retryable?: (err: unknown) => boolean
}

export async function retry<T>(
fn: () => Promise<T>,
opts: RetryOptions = {},
): Promise<T> {
const { maxRetries = 3, retryable = () => true } = opts
let retries = 0
let doneError: unknown
while (!doneError) {
try {
if (retries)
await backoff(retries, opts.backoffMultiplier, opts.backoffMax)
return await fn()
} catch (err) {
const willRetry = retries < maxRetries && retryable(err)
if (!willRetry) doneError = err
retries += 1
}
}
throw doneError
}

// Waits exponential backoff with max and jitter: ~50, ~100, ~200, ~400, ~800, ~1000, ~1000, ...
async function backoff(n: number, multiplier = 50, max = 1000) {
const exponentialMs = Math.pow(2, n) * multiplier
const ms = Math.min(exponentialMs, max)
await wait(jitter(ms))
}

// Adds randomness +/-15% of value
function jitter(value: number) {
const delta = value * 0.15
return value + randomRange(-delta, delta)
}

function randomRange(from: number, to: number) {
const rand = Math.random() * (to - from)
return rand + from
}
1 change: 0 additions & 1 deletion packages/pds/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"jose": "^5.0.1",
"key-encoder": "^2.0.3",
"kysely": "^0.22.0",
"lru-cache": "^10.0.1",
"multiformats": "^9.9.0",
"nodemailer": "^6.8.0",
"nodemailer-html-to-text": "^3.2.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/pds/src/account-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class AccountManager {
await getMigrator(this.db).migrateToLatestOrThrow()
}

async close() {
await this.db.close()
close() {
this.db.close()
}

// Account
Expand Down
Loading

0 comments on commit 786d4e0

Please sign in to comment.