github.com/hack0072008/kafka-go@v1.0.1/protocol/listgroups/listgroups.go (about)

     1  package listgroups
     2  
     3  import (
     4  	"github.com/hack0072008/kafka-go/protocol"
     5  )
     6  
     7  func init() {
     8  	protocol.Register(&Request{}, &Response{})
     9  }
    10  
    11  // Detailed API definition: https://kafka.apache.org/protocol#The_Messages_ListGroups
    12  type Request struct {
    13  	_        struct{} `kafka:"min=v0,max=v2"`
    14  	brokerID int32
    15  }
    16  
    17  func (r *Request) ApiKey() protocol.ApiKey { return protocol.ListGroups }
    18  
    19  func (r *Request) Broker(cluster protocol.Cluster) (protocol.Broker, error) {
    20  	return cluster.Brokers[r.brokerID], nil
    21  }
    22  
    23  func (r *Request) Split(cluster protocol.Cluster) (
    24  	[]protocol.Message,
    25  	protocol.Merger,
    26  	error,
    27  ) {
    28  	messages := []protocol.Message{}
    29  
    30  	for _, broker := range cluster.Brokers {
    31  		messages = append(messages, &Request{brokerID: broker.ID})
    32  	}
    33  
    34  	return messages, new(Response), nil
    35  }
    36  
    37  type Response struct {
    38  	ThrottleTimeMs int32           `kafka:"min=v1,max=v2"`
    39  	ErrorCode      int16           `kafka:"min=v0,max=v2"`
    40  	Groups         []ResponseGroup `kafka:"min=v0,max=v2"`
    41  }
    42  
    43  type ResponseGroup struct {
    44  	GroupID      string `kafka:"min=v0,max=v2"`
    45  	ProtocolType string `kafka:"min=v0,max=v2"`
    46  
    47  	// Use this to store which broker returned the response
    48  	BrokerID int32 `kafka:"-"`
    49  }
    50  
    51  func (r *Response) ApiKey() protocol.ApiKey { return protocol.ListGroups }
    52  
    53  func (r *Response) Merge(requests []protocol.Message, results []interface{}) (
    54  	protocol.Message,
    55  	error,
    56  ) {
    57  	response := &Response{}
    58  
    59  	for r, result := range results {
    60  		brokerResp := result.(*Response)
    61  		respGroups := []ResponseGroup{}
    62  
    63  		for _, brokerResp := range brokerResp.Groups {
    64  			respGroups = append(
    65  				respGroups,
    66  				ResponseGroup{
    67  					GroupID:      brokerResp.GroupID,
    68  					ProtocolType: brokerResp.ProtocolType,
    69  					BrokerID:     requests[r].(*Request).brokerID,
    70  				},
    71  			)
    72  		}
    73  
    74  		response.Groups = append(response.Groups, brokerResp.Groups...)
    75  	}
    76  
    77  	return response, nil
    78  }