github.com/hack0072008/kafka-go@v1.0.1/joingroup.go (about) 1 package kafka 2 3 import ( 4 "bufio" 5 "bytes" 6 ) 7 8 type memberGroupMetadata struct { 9 // MemberID assigned by the group coordinator or null if joining for the 10 // first time. 11 MemberID string 12 Metadata groupMetadata 13 } 14 15 type groupMetadata struct { 16 Version int16 17 Topics []string 18 UserData []byte 19 } 20 21 func (t groupMetadata) size() int32 { 22 return sizeofInt16(t.Version) + 23 sizeofStringArray(t.Topics) + 24 sizeofBytes(t.UserData) 25 } 26 27 func (t groupMetadata) writeTo(wb *writeBuffer) { 28 wb.writeInt16(t.Version) 29 wb.writeStringArray(t.Topics) 30 wb.writeBytes(t.UserData) 31 } 32 33 func (t groupMetadata) bytes() []byte { 34 buf := bytes.NewBuffer(nil) 35 t.writeTo(&writeBuffer{w: buf}) 36 return buf.Bytes() 37 } 38 39 func (t *groupMetadata) readFrom(r *bufio.Reader, size int) (remain int, err error) { 40 if remain, err = readInt16(r, size, &t.Version); err != nil { 41 return 42 } 43 if remain, err = readStringArray(r, remain, &t.Topics); err != nil { 44 return 45 } 46 if remain, err = readBytes(r, remain, &t.UserData); err != nil { 47 return 48 } 49 return 50 } 51 52 type joinGroupRequestGroupProtocolV1 struct { 53 ProtocolName string 54 ProtocolMetadata []byte 55 } 56 57 func (t joinGroupRequestGroupProtocolV1) size() int32 { 58 return sizeofString(t.ProtocolName) + 59 sizeofBytes(t.ProtocolMetadata) 60 } 61 62 func (t joinGroupRequestGroupProtocolV1) writeTo(wb *writeBuffer) { 63 wb.writeString(t.ProtocolName) 64 wb.writeBytes(t.ProtocolMetadata) 65 } 66 67 type joinGroupRequestV1 struct { 68 // GroupID holds the unique group identifier 69 GroupID string 70 71 // SessionTimeout holds the coordinator considers the consumer dead if it 72 // receives no heartbeat after this timeout in ms. 73 SessionTimeout int32 74 75 // RebalanceTimeout holds the maximum time that the coordinator will wait 76 // for each member to rejoin when rebalancing the group in ms 77 RebalanceTimeout int32 78 79 // MemberID assigned by the group coordinator or the zero string if joining 80 // for the first time. 81 MemberID string 82 83 // ProtocolType holds the unique name for class of protocols implemented by group 84 ProtocolType string 85 86 // GroupProtocols holds the list of protocols that the member supports 87 GroupProtocols []joinGroupRequestGroupProtocolV1 88 } 89 90 func (t joinGroupRequestV1) size() int32 { 91 return sizeofString(t.GroupID) + 92 sizeofInt32(t.SessionTimeout) + 93 sizeofInt32(t.RebalanceTimeout) + 94 sizeofString(t.MemberID) + 95 sizeofString(t.ProtocolType) + 96 sizeofArray(len(t.GroupProtocols), func(i int) int32 { return t.GroupProtocols[i].size() }) 97 } 98 99 func (t joinGroupRequestV1) writeTo(wb *writeBuffer) { 100 wb.writeString(t.GroupID) 101 wb.writeInt32(t.SessionTimeout) 102 wb.writeInt32(t.RebalanceTimeout) 103 wb.writeString(t.MemberID) 104 wb.writeString(t.ProtocolType) 105 wb.writeArray(len(t.GroupProtocols), func(i int) { t.GroupProtocols[i].writeTo(wb) }) 106 } 107 108 type joinGroupResponseMemberV1 struct { 109 // MemberID assigned by the group coordinator 110 MemberID string 111 MemberMetadata []byte 112 } 113 114 func (t joinGroupResponseMemberV1) size() int32 { 115 return sizeofString(t.MemberID) + 116 sizeofBytes(t.MemberMetadata) 117 } 118 119 func (t joinGroupResponseMemberV1) writeTo(wb *writeBuffer) { 120 wb.writeString(t.MemberID) 121 wb.writeBytes(t.MemberMetadata) 122 } 123 124 func (t *joinGroupResponseMemberV1) readFrom(r *bufio.Reader, size int) (remain int, err error) { 125 if remain, err = readString(r, size, &t.MemberID); err != nil { 126 return 127 } 128 if remain, err = readBytes(r, remain, &t.MemberMetadata); err != nil { 129 return 130 } 131 return 132 } 133 134 type joinGroupResponseV1 struct { 135 // ErrorCode holds response error code 136 ErrorCode int16 137 138 // GenerationID holds the generation of the group. 139 GenerationID int32 140 141 // GroupProtocol holds the group protocol selected by the coordinator 142 GroupProtocol string 143 144 // LeaderID holds the leader of the group 145 LeaderID string 146 147 // MemberID assigned by the group coordinator 148 MemberID string 149 Members []joinGroupResponseMemberV1 150 } 151 152 func (t joinGroupResponseV1) size() int32 { 153 return sizeofInt16(t.ErrorCode) + 154 sizeofInt32(t.GenerationID) + 155 sizeofString(t.GroupProtocol) + 156 sizeofString(t.LeaderID) + 157 sizeofString(t.MemberID) + 158 sizeofArray(len(t.MemberID), func(i int) int32 { return t.Members[i].size() }) 159 } 160 161 func (t joinGroupResponseV1) writeTo(wb *writeBuffer) { 162 wb.writeInt16(t.ErrorCode) 163 wb.writeInt32(t.GenerationID) 164 wb.writeString(t.GroupProtocol) 165 wb.writeString(t.LeaderID) 166 wb.writeString(t.MemberID) 167 wb.writeArray(len(t.Members), func(i int) { t.Members[i].writeTo(wb) }) 168 } 169 170 func (t *joinGroupResponseV1) readFrom(r *bufio.Reader, size int) (remain int, err error) { 171 if remain, err = readInt16(r, size, &t.ErrorCode); err != nil { 172 return 173 } 174 if remain, err = readInt32(r, remain, &t.GenerationID); err != nil { 175 return 176 } 177 if remain, err = readString(r, remain, &t.GroupProtocol); err != nil { 178 return 179 } 180 if remain, err = readString(r, remain, &t.LeaderID); err != nil { 181 return 182 } 183 if remain, err = readString(r, remain, &t.MemberID); err != nil { 184 return 185 } 186 187 fn := func(r *bufio.Reader, size int) (fnRemain int, fnErr error) { 188 var item joinGroupResponseMemberV1 189 if fnRemain, fnErr = (&item).readFrom(r, size); fnErr != nil { 190 return 191 } 192 t.Members = append(t.Members, item) 193 return 194 } 195 if remain, err = readArrayWith(r, remain, fn); err != nil { 196 return 197 } 198 199 return 200 }