Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent inflight's correlation id collisions #926

Merged
merged 3 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ class KafkaJSUnsupportedMagicByteInMessageSet extends KafkaJSNonRetriableError {
}
}

class KafkaJSCorrelationIdAlreadyExists extends KafkaJSNonRetriableError {
constructor(correlationId) {
super(...arguments)
this.name = 'KafkaJSCorrelationIdAlreadyExists'
this.message = `Correlation id ${correlationId} already exists`
}
}
ThomasFerro marked this conversation as resolved.
Show resolved Hide resolved

module.exports = {
KafkaJSError,
KafkaJSNonRetriableError,
Expand All @@ -201,4 +209,5 @@ module.exports = {
KafkaJSLockTimeout,
KafkaJSServerDoesNotSupportApiKey,
KafkaJSUnsupportedMagicByteInMessageSet,
KafkaJSCorrelationIdAlreadyExists,
}
4 changes: 4 additions & 0 deletions src/network/requestQueue/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const EventEmitter = require('events')
const SocketRequest = require('./socketRequest')
const events = require('../instrumentationEvents')
const { KafkaJSCorrelationIdAlreadyExists } = require('../../errors')

const PRIVATE = {
EMIT_QUEUE_SIZE_EVENT: Symbol('private:RequestQueue:emitQueueSizeEvent'),
Expand Down Expand Up @@ -131,6 +132,9 @@ module.exports = class RequestQueue extends EventEmitter {
instrumentationEmitter: this.instrumentationEmitter,
requestTimeout,
send: () => {
if (this.inflight.has(correlationId)) {
throw new KafkaJSCorrelationIdAlreadyExists(correlationId)
}
this.inflight.set(correlationId, socketRequest)
pushedRequest.sendRequest()
},
Expand Down
8 changes: 8 additions & 0 deletions src/network/requestQueue/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { newLogger } = require('testHelpers')
const InstrumentationEventEmitter = require('../../instrumentation/emitter')
const events = require('../instrumentationEvents')
const RequestQueue = require('./index')
const { KafkaJSCorrelationIdAlreadyExists } = require('../../errors')

describe('Network > RequestQueue', () => {
let requestQueue
Expand Down Expand Up @@ -187,6 +188,13 @@ describe('Network > RequestQueue', () => {
const sentAt = await sendDone
expect(sentAt).toBeGreaterThanOrEqual(before + clientSideThrottleTime)
})

it('does not allow for a inflight correlation ids collision', async () => {
requestQueue.inflight.set(request.entry.correlationId, 'already existing inflight')
expect(() => {
requestQueue.push(request)
}).toThrowError(new KafkaJSCorrelationIdAlreadyExists(request.entry.correlationId))
})
})

describe('#fulfillRequest', () => {
Expand Down