github.com/rbisecke/kafka-go@v0.4.27/syncgroup.go (about)

     1  package kafka
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  )
     7  
     8  type groupAssignment struct {
     9  	Version  int16
    10  	Topics   map[string][]int32
    11  	UserData []byte
    12  }
    13  
    14  func (t groupAssignment) size() int32 {
    15  	sz := sizeofInt16(t.Version) + sizeofInt16(int16(len(t.Topics)))
    16  
    17  	for topic, partitions := range t.Topics {
    18  		sz += sizeofString(topic) + sizeofInt32Array(partitions)
    19  	}
    20  
    21  	return sz + sizeofBytes(t.UserData)
    22  }
    23  
    24  func (t groupAssignment) writeTo(wb *writeBuffer) {
    25  	wb.writeInt16(t.Version)
    26  	wb.writeInt32(int32(len(t.Topics)))
    27  
    28  	for topic, partitions := range t.Topics {
    29  		wb.writeString(topic)
    30  		wb.writeInt32Array(partitions)
    31  	}
    32  
    33  	wb.writeBytes(t.UserData)
    34  }
    35  
    36  func (t *groupAssignment) readFrom(r *bufio.Reader, size int) (remain int, err error) {
    37  	// I came across this case when testing for compatibility with bsm/sarama-cluster. It
    38  	// appears in some cases, sarama-cluster can send a nil array entry. Admittedly, I
    39  	// didn't look too closely at it.
    40  	if size == 0 {
    41  		t.Topics = map[string][]int32{}
    42  		return 0, nil
    43  	}
    44  
    45  	if remain, err = readInt16(r, size, &t.Version); err != nil {
    46  		return
    47  	}
    48  	if remain, err = readMapStringInt32(r, remain, &t.Topics); err != nil {
    49  		return
    50  	}
    51  	if remain, err = readBytes(r, remain, &t.UserData); err != nil {
    52  		return
    53  	}
    54  
    55  	return
    56  }
    57  
    58  func (t groupAssignment) bytes() []byte {
    59  	buf := bytes.NewBuffer(nil)
    60  	t.writeTo(&writeBuffer{w: buf})
    61  	return buf.Bytes()
    62  }
    63  
    64  type syncGroupRequestGroupAssignmentV0 struct {
    65  	// MemberID assigned by the group coordinator
    66  	MemberID string
    67  
    68  	// MemberAssignments holds client encoded assignments
    69  	//
    70  	// See consumer groups section of https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
    71  	MemberAssignments []byte
    72  }
    73  
    74  func (t syncGroupRequestGroupAssignmentV0) size() int32 {
    75  	return sizeofString(t.MemberID) +
    76  		sizeofBytes(t.MemberAssignments)
    77  }
    78  
    79  func (t syncGroupRequestGroupAssignmentV0) writeTo(wb *writeBuffer) {
    80  	wb.writeString(t.MemberID)
    81  	wb.writeBytes(t.MemberAssignments)
    82  }
    83  
    84  type syncGroupRequestV0 struct {
    85  	// GroupID holds the unique group identifier
    86  	GroupID string
    87  
    88  	// GenerationID holds the generation of the group.
    89  	GenerationID int32
    90  
    91  	// MemberID assigned by the group coordinator
    92  	MemberID string
    93  
    94  	GroupAssignments []syncGroupRequestGroupAssignmentV0
    95  }
    96  
    97  func (t syncGroupRequestV0) size() int32 {
    98  	return sizeofString(t.GroupID) +
    99  		sizeofInt32(t.GenerationID) +
   100  		sizeofString(t.MemberID) +
   101  		sizeofArray(len(t.GroupAssignments), func(i int) int32 { return t.GroupAssignments[i].size() })
   102  }
   103  
   104  func (t syncGroupRequestV0) writeTo(wb *writeBuffer) {
   105  	wb.writeString(t.GroupID)
   106  	wb.writeInt32(t.GenerationID)
   107  	wb.writeString(t.MemberID)
   108  	wb.writeArray(len(t.GroupAssignments), func(i int) { t.GroupAssignments[i].writeTo(wb) })
   109  }
   110  
   111  type syncGroupResponseV0 struct {
   112  	// ErrorCode holds response error code
   113  	ErrorCode int16
   114  
   115  	// MemberAssignments holds client encoded assignments
   116  	//
   117  	// See consumer groups section of https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
   118  	MemberAssignments []byte
   119  }
   120  
   121  func (t syncGroupResponseV0) size() int32 {
   122  	return sizeofInt16(t.ErrorCode) +
   123  		sizeofBytes(t.MemberAssignments)
   124  }
   125  
   126  func (t syncGroupResponseV0) writeTo(wb *writeBuffer) {
   127  	wb.writeInt16(t.ErrorCode)
   128  	wb.writeBytes(t.MemberAssignments)
   129  }
   130  
   131  func (t *syncGroupResponseV0) readFrom(r *bufio.Reader, sz int) (remain int, err error) {
   132  	if remain, err = readInt16(r, sz, &t.ErrorCode); err != nil {
   133  		return
   134  	}
   135  	if remain, err = readBytes(r, remain, &t.MemberAssignments); err != nil {
   136  		return
   137  	}
   138  	return
   139  }