github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/listgroups.go (about)

     1  package kafka
     2  
     3  import (
     4  	"bufio"
     5  	"context"
     6  	"net"
     7  
     8  	"github.com/segmentio/kafka-go/protocol/listgroups"
     9  )
    10  
    11  // ListGroupsRequest is a request to the ListGroups API.
    12  type ListGroupsRequest struct {
    13  	// Addr is the address of the kafka broker to send the request to.
    14  	Addr net.Addr
    15  }
    16  
    17  // ListGroupsResponse is a response from the ListGroups API.
    18  type ListGroupsResponse struct {
    19  	// Error is set to a non-nil value if a top-level error occurred while fetching
    20  	// groups.
    21  	Error error
    22  
    23  	// Groups contains the list of groups.
    24  	Groups []ListGroupsResponseGroup
    25  }
    26  
    27  // ListGroupsResponseGroup contains the response details for a single group.
    28  type ListGroupsResponseGroup struct {
    29  	// GroupID is the ID of the group.
    30  	GroupID string
    31  
    32  	// Coordinator is the ID of the coordinator broker for the group.
    33  	Coordinator int
    34  }
    35  
    36  func (c *Client) ListGroups(
    37  	ctx context.Context,
    38  	req *ListGroupsRequest,
    39  ) (*ListGroupsResponse, error) {
    40  	protoResp, err := c.roundTrip(ctx, req.Addr, &listgroups.Request{})
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	apiResp := protoResp.(*listgroups.Response)
    45  	resp := &ListGroupsResponse{
    46  		Error: makeError(apiResp.ErrorCode, ""),
    47  	}
    48  
    49  	for _, apiGroupInfo := range apiResp.Groups {
    50  		resp.Groups = append(resp.Groups, ListGroupsResponseGroup{
    51  			GroupID:     apiGroupInfo.GroupID,
    52  			Coordinator: int(apiGroupInfo.BrokerID),
    53  		})
    54  	}
    55  
    56  	return resp, nil
    57  }
    58  
    59  // TODO: Remove everything below and use protocol-based version above everywhere.
    60  type listGroupsRequestV1 struct {
    61  }
    62  
    63  func (t listGroupsRequestV1) size() int32 {
    64  	return 0
    65  }
    66  
    67  func (t listGroupsRequestV1) writeTo(wb *writeBuffer) {
    68  }
    69  
    70  type listGroupsResponseGroupV1 struct {
    71  	// GroupID holds the unique group identifier
    72  	GroupID      string
    73  	ProtocolType string
    74  }
    75  
    76  func (t listGroupsResponseGroupV1) size() int32 {
    77  	return sizeofString(t.GroupID) + sizeofString(t.ProtocolType)
    78  }
    79  
    80  func (t listGroupsResponseGroupV1) writeTo(wb *writeBuffer) {
    81  	wb.writeString(t.GroupID)
    82  	wb.writeString(t.ProtocolType)
    83  }
    84  
    85  func (t *listGroupsResponseGroupV1) readFrom(r *bufio.Reader, size int) (remain int, err error) {
    86  	if remain, err = readString(r, size, &t.GroupID); err != nil {
    87  		return
    88  	}
    89  	if remain, err = readString(r, remain, &t.ProtocolType); err != nil {
    90  		return
    91  	}
    92  	return
    93  }
    94  
    95  type listGroupsResponseV1 struct {
    96  	// ThrottleTimeMS holds the duration in milliseconds for which the request
    97  	// was throttled due to quota violation (Zero if the request did not violate
    98  	// any quota)
    99  	ThrottleTimeMS int32
   100  
   101  	// ErrorCode holds response error code
   102  	ErrorCode int16
   103  	Groups    []listGroupsResponseGroupV1
   104  }
   105  
   106  func (t listGroupsResponseV1) size() int32 {
   107  	return sizeofInt32(t.ThrottleTimeMS) +
   108  		sizeofInt16(t.ErrorCode) +
   109  		sizeofArray(len(t.Groups), func(i int) int32 { return t.Groups[i].size() })
   110  }
   111  
   112  func (t listGroupsResponseV1) writeTo(wb *writeBuffer) {
   113  	wb.writeInt32(t.ThrottleTimeMS)
   114  	wb.writeInt16(t.ErrorCode)
   115  	wb.writeArray(len(t.Groups), func(i int) { t.Groups[i].writeTo(wb) })
   116  }
   117  
   118  func (t *listGroupsResponseV1) readFrom(r *bufio.Reader, size int) (remain int, err error) {
   119  	if remain, err = readInt32(r, size, &t.ThrottleTimeMS); err != nil {
   120  		return
   121  	}
   122  	if remain, err = readInt16(r, remain, &t.ErrorCode); err != nil {
   123  		return
   124  	}
   125  
   126  	fn := func(withReader *bufio.Reader, withSize int) (fnRemain int, fnErr error) {
   127  		var item listGroupsResponseGroupV1
   128  		if fnRemain, fnErr = (&item).readFrom(withReader, withSize); err != nil {
   129  			return
   130  		}
   131  		t.Groups = append(t.Groups, item)
   132  		return
   133  	}
   134  	if remain, err = readArrayWith(r, remain, fn); err != nil {
   135  		return
   136  	}
   137  
   138  	return
   139  }