Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

144 lignes
3.6 KiB

  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. // The group protocol type (eg "consumer", "connect")
  28. ProtocolType string
  29. }
  30. func (c *Client) ListGroups(
  31. ctx context.Context,
  32. req *ListGroupsRequest,
  33. ) (*ListGroupsResponse, error) {
  34. protoResp, err := c.roundTrip(ctx, req.Addr, &listgroups.Request{})
  35. if err != nil {
  36. return nil, err
  37. }
  38. apiResp := protoResp.(*listgroups.Response)
  39. resp := &ListGroupsResponse{
  40. Error: makeError(apiResp.ErrorCode, ""),
  41. }
  42. for _, apiGroupInfo := range apiResp.Groups {
  43. resp.Groups = append(resp.Groups, ListGroupsResponseGroup{
  44. GroupID: apiGroupInfo.GroupID,
  45. Coordinator: int(apiGroupInfo.BrokerID),
  46. ProtocolType: apiGroupInfo.ProtocolType,
  47. })
  48. }
  49. return resp, nil
  50. }
  51. // TODO: Remove everything below and use protocol-based version above everywhere.
  52. type listGroupsRequestV1 struct {
  53. }
  54. func (t listGroupsRequestV1) size() int32 {
  55. return 0
  56. }
  57. func (t listGroupsRequestV1) writeTo(wb *writeBuffer) {
  58. }
  59. type listGroupsResponseGroupV1 struct {
  60. // GroupID holds the unique group identifier
  61. GroupID string
  62. ProtocolType string
  63. }
  64. func (t listGroupsResponseGroupV1) size() int32 {
  65. return sizeofString(t.GroupID) + sizeofString(t.ProtocolType)
  66. }
  67. func (t listGroupsResponseGroupV1) writeTo(wb *writeBuffer) {
  68. wb.writeString(t.GroupID)
  69. wb.writeString(t.ProtocolType)
  70. }
  71. func (t *listGroupsResponseGroupV1) readFrom(r *bufio.Reader, size int) (remain int, err error) {
  72. if remain, err = readString(r, size, &t.GroupID); err != nil {
  73. return
  74. }
  75. if remain, err = readString(r, remain, &t.ProtocolType); err != nil {
  76. return
  77. }
  78. return
  79. }
  80. type listGroupsResponseV1 struct {
  81. // ThrottleTimeMS holds the duration in milliseconds for which the request
  82. // was throttled due to quota violation (Zero if the request did not violate
  83. // any quota)
  84. ThrottleTimeMS int32
  85. // ErrorCode holds response error code
  86. ErrorCode int16
  87. Groups []listGroupsResponseGroupV1
  88. }
  89. func (t listGroupsResponseV1) size() int32 {
  90. return sizeofInt32(t.ThrottleTimeMS) +
  91. sizeofInt16(t.ErrorCode) +
  92. sizeofArray(len(t.Groups), func(i int) int32 { return t.Groups[i].size() })
  93. }
  94. func (t listGroupsResponseV1) writeTo(wb *writeBuffer) {
  95. wb.writeInt32(t.ThrottleTimeMS)
  96. wb.writeInt16(t.ErrorCode)
  97. wb.writeArray(len(t.Groups), func(i int) { t.Groups[i].writeTo(wb) })
  98. }
  99. func (t *listGroupsResponseV1) readFrom(r *bufio.Reader, size int) (remain int, err error) {
  100. if remain, err = readInt32(r, size, &t.ThrottleTimeMS); err != nil {
  101. return
  102. }
  103. if remain, err = readInt16(r, remain, &t.ErrorCode); err != nil {
  104. return
  105. }
  106. fn := func(withReader *bufio.Reader, withSize int) (fnRemain int, fnErr error) {
  107. var item listGroupsResponseGroupV1
  108. if fnRemain, fnErr = (&item).readFrom(withReader, withSize); fnErr != nil {
  109. return
  110. }
  111. t.Groups = append(t.Groups, item)
  112. return
  113. }
  114. if remain, err = readArrayWith(r, remain, fn); err != nil {
  115. return
  116. }
  117. return
  118. }