github.com/deanMdreon/kafka-go@v0.4.32/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  	v4  = 4
   110  	v5  = 5
   111  	v6  = 6
   112  	v7  = 7
   113  	v8  = 8
   114  	v9  = 9
   115  	v10 = 10
   116  )
   117  
   118  var apiKeyStrings = [...]string{
   119  	produce:                     "Produce",
   120  	fetch:                       "Fetch",
   121  	listOffsets:                 "ListOffsets",
   122  	metadata:                    "Metadata",
   123  	leaderAndIsr:                "LeaderAndIsr",
   124  	stopReplica:                 "StopReplica",
   125  	updateMetadata:              "UpdateMetadata",
   126  	controlledShutdown:          "ControlledShutdown",
   127  	offsetCommit:                "OffsetCommit",
   128  	offsetFetch:                 "OffsetFetch",
   129  	findCoordinator:             "FindCoordinator",
   130  	joinGroup:                   "JoinGroup",
   131  	heartbeat:                   "Heartbeat",
   132  	leaveGroup:                  "LeaveGroup",
   133  	syncGroup:                   "SyncGroup",
   134  	describeGroups:              "DescribeGroups",
   135  	listGroups:                  "ListGroups",
   136  	saslHandshake:               "SaslHandshake",
   137  	apiVersions:                 "ApiVersions",
   138  	createTopics:                "CreateTopics",
   139  	deleteTopics:                "DeleteTopics",
   140  	deleteRecords:               "DeleteRecords",
   141  	initProducerId:              "InitProducerId",
   142  	offsetForLeaderEpoch:        "OffsetForLeaderEpoch",
   143  	addPartitionsToTxn:          "AddPartitionsToTxn",
   144  	addOffsetsToTxn:             "AddOffsetsToTxn",
   145  	endTxn:                      "EndTxn",
   146  	writeTxnMarkers:             "WriteTxnMarkers",
   147  	txnOffsetCommit:             "TxnOffsetCommit",
   148  	describeAcls:                "DescribeAcls",
   149  	createAcls:                  "CreateAcls",
   150  	deleteAcls:                  "DeleteAcls",
   151  	describeConfigs:             "DescribeConfigs",
   152  	alterConfigs:                "AlterConfigs",
   153  	alterReplicaLogDirs:         "AlterReplicaLogDirs",
   154  	describeLogDirs:             "DescribeLogDirs",
   155  	saslAuthenticate:            "SaslAuthenticate",
   156  	createPartitions:            "CreatePartitions",
   157  	createDelegationToken:       "CreateDelegationToken",
   158  	renewDelegationToken:        "RenewDelegationToken",
   159  	expireDelegationToken:       "ExpireDelegationToken",
   160  	describeDelegationToken:     "DescribeDelegationToken",
   161  	deleteGroups:                "DeleteGroups",
   162  	electLeaders:                "ElectLeaders",
   163  	incrementalAlterConfigs:     "IncrementalAlfterConfigs",
   164  	alterPartitionReassignments: "AlterPartitionReassignments",
   165  	listPartitionReassignments:  "ListPartitionReassignments",
   166  	offsetDelete:                "OffsetDelete",
   167  }
   168  
   169  type requestHeader struct {
   170  	Size          int32
   171  	ApiKey        int16
   172  	ApiVersion    int16
   173  	CorrelationID int32
   174  	ClientID      string
   175  }
   176  
   177  func (h requestHeader) size() int32 {
   178  	return 4 + 2 + 2 + 4 + sizeofString(h.ClientID)
   179  }
   180  
   181  func (h requestHeader) writeTo(wb *writeBuffer) {
   182  	wb.writeInt32(h.Size)
   183  	wb.writeInt16(h.ApiKey)
   184  	wb.writeInt16(h.ApiVersion)
   185  	wb.writeInt32(h.CorrelationID)
   186  	wb.writeString(h.ClientID)
   187  }
   188  
   189  type request interface {
   190  	size() int32
   191  	writable
   192  }
   193  
   194  func makeInt8(b []byte) int8 {
   195  	return int8(b[0])
   196  }
   197  
   198  func makeInt16(b []byte) int16 {
   199  	return int16(binary.BigEndian.Uint16(b))
   200  }
   201  
   202  func makeInt32(b []byte) int32 {
   203  	return int32(binary.BigEndian.Uint32(b))
   204  }
   205  
   206  func makeInt64(b []byte) int64 {
   207  	return int64(binary.BigEndian.Uint64(b))
   208  }
   209  
   210  func expectZeroSize(sz int, err error) error {
   211  	if err == nil && sz != 0 {
   212  		err = fmt.Errorf("reading a response left %d unread bytes", sz)
   213  	}
   214  	return err
   215  }