github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/describeclientquotas.go (about)

     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  // DescribeClientQuotasRequest represents a request sent to a kafka broker to
    13  // describe client quotas.
    14  type DescribeClientQuotasRequest struct {
    15  	// Address of the kafka broker to send the request to
    16  	Addr net.Addr
    17  
    18  	// List of quota components to describe.
    19  	Components []DescribeClientQuotasRequestComponent
    20  
    21  	// Whether the match is strict, i.e. should exclude entities with
    22  	// unspecified entity types.
    23  	Strict bool
    24  }
    25  
    26  type DescribeClientQuotasRequestComponent struct {
    27  	// The entity type that the filter component applies to.
    28  	EntityType string
    29  
    30  	// How to match the entity (0 = exact name, 1 = default name,
    31  	// 2 = any specified name).
    32  	MatchType int8
    33  
    34  	// The string to match against, or null if unused for the match type.
    35  	Match string
    36  }
    37  
    38  // DescribeClientQuotasReesponse represents a response from a kafka broker to a describe client quota request.
    39  type DescribeClientQuotasResponse struct {
    40  	// The amount of time that the broker throttled the request.
    41  	Throttle time.Duration
    42  
    43  	// Error is set to a non-nil value including the code and message if a top-level
    44  	// error was encountered when doing the update.
    45  	Error error
    46  
    47  	// List of describe client quota responses.
    48  	Entries []DescribeClientQuotasResponseQuotas
    49  }
    50  
    51  type DescribeClientQuotasEntity struct {
    52  	// The quota entity type.
    53  	EntityType string
    54  
    55  	// The name of the quota entity, or null if the default.
    56  	EntityName string
    57  }
    58  
    59  type DescribeClientQuotasValue struct {
    60  	// The quota configuration key.
    61  	Key string
    62  
    63  	// The quota configuration value.
    64  	Value float64
    65  }
    66  
    67  type DescribeClientQuotasResponseQuotas struct {
    68  	// List of client quota entities and their descriptions.
    69  	Entities []DescribeClientQuotasEntity
    70  
    71  	// The client quota configuration values.
    72  	Values []DescribeClientQuotasValue
    73  }
    74  
    75  // DescribeClientQuotas sends a describe client quotas request to a kafka broker and returns
    76  // the response.
    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  }