Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import axios, { type AxiosInstance, type AxiosRequestConfig } from 'axios'
import axiosRetry, { type AxiosRetry, exponentialDelay } from 'axios-retry'
import axiosRetry, {
type AxiosRetry,
exponentialDelay,
isIdempotentRequestError,
} from 'axios-retry'

import { errorInterceptor } from './error-interceptor.js'
import { serializeUrlSearchParams } from './url-search-params-serializer.js'
Expand Down Expand Up @@ -27,6 +31,7 @@ export const createClient = (options: ClientOptions): AxiosInstance => {
axiosRetry(client, {
retries: 2,
retryDelay: exponentialDelay,
retryCondition: isIdempotentRequestError,
...options.axiosRetryOptions,
})

Expand Down
63 changes: 63 additions & 0 deletions test/seam/connect/retry.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createServer } from 'node:http'

import test from 'ava'
import { AxiosError } from 'axios'
import { getTestServer } from 'fixtures/seam/connect/api.js'
Expand Down Expand Up @@ -35,3 +37,64 @@ test('SeamHttp: retries 503 status errors twice by default ', async (t) => {

t.is(err?.response?.status, 503)
})

test('SeamHttp: does not retry POST requests by default', async (t) => {
const { seed, endpoint } = await getTestServer(t)

const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
endpoint,
axiosRetryOptions: {
onRetry: () => {
t.fail('should not retry a POST request')
},
},
})

await seam.client.post('/_fake/simulate_workspace_outage', {
workspace_id: seed.seed_workspace_1,
routes: ['/devices/list'],
})

const err = await t.throwsAsync(async () => await seam.devices.list(), {
instanceOf: AxiosError,
})

t.is(err?.response?.status, 503)
})

test('SeamHttp: does not replay a POST after a mid-flight connection reset', async (t) => {
let attempts = 0

// A raw server that receives the full request and then resets the
// connection without ever sending a response, e.g., as if a load balancer
// or proxy dropped the connection after forwarding the request upstream.
const server = createServer((req) => {
attempts++
req.resume()
req.on('end', () => {
req.socket.destroy()
})
})

await new Promise<void>((resolve) => {
server.listen(0, resolve)
})
t.teardown(async () => {
await new Promise<void>((resolve) => {
server.close(() => resolve())
})
})

const address = server.address()
if (address == null || typeof address === 'string') {
throw new Error('Could not determine server address')
}

const seam = SeamHttp.fromApiKey('seam_invalidapikey_token', {
endpoint: `http://127.0.0.1:${address.port}`,
})

await t.throwsAsync(async () => await seam.devices.list())

t.is(attempts, 1)
})
Loading