github.com/hoveychen/kafka-go@v0.4.42/protocol/listgroups/listgroups.go (about)

     1  package listgroups
     2  
     3  import (
     4  	"github.com/hoveychen/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  		m, err := protocol.Result(result)
    61  		if err != nil {
    62  			return nil, err
    63  		}
    64  		brokerResp := m.(*Response)
    65  		respGroups := []ResponseGroup{}
    66  
    67  		for _, brokerResp := range brokerResp.Groups {
    68  			respGroups = append(
    69  				respGroups,
    70  				ResponseGroup{
    71  					GroupID:      brokerResp.GroupID,
    72  					ProtocolType: brokerResp.ProtocolType,
    73  					BrokerID:     requests[r].(*Request).brokerID,
    74  				},
    75  			)
    76  		}
    77  
    78  		response.Groups = append(response.Groups, respGroups...)
    79  	}
    80  
    81  	return response, nil
    82  }