blob: e993432d0e6ce0284c758a65d6b15a5ea88e8e76 [file] [log] [blame]
Abhay Kumara2ae5992025-11-10 14:02:24 +00001package sarama
2
3type apiVersionRange struct {
4 minVersion int16
5 maxVersion int16
6}
7
8type apiVersionMap map[int16]*apiVersionRange
9
10// restrictApiVersion selects the appropriate API version for a given protocol body according to
11// the client and broker version ranges. By default, it selects the maximum version supported by both
12// client and broker, capped by the maximum Kafka version from Config.
13// It then calls setVersion() on the protocol body.
14// If no valid version is found, an error is returned.
15func restrictApiVersion(pb protocolBody, brokerVersions apiVersionMap) error {
16 key := pb.key()
17 // Since message constructors take a Kafka version and select the maximum supported protocol version already, we can
18 // rely on pb.version() being the max version supported for the user-selected Kafka API version.
19 clientMax := pb.version()
20
21 if brokerVersionRange := brokerVersions[key]; brokerVersionRange != nil {
22 // Select the maximum version that both client and server support
23 // Clamp to the client max to respect user preference above broker advertised version range
24 pb.setVersion(min(clientMax, max(min(clientMax, brokerVersionRange.maxVersion), brokerVersionRange.minVersion)))
25 return nil
26 }
27
28 return nil // no version ranges available, no restriction
29}
30
31const (
32 apiKeyProduce = 0
33 apiKeyFetch = 1
34 apiKeyListOffsets = 2
35 apiKeyMetadata = 3
36 apiKeyLeaderAndIsr = 4
37 apiKeyStopReplica = 5
38 apiKeyUpdateMetadata = 6
39 apiKeyControlledShutdown = 7
40 apiKeyOffsetCommit = 8
41 apiKeyOffsetFetch = 9
42 apiKeyFindCoordinator = 10
43 apiKeyJoinGroup = 11
44 apiKeyHeartbeat = 12
45 apiKeyLeaveGroup = 13
46 apiKeySyncGroup = 14
47 apiKeyDescribeGroups = 15
48 apiKeyListGroups = 16
49 apiKeySaslHandshake = 17
50 apiKeyApiVersions = 18
51 apiKeyCreateTopics = 19
52 apiKeyDeleteTopics = 20
53 apiKeyDeleteRecords = 21
54 apiKeyInitProducerId = 22
55 apiKeyOffsetForLeaderEpoch = 23
56 apiKeyAddPartitionsToTxn = 24
57 apiKeyAddOffsetsToTxn = 25
58 apiKeyEndTxn = 26
59 apiKeyWriteTxnMarkers = 27
60 apiKeyTxnOffsetCommit = 28
61 apiKeyDescribeAcls = 29
62 apiKeyCreateAcls = 30
63 apiKeyDeleteAcls = 31
64 apiKeyDescribeConfigs = 32
65 apiKeyAlterConfigs = 33
66 apiKeyAlterReplicaLogDirs = 34
67 apiKeyDescribeLogDirs = 35
68 apiKeySASLAuth = 36
69 apiKeyCreatePartitions = 37
70 apiKeyCreateDelegationToken = 38
71 apiKeyRenewDelegationToken = 39
72 apiKeyExpireDelegationToken = 40
73 apiKeyDescribeDelegationToken = 41
74 apiKeyDeleteGroups = 42
75 apiKeyElectLeaders = 43
76 apiKeyIncrementalAlterConfigs = 44
77 apiKeyAlterPartitionReassignments = 45
78 apiKeyListPartitionReassignments = 46
79 apiKeyOffsetDelete = 47
80 apiKeyDescribeClientQuotas = 48
81 apiKeyAlterClientQuotas = 49
82 apiKeyDescribeUserScramCredentials = 50
83 apiKeyAlterUserScramCredentials = 51
84)