Skip to content

Commit

Permalink
feat: 再送制御を賢く
Browse files Browse the repository at this point in the history
  • Loading branch information
rokoucha committed Oct 5, 2021
1 parent 680c6d4 commit 0ad30c7
Showing 1 changed file with 51 additions and 28 deletions.
79 changes: 51 additions & 28 deletions src/lib/futaba/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { dirname, join } from 'path'
import { sleep } from '../../utils'
import { Threads, threadSchema, threadsSchema } from './schema'

export const FETCH_INTERVAL_MS = 4000 as const // 操作が早すぎます あと2秒で再送できます
export const FETCH_INTERVAL_MS = 3000 as const
export const RETRY_MAX = 5 as const

export type { Response, Thread, Threads } from './schema'

Expand Down Expand Up @@ -53,37 +54,75 @@ export class FutabaClient {

const matches = Array.from(text.matchAll(regex))

if (matches.length < 3) throw new Error(text)

return matches.map(([_, url, title]) => ({
url: `${bbsmenuUrl.protocol}${url}`,
title,
}))
}

get fetchedAt() {
return this.#fetchedAt
}

set fetchedAt(value: Date) {
this.#fetchedAt = value
}

stats() {
return {
baseUrl: this.#baseUrl,
fetchedAt: this.#fetchedAt,
threads: this.#threads.res.length,
}
}

async #fetch(
input: string,
init?: RequestInit | undefined,
retry = 0,
): Promise<Response> {
const elapsedTime = new Date().getTime() - this.#fetchedAt.getTime()

if (elapsedTime < FETCH_INTERVAL_MS) {
console.log('FutabaClient', 'throttling', elapsedTime, 'ms')
const wait = FETCH_INTERVAL_MS * (retry === 0 ? 1 : retry) - elapsedTime

console.log(
'FutabaClient',
'elapsed',
elapsedTime,
'ms,',
'throttling',
wait,
'ms',
)

await sleep(FETCH_INTERVAL_MS)
await sleep(wait)
}

const res = await fetch(join(this.#baseUrl, input), init)

this.#fetchedAt = new Date()

return res
}
if (!res.headers.get('Content-Type')?.startsWith('application/json')) {
const message = this.#decoder.decode(await res.arrayBuffer())

stats() {
return {
baseUrl: this.#baseUrl,
fetchedAt: this.#fetchedAt,
threads: this.#threads.res.length,
if (
/ 2/g.test(message) &&
retry < RETRY_MAX
) {
++retry

console.log('FutabaClient', 'retry', retry, '/', RETRY_MAX)

return this.#fetch(input, init, retry)
}

throw new Error(message)
}

return res
}

async threads() {
Expand All @@ -92,15 +131,7 @@ export class FutabaClient {
throw new Error('Failed to fetch threads')
}

const resErr = res.clone()
const text = await res.text()

let threads
try {
threads = threadsSchema.parse(JSON.parse(text))
} catch (_) {
throw new Error(this.#decoder.decode(await resErr.arrayBuffer()))
}
const threads = threadsSchema.parse(await res.json())

this.#threads = threads

Expand Down Expand Up @@ -128,15 +159,7 @@ export class FutabaClient {
throw new Error('Failed to fetch responses')
}

const resErr = res.clone()
const text = await res.text()

let thread
try {
thread = threadSchema.parse(JSON.parse(text))
} catch (_) {
throw new Error(this.#decoder.decode(await resErr.arrayBuffer()))
}
const thread = threadSchema.parse(await res.json())

let index = this.#threads.res.findIndex(
(t) => t.res[0].resId === params.res,
Expand Down

0 comments on commit 0ad30c7

Please sign in to comment.