Skip to content

Commit

Permalink
Log one-time warning when not specifying a partitioner
Browse files Browse the repository at this point in the history
This is to hopefully avoid people mistakenly using the wrong partitioner
because they're relying on the default. This warning will be removed in
a future version.
  • Loading branch information
Nevon committed May 5, 2022
1 parent b59cc1e commit 6e0943f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const createConsumer = require('./consumer')
const createAdmin = require('./admin')
const ISOLATION_LEVEL = require('./protocol/isolationLevel')
const defaultSocketFactory = require('./network/socketFactory')
const once = require('./utils/once')
const websiteUrl = require('./utils/websiteUrl')

const PRIVATE = {
CREATE_CLUSTER: Symbol('private:Kafka:createCluster'),
Expand All @@ -20,6 +22,15 @@ const PRIVATE = {
}

const DEFAULT_METADATA_MAX_AGE = 300000
const warnOfDefaultPartitioner = once(logger => {
if (process.env.KAFKAJS_NO_PARTITIONER_WARNING == null) {
logger.warn(
`KafkaJS v2.0.0 switched default partitioner. To retain the same partitioning behavior as in previous versions, create the producer with the option "createPartitioner: Partitioners.LegacyPartitioner". See ${websiteUrl(
'/docs/2.0.0/migration-guide-v2.0.0'
)} for details. Silence this warning by setting the environment variable "KAFKAJS_NO_PARTITIONER_WARNING=1"`
)
}
})

module.exports = class Client {
/**
Expand Down Expand Up @@ -104,6 +115,10 @@ module.exports = class Client {
instrumentationEmitter,
})

if (createPartitioner == null) {
warnOfDefaultPartitioner(this[PRIVATE.LOGGER])
}

return createProducer({
retry: { ...this[PRIVATE.CLUSTER_RETRY], ...retry },
logger: this[PRIVATE.LOGGER],
Expand Down
10 changes: 10 additions & 0 deletions src/utils/once.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = fn => {
let called = false

return (...args) => {
if (!called) {
called = true
return fn(...args)
}
}
}
14 changes: 14 additions & 0 deletions src/utils/once.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const once = require('./once')

describe('Utils > once', () => {
it('should call the wrapped function only once', () => {
const original = jest.fn().mockReturnValue('foo')
const wrapped = once(original)

expect(wrapped('hello')).toEqual('foo')
expect(wrapped('hello')).toBeUndefined()

expect(original).toHaveBeenCalledTimes(1)
expect(original).toHaveBeenCalledWith('hello')
})
})
6 changes: 5 additions & 1 deletion src/utils/websiteUrl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const BASE_URL = 'https://kafka.js.org'
const stripLeading = char => str => (str.charAt(0) === char ? str.substring(1) : str)
const stripLeadingSlash = stripLeading('/')
const stripLeadingHash = stripLeading('#')

module.exports = (path, hash) => `${BASE_URL}/${path}${hash ? '#' + hash : ''}`
module.exports = (path, hash) =>
`${BASE_URL}/${stripLeadingSlash(path)}${hash ? '#' + stripLeadingHash(hash) : ''}`
13 changes: 7 additions & 6 deletions src/utils/websiteUrl.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const websiteUrl = require('./websiteUrl')

describe('Utils > websiteUrl', () => {
it('generates links to the website', () => {
expect(websiteUrl('docs/faq')).toEqual('https://kafka.js.org/docs/faq')
it.each([['docs/faq'], ['/docs/faq']])('generates links to the website', path => {
expect(websiteUrl(path)).toEqual('https://kafka.js.org/docs/faq')
})

it('allows specifying a hash', () => {
expect(
websiteUrl('docs/faq', 'why-am-i-receiving-messages-for-topics-i-m-not-subscribed-to')
).toEqual(
it.each([
['why-am-i-receiving-messages-for-topics-i-m-not-subscribed-to'],
['#why-am-i-receiving-messages-for-topics-i-m-not-subscribed-to'],
])('allows specifying an anchor', anchor => {
expect(websiteUrl('docs/faq', anchor)).toEqual(
'https://kafka.js.org/docs/faq#why-am-i-receiving-messages-for-topics-i-m-not-subscribed-to'
)
})
Expand Down

0 comments on commit 6e0943f

Please sign in to comment.