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

#1147 Allow disconnect on rebalance, currently if trying to rebalance kafka… #1462

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
17 changes: 14 additions & 3 deletions src/consumer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,18 @@ module.exports = ({
)
}

let allowCrashReconnect = true
/** @type {import("../../types").Consumer["connect"]} */
const connect = async () => {
allowCrashReconnect = true
await cluster.connect()
instrumentationEmitter.emit(CONNECT)
}

/** @type {import("../../types").Consumer["disconnect"]} */
const disconnect = async () => {
const disconnect = async (lAllowCrashReconnect = false) => {
try {
allowCrashReconnect = lAllowCrashReconnect
await stop()
logger.debug('consumer has stopped, disconnecting', { groupId })
await cluster.disconnect()
Expand Down Expand Up @@ -201,6 +204,10 @@ module.exports = ({
}

const start = async onCrash => {
if (!allowCrashReconnect) {
return
}

logger.info('Starting', { groupId })

consumerGroup = new ConsumerGroup({
Expand Down Expand Up @@ -254,7 +261,7 @@ module.exports = ({
cluster.removeBroker({ host: e.host, port: e.port })
}

await disconnect()
await disconnect(allowCrashReconnect)

const getOriginalCause = error => {
if (error.cause) {
Expand Down Expand Up @@ -286,9 +293,13 @@ module.exports = ({
instrumentationEmitter.emit(CRASH, {
error: e,
groupId,
restart: shouldRestart,
restart: shouldRestart && allowCrashReconnect,
})

if (!allowCrashReconnect) {
return
}

if (shouldRestart) {
const retryTime = e.retryTime || (retry && retry.initialRetryTime) || initialRetryTime
logger.error(`Restarting the consumer in ${retryTime}ms`, {
Expand Down