join_group_response.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package sarama
  2. import "time"
  3. type JoinGroupResponse struct {
  4. // Version defines the protocol version to use for encode and decode
  5. Version int16
  6. // ThrottleTime contains the duration for which the request was throttled due
  7. // to a quota violation, or zero if the request did not violate any quota.
  8. ThrottleTime int32
  9. // Err contains the error code, or 0 if there was no error.
  10. Err KError
  11. // GenerationId contains the generation ID of the group.
  12. GenerationId int32
  13. // GroupProtocol contains the group protocol selected by the coordinator.
  14. GroupProtocol string
  15. // LeaderId contains the leader of the group.
  16. LeaderId string
  17. // MemberId contains the member ID assigned by the group coordinator.
  18. MemberId string
  19. // Members contains the per-group-member information.
  20. Members []GroupMember
  21. }
  22. type GroupMember struct {
  23. // MemberId contains the group member ID.
  24. MemberId string
  25. // GroupInstanceId contains the unique identifier of the consumer instance
  26. // provided by end user.
  27. GroupInstanceId *string
  28. // Metadata contains the group member metadata.
  29. Metadata []byte
  30. }
  31. func (r *JoinGroupResponse) GetMembers() (map[string]ConsumerGroupMemberMetadata, error) {
  32. members := make(map[string]ConsumerGroupMemberMetadata, len(r.Members))
  33. for _, member := range r.Members {
  34. meta := new(ConsumerGroupMemberMetadata)
  35. if err := decode(member.Metadata, meta, nil); err != nil {
  36. return nil, err
  37. }
  38. members[member.MemberId] = *meta
  39. }
  40. return members, nil
  41. }
  42. func (r *JoinGroupResponse) encode(pe packetEncoder) error {
  43. if r.Version >= 2 {
  44. pe.putInt32(r.ThrottleTime)
  45. }
  46. pe.putInt16(int16(r.Err))
  47. pe.putInt32(r.GenerationId)
  48. if err := pe.putString(r.GroupProtocol); err != nil {
  49. return err
  50. }
  51. if err := pe.putString(r.LeaderId); err != nil {
  52. return err
  53. }
  54. if err := pe.putString(r.MemberId); err != nil {
  55. return err
  56. }
  57. if err := pe.putArrayLength(len(r.Members)); err != nil {
  58. return err
  59. }
  60. for _, member := range r.Members {
  61. if err := pe.putString(member.MemberId); err != nil {
  62. return err
  63. }
  64. if r.Version >= 5 {
  65. if err := pe.putNullableString(member.GroupInstanceId); err != nil {
  66. return err
  67. }
  68. }
  69. if err := pe.putBytes(member.Metadata); err != nil {
  70. return err
  71. }
  72. }
  73. return nil
  74. }
  75. func (r *JoinGroupResponse) decode(pd packetDecoder, version int16) (err error) {
  76. r.Version = version
  77. if version >= 2 {
  78. if r.ThrottleTime, err = pd.getInt32(); err != nil {
  79. return
  80. }
  81. }
  82. kerr, err := pd.getInt16()
  83. if err != nil {
  84. return err
  85. }
  86. r.Err = KError(kerr)
  87. if r.GenerationId, err = pd.getInt32(); err != nil {
  88. return
  89. }
  90. if r.GroupProtocol, err = pd.getString(); err != nil {
  91. return
  92. }
  93. if r.LeaderId, err = pd.getString(); err != nil {
  94. return
  95. }
  96. if r.MemberId, err = pd.getString(); err != nil {
  97. return
  98. }
  99. n, err := pd.getArrayLength()
  100. if err != nil {
  101. return err
  102. }
  103. if n == 0 {
  104. return nil
  105. }
  106. r.Members = make([]GroupMember, n)
  107. for i := 0; i < n; i++ {
  108. memberId, err := pd.getString()
  109. if err != nil {
  110. return err
  111. }
  112. var groupInstanceId *string = nil
  113. if r.Version >= 5 {
  114. groupInstanceId, err = pd.getNullableString()
  115. if err != nil {
  116. return err
  117. }
  118. }
  119. memberMetadata, err := pd.getBytes()
  120. if err != nil {
  121. return err
  122. }
  123. r.Members[i] = GroupMember{MemberId: memberId, GroupInstanceId: groupInstanceId, Metadata: memberMetadata}
  124. }
  125. return nil
  126. }
  127. func (r *JoinGroupResponse) key() int16 {
  128. return 11
  129. }
  130. func (r *JoinGroupResponse) version() int16 {
  131. return r.Version
  132. }
  133. func (r *JoinGroupResponse) headerVersion() int16 {
  134. return 0
  135. }
  136. func (r *JoinGroupResponse) isValidVersion() bool {
  137. return r.Version >= 0 && r.Version <= 5
  138. }
  139. func (r *JoinGroupResponse) requiredVersion() KafkaVersion {
  140. switch r.Version {
  141. case 5:
  142. return V2_3_0_0
  143. case 4:
  144. return V2_2_0_0
  145. case 3:
  146. return V2_0_0_0
  147. case 2:
  148. return V0_11_0_0
  149. case 1:
  150. return V0_10_1_0
  151. case 0:
  152. return V0_10_0_0
  153. default:
  154. return V2_3_0_0
  155. }
  156. }
  157. func (r *JoinGroupResponse) throttleTime() time.Duration {
  158. return time.Duration(r.ThrottleTime) * time.Millisecond
  159. }