listgroups.go 3.5 KB

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