github.com/QuangHoangHao/kafka-go@v0.4.36/protocol.go (about) 1 package kafka 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "strconv" 7 ) 8 9 type ApiVersion struct { 10 ApiKey int16 11 MinVersion int16 12 MaxVersion int16 13 } 14 15 func (v ApiVersion) Format(w fmt.State, r rune) { 16 switch r { 17 case 's': 18 fmt.Fprint(w, apiKey(v.ApiKey)) 19 case 'd': 20 switch { 21 case w.Flag('-'): 22 fmt.Fprint(w, v.MinVersion) 23 case w.Flag('+'): 24 fmt.Fprint(w, v.MaxVersion) 25 default: 26 fmt.Fprint(w, v.ApiKey) 27 } 28 case 'v': 29 switch { 30 case w.Flag('-'): 31 fmt.Fprintf(w, "v%d", v.MinVersion) 32 case w.Flag('+'): 33 fmt.Fprintf(w, "v%d", v.MaxVersion) 34 case w.Flag('#'): 35 fmt.Fprintf(w, "kafka.ApiVersion{ApiKey:%d MinVersion:%d MaxVersion:%d}", v.ApiKey, v.MinVersion, v.MaxVersion) 36 default: 37 fmt.Fprintf(w, "%s[v%d:v%d]", apiKey(v.ApiKey), v.MinVersion, v.MaxVersion) 38 } 39 } 40 } 41 42 type apiKey int16 43 44 const ( 45 produce apiKey = 0 46 fetch apiKey = 1 47 listOffsets apiKey = 2 48 metadata apiKey = 3 49 leaderAndIsr apiKey = 4 50 stopReplica apiKey = 5 51 updateMetadata apiKey = 6 52 controlledShutdown apiKey = 7 53 offsetCommit apiKey = 8 54 offsetFetch apiKey = 9 55 findCoordinator apiKey = 10 56 joinGroup apiKey = 11 57 heartbeat apiKey = 12 58 leaveGroup apiKey = 13 59 syncGroup apiKey = 14 60 describeGroups apiKey = 15 61 listGroups apiKey = 16 62 saslHandshake apiKey = 17 63 apiVersions apiKey = 18 64 createTopics apiKey = 19 65 deleteTopics apiKey = 20 66 deleteRecords apiKey = 21 67 initProducerId apiKey = 22 68 offsetForLeaderEpoch apiKey = 23 69 addPartitionsToTxn apiKey = 24 70 addOffsetsToTxn apiKey = 25 71 endTxn apiKey = 26 72 writeTxnMarkers apiKey = 27 73 txnOffsetCommit apiKey = 28 74 describeAcls apiKey = 29 75 createAcls apiKey = 30 76 deleteAcls apiKey = 31 77 describeConfigs apiKey = 32 78 alterConfigs apiKey = 33 79 alterReplicaLogDirs apiKey = 34 80 describeLogDirs apiKey = 35 81 saslAuthenticate apiKey = 36 82 createPartitions apiKey = 37 83 createDelegationToken apiKey = 38 84 renewDelegationToken apiKey = 39 85 expireDelegationToken apiKey = 40 86 describeDelegationToken apiKey = 41 87 deleteGroups apiKey = 42 88 electLeaders apiKey = 43 89 incrementalAlterConfigs apiKey = 44 90 alterPartitionReassignments apiKey = 45 91 listPartitionReassignments apiKey = 46 92 offsetDelete apiKey = 47 93 ) 94 95 func (k apiKey) String() string { 96 if i := int(k); i >= 0 && i < len(apiKeyStrings) { 97 return apiKeyStrings[i] 98 } 99 return strconv.Itoa(int(k)) 100 } 101 102 type apiVersion int16 103 104 const ( 105 v0 = 0 106 v1 = 1 107 v2 = 2 108 v3 = 3 109 v5 = 5 110 v7 = 7 111 v10 = 10 112 113 // Unused protocol versions: v4, v6, v8, v9. 114 ) 115 116 var apiKeyStrings = [...]string{ 117 produce: "Produce", 118 fetch: "Fetch", 119 listOffsets: "ListOffsets", 120 metadata: "Metadata", 121 leaderAndIsr: "LeaderAndIsr", 122 stopReplica: "StopReplica", 123 updateMetadata: "UpdateMetadata", 124 controlledShutdown: "ControlledShutdown", 125 offsetCommit: "OffsetCommit", 126 offsetFetch: "OffsetFetch", 127 findCoordinator: "FindCoordinator", 128 joinGroup: "JoinGroup", 129 heartbeat: "Heartbeat", 130 leaveGroup: "LeaveGroup", 131 syncGroup: "SyncGroup", 132 describeGroups: "DescribeGroups", 133 listGroups: "ListGroups", 134 saslHandshake: "SaslHandshake", 135 apiVersions: "ApiVersions", 136 createTopics: "CreateTopics", 137 deleteTopics: "DeleteTopics", 138 deleteRecords: "DeleteRecords", 139 initProducerId: "InitProducerId", 140 offsetForLeaderEpoch: "OffsetForLeaderEpoch", 141 addPartitionsToTxn: "AddPartitionsToTxn", 142 addOffsetsToTxn: "AddOffsetsToTxn", 143 endTxn: "EndTxn", 144 writeTxnMarkers: "WriteTxnMarkers", 145 txnOffsetCommit: "TxnOffsetCommit", 146 describeAcls: "DescribeAcls", 147 createAcls: "CreateAcls", 148 deleteAcls: "DeleteAcls", 149 describeConfigs: "DescribeConfigs", 150 alterConfigs: "AlterConfigs", 151 alterReplicaLogDirs: "AlterReplicaLogDirs", 152 describeLogDirs: "DescribeLogDirs", 153 saslAuthenticate: "SaslAuthenticate", 154 createPartitions: "CreatePartitions", 155 createDelegationToken: "CreateDelegationToken", 156 renewDelegationToken: "RenewDelegationToken", 157 expireDelegationToken: "ExpireDelegationToken", 158 describeDelegationToken: "DescribeDelegationToken", 159 deleteGroups: "DeleteGroups", 160 electLeaders: "ElectLeaders", 161 incrementalAlterConfigs: "IncrementalAlfterConfigs", 162 alterPartitionReassignments: "AlterPartitionReassignments", 163 listPartitionReassignments: "ListPartitionReassignments", 164 offsetDelete: "OffsetDelete", 165 } 166 167 type requestHeader struct { 168 Size int32 169 ApiKey int16 170 ApiVersion int16 171 CorrelationID int32 172 ClientID string 173 } 174 175 func (h requestHeader) size() int32 { 176 return 4 + 2 + 2 + 4 + sizeofString(h.ClientID) 177 } 178 179 func (h requestHeader) writeTo(wb *writeBuffer) { 180 wb.writeInt32(h.Size) 181 wb.writeInt16(h.ApiKey) 182 wb.writeInt16(h.ApiVersion) 183 wb.writeInt32(h.CorrelationID) 184 wb.writeString(h.ClientID) 185 } 186 187 type request interface { 188 size() int32 189 writable 190 } 191 192 func makeInt8(b []byte) int8 { 193 return int8(b[0]) 194 } 195 196 func makeInt16(b []byte) int16 { 197 return int16(binary.BigEndian.Uint16(b)) 198 } 199 200 func makeInt32(b []byte) int32 { 201 return int32(binary.BigEndian.Uint32(b)) 202 } 203 204 func makeInt64(b []byte) int64 { 205 return int64(binary.BigEndian.Uint64(b)) 206 } 207 208 func expectZeroSize(sz int, err error) error { 209 if err == nil && sz != 0 { 210 err = fmt.Errorf("reading a response left %d unread bytes", sz) 211 } 212 return err 213 }