@@ -42,8 +42,30 @@ function KafkaConsumer(conf, topicConf) {
4242
4343 var onRebalance = conf . rebalance_cb ;
4444
45- // Delete it because it messes other things up
46- delete conf . rebalance_cb ;
45+ var self = this ;
46+
47+ // If rebalance is undefined we don't want any part of this
48+ if ( onRebalance && typeof onRebalance === 'boolean' ) {
49+ conf . rebalance_cb = function ( e ) {
50+ // That's it
51+ if ( e . code === 500 /*CODES.REBALANCE.PARTITION_ASSIGNMENT*/ ) {
52+ self . assign ( e . assignment ) ;
53+ } else if ( e . code === 501 /*CODES.REBALANCE.PARTITION_UNASSIGNMENT*/ ) {
54+ self . unassign ( e . assignment ) ;
55+ }
56+ } ;
57+ } else if ( onRebalance && typeof onRebalance === 'function' ) {
58+ /*
59+ * Once this is opted in to, that's it. It's going to manually rebalance
60+ * forever. There is no way to unset config values in librdkafka, just
61+ * a way to override them.
62+ */
63+
64+ conf . rebalance_cb = function ( e ) {
65+ self . emit ( 'rebalance' , e ) ;
66+ onRebalance . call ( self , e ) ;
67+ } ;
68+ }
4769
4870
4971 /**
@@ -63,42 +85,6 @@ function KafkaConsumer(conf, topicConf) {
6385
6486 Client . call ( this , conf , Kafka . KafkaConsumer , topicConf ) ;
6587
66- var self = this ;
67-
68- if ( onRebalance !== false ) {
69- // If rebalance is specifically false we don't want any part of this
70-
71- if ( typeof onRebalance !== 'function' ) {
72- onRebalance = function ( e ) {
73- // That's it
74- self . assign ( e . assignments ) ;
75- } ;
76- }
77-
78- }
79-
80- /*
81- * Once this is opted in to, that's it. It's going to manually rebalance
82- * forever. There is no way to unset config values in librdkafka, just
83- * a way to override them.
84- */
85-
86- if ( onRebalance ) {
87- /**
88- * Rebalance event. Called when the KafkaConsumer is rebalancing.
89- *
90- * @event KafkaConsumer#rebalance
91- * @type {object }
92- * @property {number } code - whether the rebalance was an assignment or
93- * an unassignment
94- */
95- this . _client . onRebalance ( function ( e ) {
96- self . emit ( 'rebalance' , e ) ;
97- onRebalance . call ( self , e ) ;
98- } ) ;
99-
100- }
101-
10288 this . globalConfig = conf ;
10389 this . topicConfig = topicConf ;
10490}
0 commit comments