describeclientquotas.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package kafka
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "time"
  7. "github.com/segmentio/kafka-go/protocol/describeclientquotas"
  8. )
  9. // DescribeClientQuotasRequest represents a request sent to a kafka broker to
  10. // describe client quotas.
  11. type DescribeClientQuotasRequest struct {
  12. // Address of the kafka broker to send the request to
  13. Addr net.Addr
  14. // List of quota components to describe.
  15. Components []DescribeClientQuotasRequestComponent
  16. // Whether the match is strict, i.e. should exclude entities with
  17. // unspecified entity types.
  18. Strict bool
  19. }
  20. type DescribeClientQuotasRequestComponent struct {
  21. // The entity type that the filter component applies to.
  22. EntityType string
  23. // How to match the entity (0 = exact name, 1 = default name,
  24. // 2 = any specified name).
  25. MatchType int8
  26. // The string to match against, or null if unused for the match type.
  27. Match string
  28. }
  29. // DescribeClientQuotasReesponse represents a response from a kafka broker to a describe client quota request.
  30. type DescribeClientQuotasResponse struct {
  31. // The amount of time that the broker throttled the request.
  32. Throttle time.Duration
  33. // Error is set to a non-nil value including the code and message if a top-level
  34. // error was encountered when doing the update.
  35. Error error
  36. // List of describe client quota responses.
  37. Entries []DescribeClientQuotasResponseQuotas
  38. }
  39. type DescribeClientQuotasEntity struct {
  40. // The quota entity type.
  41. EntityType string
  42. // The name of the quota entity, or null if the default.
  43. EntityName string
  44. }
  45. type DescribeClientQuotasValue struct {
  46. // The quota configuration key.
  47. Key string
  48. // The quota configuration value.
  49. Value float64
  50. }
  51. type DescribeClientQuotasResponseQuotas struct {
  52. // List of client quota entities and their descriptions.
  53. Entities []DescribeClientQuotasEntity
  54. // The client quota configuration values.
  55. Values []DescribeClientQuotasValue
  56. }
  57. // DescribeClientQuotas sends a describe client quotas request to a kafka broker and returns
  58. // the response.
  59. func (c *Client) DescribeClientQuotas(ctx context.Context, req *DescribeClientQuotasRequest) (*DescribeClientQuotasResponse, error) {
  60. components := make([]describeclientquotas.Component, len(req.Components))
  61. for componentIdx, component := range req.Components {
  62. components[componentIdx] = describeclientquotas.Component{
  63. EntityType: component.EntityType,
  64. MatchType: component.MatchType,
  65. Match: component.Match,
  66. }
  67. }
  68. m, err := c.roundTrip(ctx, req.Addr, &describeclientquotas.Request{
  69. Components: components,
  70. Strict: req.Strict,
  71. })
  72. if err != nil {
  73. return nil, fmt.Errorf("kafka.(*Client).DescribeClientQuotas: %w", err)
  74. }
  75. res := m.(*describeclientquotas.Response)
  76. responseEntries := make([]DescribeClientQuotasResponseQuotas, len(res.Entries))
  77. for responseEntryIdx, responseEntry := range res.Entries {
  78. responseEntities := make([]DescribeClientQuotasEntity, len(responseEntry.Entities))
  79. for responseEntityIdx, responseEntity := range responseEntry.Entities {
  80. responseEntities[responseEntityIdx] = DescribeClientQuotasEntity{
  81. EntityType: responseEntity.EntityType,
  82. EntityName: responseEntity.EntityName,
  83. }
  84. }
  85. responseValues := make([]DescribeClientQuotasValue, len(responseEntry.Values))
  86. for responseValueIdx, responseValue := range responseEntry.Values {
  87. responseValues[responseValueIdx] = DescribeClientQuotasValue{
  88. Key: responseValue.Key,
  89. Value: responseValue.Value,
  90. }
  91. }
  92. responseEntries[responseEntryIdx] = DescribeClientQuotasResponseQuotas{
  93. Entities: responseEntities,
  94. Values: responseValues,
  95. }
  96. }
  97. ret := &DescribeClientQuotasResponse{
  98. Throttle: time.Duration(res.ThrottleTimeMs),
  99. Entries: responseEntries,
  100. }
  101. return ret, nil
  102. }