@@ -47,6 +47,7 @@ import {
4747import { type GenericError , type ProtocolError , UserError } from '../../errors.ts'
4848import { type ConnectionPool } from '../../network/connection-pool.ts'
4949import { type Connection } from '../../network/connection.ts'
50+ import { INT32_SIZE } from '../../protocol/definitions.ts'
5051import { Reader } from '../../protocol/reader.ts'
5152import { Writer } from '../../protocol/writer.ts'
5253import {
@@ -117,8 +118,8 @@ export class Consumer<Key = Buffer, Value = Buffer, HeaderKey = Buffer, HeaderVa
117118 #coordinatorId: number | null
118119 #heartbeatInterval: NodeJS . Timeout | null
119120 #lastHeartbeat: Date | null
120- #streams: Set < MessagesStream < Key , Value , HeaderKey , HeaderValue > >
121- #partitionsAssigner: GroupPartitionsAssigner ;
121+ #streams: Set < MessagesStream < Key , Value , HeaderKey , HeaderValue > > ;
122+
122123 /*
123124 The following requests are blocking in Kafka:
124125
@@ -158,7 +159,6 @@ export class Consumer<Key = Buffer, Value = Buffer, HeaderKey = Buffer, HeaderVa
158159 this . #heartbeatInterval = null
159160 this . #lastHeartbeat = null
160161 this . #streams = new Set ( )
161- this . #partitionsAssigner = this [ kOptions ] . partitionAssigner ?? roundRobinAssigner
162162
163163 this . #validateGroupOptions( this [ kOptions ] , groupIdAndOptionsValidator )
164164
@@ -868,12 +868,16 @@ export class Consumer<Key = Buffer, Value = Buffer, HeaderKey = Buffer, HeaderVa
868868 )
869869 }
870870
871- #syncGroup ( callback : CallbackWithPromise < GroupAssignment [ ] > ) : void {
871+ #syncGroup (
872+ partitionsAssigner : GroupPartitionsAssigner | null ,
873+ callback : CallbackWithPromise < GroupAssignment [ ] >
874+ ) : void {
872875 consumerGroupChannel . traceCallback (
873876 this . #performSyncGroup,
874- 1 ,
877+ 2 ,
875878 createDiagnosticContext ( { client : this , operation : 'syncGroup' } ) ,
876879 this ,
880+ partitionsAssigner ,
877881 null ,
878882 callback
879883 )
@@ -1108,7 +1112,7 @@ export class Consumer<Key = Buffer, Value = Buffer, HeaderKey = Buffer, HeaderVa
11081112 }
11091113
11101114 // Send a syncGroup request
1111- this . #syncGroup( ( error , response ) => {
1115+ this . #syncGroup( options . partitionAssigner , ( error , response ) => {
11121116 if ( ! this . #membershipActive) {
11131117 callback ( null , undefined as unknown as string )
11141118 return
@@ -1225,6 +1229,7 @@ export class Consumer<Key = Buffer, Value = Buffer, HeaderKey = Buffer, HeaderVa
12251229 }
12261230
12271231 #performSyncGroup (
1232+ partitionsAssigner : GroupPartitionsAssigner | null ,
12281233 assignments : SyncGroupRequestAssignment [ ] | null ,
12291234 callback : CallbackWithPromise < GroupAssignment [ ] >
12301235 ) : void {
@@ -1257,7 +1262,7 @@ export class Consumer<Key = Buffer, Value = Buffer, HeaderKey = Buffer, HeaderVa
12571262 return
12581263 }
12591264
1260- this . #performSyncGroup( this . #createAssignments( metadata ) , callback )
1265+ this . #performSyncGroup( partitionsAssigner , this . #createAssignments( partitionsAssigner , metadata ) , callback )
12611266 } )
12621267
12631268 return
@@ -1409,6 +1414,10 @@ export class Consumer<Key = Buffer, Value = Buffer, HeaderKey = Buffer, HeaderVa
14091414
14101415 reader . skip ( 2 ) // Ignore Version information
14111416
1417+ if ( reader . remaining < INT32_SIZE ) {
1418+ return [ ]
1419+ }
1420+
14121421 return reader . readArray (
14131422 r => {
14141423 return {
@@ -1421,7 +1430,10 @@ export class Consumer<Key = Buffer, Value = Buffer, HeaderKey = Buffer, HeaderVa
14211430 )
14221431 }
14231432
1424- #createAssignments ( metadata : ClusterMetadata ) : SyncGroupRequestAssignment [ ] {
1433+ #createAssignments (
1434+ partitionsAssigner : GroupPartitionsAssigner | null ,
1435+ metadata : ClusterMetadata
1436+ ) : SyncGroupRequestAssignment [ ] {
14251437 const partitionTracker : Map < string , { next : number ; max : number } > = new Map ( )
14261438
14271439 // First of all, layout topics-partitions in a list
@@ -1448,12 +1460,9 @@ export class Consumer<Key = Buffer, Value = Buffer, HeaderKey = Buffer, HeaderVa
14481460 }
14491461
14501462 const encodedAssignments : SyncGroupRequestAssignment [ ] = [ ]
1451- for ( const member of this . #partitionsAssigner(
1452- this . memberId ! ,
1453- this . #members,
1454- new Set ( this . topics . current ) ,
1455- metadata
1456- ) ) {
1463+
1464+ partitionsAssigner ??= this [ kOptions ] . partitionAssigner ?? roundRobinAssigner
1465+ for ( const member of partitionsAssigner ( this . memberId ! , this . #members, new Set ( this . topics . current ) , metadata ) ) {
14571466 encodedAssignments . push ( {
14581467 memberId : member . memberId ,
14591468 assignment : this . #encodeProtocolAssignment( Array . from ( member . assignments . values ( ) ) )
0 commit comments