Skip to content

Commit 3a8161a

Browse files
committed
Adjusted the Request-Id header limit to 7k and updated unit tests
1 parent d0724e6 commit 3a8161a

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/api-client.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,17 @@ export class APIClient {
174174
delinquencyConfig.warning_shown = true
175175
}
176176

177-
// eslint-disable-next-line complexity
178177
static async request<T>(url: string, opts: APIClient.Options = {}, retries = 3): Promise<APIHTTPClient<T>> {
179178
opts.headers = opts.headers || {}
180179
const currentRequestId = RequestId.create() && RequestId.headerValue
181180

182181
// Accumulation of requestIds in the header
183-
// causes a header overflow error. These have been
182+
// causes a header overflow error. Headers have been
184183
// observed to be larger than 8k (Node default max)
185184
// in long running poll operations such as pg:wait
186-
if (Buffer.from(currentRequestId).byteLength > 1024 * 8) {
185+
// We limit the Request-Id header to 7k to allow some
186+
// room fo other headers.
187+
if (Buffer.from(currentRequestId).byteLength > 1024 * 7) {
187188
RequestId.empty()
188189
opts.headers[requestIdHeader] = RequestId.create()
189190
} else {

test/api-client.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,5 +635,32 @@ describe('api_client', () => {
635635

636636
expect(secondRequest.getHeader(requestIdHeader)).to.deep.equal('second-random-uuid,first-existing-request-id,second-existing-request-id,random-uuid')
637637
})
638+
639+
test
640+
.it('resets request id when it exceeds 7KB', async ctx => {
641+
const cmd = new Command([], ctx.config)
642+
// Create a large request ID that exceeds 7KB
643+
const largeRequestId = 'x'.repeat(1024 * 8)
644+
Reflect.set(RequestId, 'ids', [largeRequestId])
645+
646+
generateStub.returns('new-uuid-after-reset')
647+
api = nock('https://api.heroku.com').get('/apps').reply(200, [{name: 'myapp'}])
648+
649+
const {request} = await cmd.heroku.get('/apps')
650+
expect(request.getHeader(requestIdHeader)).to.deep.equal(['new-uuid-after-reset'])
651+
})
652+
653+
test
654+
.it('keeps existing request id when under 7KB', async ctx => {
655+
const cmd = new Command([], ctx.config)
656+
// Create a request ID that's under 7KB
657+
const normalRequestId = 'normal-request-id'
658+
Reflect.set(RequestId, 'ids', [normalRequestId])
659+
660+
api = nock('https://api.heroku.com').get('/apps').reply(200, [{name: 'myapp'}])
661+
662+
const {request} = await cmd.heroku.get('/apps')
663+
expect(request.getHeader(requestIdHeader)).to.deep.equal(',normal-request-id')
664+
})
638665
})
639666
})

0 commit comments

Comments
 (0)