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

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/segmentio/kafka-go/protocol/alterclientquotas"
    10  )
    11  
    12  // AlterClientQuotasRequest represents a request sent to a kafka broker to
    13  // alter client quotas.
    14  type AlterClientQuotasRequest struct {
    15  	// Address of the kafka broker to send the request to.
    16  	Addr net.Addr
    17  
    18  	// List of client quotas entries to alter.
    19  	Entries []AlterClientQuotaEntry
    20  
    21  	// Whether the alteration should be validated, but not performed.
    22  	ValidateOnly bool
    23  }
    24  
    25  type AlterClientQuotaEntry struct {
    26  	// The quota entities to alter.
    27  	Entities []AlterClientQuotaEntity
    28  
    29  	// An individual quota configuration entry to alter.
    30  	Ops []AlterClientQuotaOps
    31  }
    32  
    33  type AlterClientQuotaEntity struct {
    34  	// The quota entity type.
    35  	EntityType string
    36  
    37  	// The name of the quota entity, or null if the default.
    38  	EntityName string
    39  }
    40  
    41  type AlterClientQuotaOps struct {
    42  	// The quota configuration key.
    43  	Key string
    44  
    45  	// The quota configuration value to set, otherwise ignored if the value is to be removed.
    46  	Value float64
    47  
    48  	// Whether the quota configuration value should be removed, otherwise set.
    49  	Remove bool
    50  }
    51  
    52  type AlterClientQuotaResponseQuotas struct {
    53  	// Error is set to a non-nil value including the code and message if a top-level
    54  	// error was encountered when doing the update.
    55  	Error error
    56  
    57  	// The altered quota entities.
    58  	Entities []AlterClientQuotaEntity
    59  }
    60  
    61  // AlterClientQuotasResponse represents a response from a kafka broker to an alter client
    62  // quotas request.
    63  type AlterClientQuotasResponse struct {
    64  	// The amount of time that the broker throttled the request.
    65  	Throttle time.Duration
    66  
    67  	// List of altered client quotas responses.
    68  	Entries []AlterClientQuotaResponseQuotas
    69  }
    70  
    71  // AlterClientQuotas sends client quotas alteration request to a kafka broker and returns
    72  // the response.
    73  func (c *Client) AlterClientQuotas(ctx context.Context, req *AlterClientQuotasRequest) (*AlterClientQuotasResponse, error) {
    74  	entries := make([]alterclientquotas.Entry, len(req.Entries))
    75  
    76  	for entryIdx, entry := range req.Entries {
    77  		entities := make([]alterclientquotas.Entity, len(entry.Entities))
    78  		for entityIdx, entity := range entry.Entities {
    79  			entities[entityIdx] = alterclientquotas.Entity{
    80  				EntityType: entity.EntityType,
    81  				EntityName: entity.EntityName,
    82  			}
    83  		}
    84  
    85  		ops := make([]alterclientquotas.Ops, len(entry.Ops))
    86  		for opsIdx, op := range entry.Ops {
    87  			ops[opsIdx] = alterclientquotas.Ops{
    88  				Key:    op.Key,
    89  				Value:  op.Value,
    90  				Remove: op.Remove,
    91  			}
    92  		}
    93  
    94  		entries[entryIdx] = alterclientquotas.Entry{
    95  			Entities: entities,
    96  			Ops:      ops,
    97  		}
    98  	}
    99  
   100  	m, err := c.roundTrip(ctx, req.Addr, &alterclientquotas.Request{
   101  		Entries:      entries,
   102  		ValidateOnly: req.ValidateOnly,
   103  	})
   104  	if err != nil {
   105  		return nil, fmt.Errorf("kafka.(*Client).AlterClientQuotas: %w", err)
   106  	}
   107  
   108  	res := m.(*alterclientquotas.Response)
   109  	responseEntries := make([]AlterClientQuotaResponseQuotas, len(res.Results))
   110  
   111  	for responseEntryIdx, responseEntry := range res.Results {
   112  		responseEntities := make([]AlterClientQuotaEntity, len(responseEntry.Entities))
   113  		for responseEntityIdx, responseEntity := range responseEntry.Entities {
   114  			responseEntities[responseEntityIdx] = AlterClientQuotaEntity{
   115  				EntityType: responseEntity.EntityType,
   116  				EntityName: responseEntity.EntityName,
   117  			}
   118  		}
   119  
   120  		responseEntries[responseEntryIdx] = AlterClientQuotaResponseQuotas{
   121  			Error:    makeError(responseEntry.ErrorCode, responseEntry.ErrorMessage),
   122  			Entities: responseEntities,
   123  		}
   124  	}
   125  	ret := &AlterClientQuotasResponse{
   126  		Throttle: makeDuration(res.ThrottleTimeMs),
   127  		Entries:  responseEntries,
   128  	}
   129  
   130  	return ret, nil
   131  }