| 1 |
package kafka |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/segmentio/kafka-go/protocol/describeclientquotas" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
type DescribeClientQuotasRequest struct { |
| 15 |
|
| 16 |
Addr net.Addr |
| 17 |
|
| 18 |
|
| 19 |
Components []DescribeClientQuotasRequestComponent |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
Strict bool |
| 24 |
} |
| 25 |
|
| 26 |
type DescribeClientQuotasRequestComponent struct { |
| 27 |
|
| 28 |
EntityType string |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
MatchType int8 |
| 33 |
|
| 34 |
|
| 35 |
Match string |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
type DescribeClientQuotasResponse struct { |
| 40 |
|
| 41 |
Throttle time.Duration |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
Error error |
| 46 |
|
| 47 |
|
| 48 |
Entries []DescribeClientQuotasResponseQuotas |
| 49 |
} |
| 50 |
|
| 51 |
type DescribeClientQuotasEntity struct { |
| 52 |
|
| 53 |
EntityType string |
| 54 |
|
| 55 |
|
| 56 |
EntityName string |
| 57 |
} |
| 58 |
|
| 59 |
type DescribeClientQuotasValue struct { |
| 60 |
|
| 61 |
Key string |
| 62 |
|
| 63 |
|
| 64 |
Value float64 |
| 65 |
} |
| 66 |
|
| 67 |
type DescribeClientQuotasResponseQuotas struct { |
| 68 |
|
| 69 |
Entities []DescribeClientQuotasEntity |
| 70 |
|
| 71 |
|
| 72 |
Values []DescribeClientQuotasValue |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func (c *Client) DescribeClientQuotas(ctx context.Context, req *DescribeClientQuotasRequest) (*DescribeClientQuotasResponse, error) { |
| 78 |
components := make([]describeclientquotas.Component, len(req.Components)) |
| 79 |
|
| 80 |
for componentIdx, component := range req.Components { |
| 81 |
components[componentIdx] = describeclientquotas.Component{ |
| 82 |
EntityType: component.EntityType, |
| 83 |
MatchType: component.MatchType, |
| 84 |
Match: component.Match, |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
m, err := c.roundTrip(ctx, req.Addr, &describeclientquotas.Request{ |
| 89 |
Components: components, |
| 90 |
Strict: req.Strict, |
| 91 |
}) |
| 92 |
if err != nil { |
| 93 |
return nil, fmt.Errorf("kafka.(*Client).DescribeClientQuotas: %w", err) |
| 94 |
} |
| 95 |
|
| 96 |
res := m.(*describeclientquotas.Response) |
| 97 |
responseEntries := make([]DescribeClientQuotasResponseQuotas, len(res.Entries)) |
| 98 |
|
| 99 |
for responseEntryIdx, responseEntry := range res.Entries { |
| 100 |
responseEntities := make([]DescribeClientQuotasEntity, len(responseEntry.Entities)) |
| 101 |
for responseEntityIdx, responseEntity := range responseEntry.Entities { |
| 102 |
responseEntities[responseEntityIdx] = DescribeClientQuotasEntity{ |
| 103 |
EntityType: responseEntity.EntityType, |
| 104 |
EntityName: responseEntity.EntityName, |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
responseValues := make([]DescribeClientQuotasValue, len(responseEntry.Values)) |
| 109 |
for responseValueIdx, responseValue := range responseEntry.Values { |
| 110 |
responseValues[responseValueIdx] = DescribeClientQuotasValue{ |
| 111 |
Key: responseValue.Key, |
| 112 |
Value: responseValue.Value, |
| 113 |
} |
| 114 |
} |
| 115 |
responseEntries[responseEntryIdx] = DescribeClientQuotasResponseQuotas{ |
| 116 |
Entities: responseEntities, |
| 117 |
Values: responseValues, |
| 118 |
} |
| 119 |
} |
| 120 |
ret := &DescribeClientQuotasResponse{ |
| 121 |
Throttle: time.Duration(res.ThrottleTimeMs), |
| 122 |
Entries: responseEntries, |
| 123 |
} |
| 124 |
|
| 125 |
return ret, nil |
| 126 |
} |