-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Group Join request and response pair.
- Loading branch information
1 parent
c36adfa
commit 01e515a
Showing
8 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package sarama | ||
|
||
type JoinGroupRequest struct { | ||
GroupId string | ||
SessionTimeout int32 | ||
MemberId string | ||
ProtocolType string | ||
GroupProtocols []GroupProtocol | ||
} | ||
|
||
func (r *JoinGroupRequest) encode(pe packetEncoder) error { | ||
if err := pe.putString(r.GroupId); err != nil { | ||
return err | ||
} | ||
pe.putInt32(r.SessionTimeout) | ||
if err := pe.putString(r.MemberId); err != nil { | ||
return err | ||
} | ||
if err := pe.putString(r.ProtocolType); err != nil { | ||
return err | ||
} | ||
|
||
if err := pe.putArrayLength(len(r.GroupProtocols)); err != nil { | ||
return err | ||
} | ||
for _, groupProtocol := range r.GroupProtocols { | ||
if err := groupProtocol.encodeGroupProtocol(pe); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *JoinGroupRequest) decode(pd packetDecoder) (err error) { | ||
r.GroupId, err = pd.getString() | ||
if err != nil { | ||
return | ||
} | ||
|
||
r.SessionTimeout, err = pd.getInt32() | ||
if err != nil { | ||
return | ||
} | ||
|
||
r.MemberId, err = pd.getString() | ||
if err != nil { | ||
return | ||
} | ||
|
||
r.ProtocolType, err = pd.getString() | ||
if err != nil { | ||
return | ||
} | ||
|
||
switch r.ProtocolType { | ||
case "consumer": | ||
n, err := pd.getArrayLength() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r.GroupProtocols = make([]GroupProtocol, n) | ||
for i := 0; i < n; i++ { | ||
r.GroupProtocols[i] = new(ConsumerGroupProtocol) | ||
if err := r.GroupProtocols[i].decodeGroupProtocol(pd); err != nil { | ||
return nil | ||
} | ||
} | ||
|
||
default: | ||
return ErrUnknownGroupProtocol | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *JoinGroupRequest) key() int16 { | ||
return 11 | ||
} | ||
|
||
func (r *JoinGroupRequest) version() int16 { | ||
return 0 | ||
} | ||
|
||
type GroupProtocol interface { | ||
encodeGroupProtocol(packetEncoder) error | ||
decodeGroupProtocol(packetDecoder) error | ||
} | ||
|
||
type ConsumerGroupProtocol struct { | ||
ProtocolName string | ||
Version int16 | ||
Subscription []string | ||
UserData []byte | ||
} | ||
|
||
func (cgp *ConsumerGroupProtocol) encodeGroupProtocol(pe packetEncoder) error { | ||
if err := pe.putString(cgp.ProtocolName); err != nil { | ||
return err | ||
} | ||
pe.putInt16(cgp.Version) | ||
if err := pe.putStringArray(cgp.Subscription); err != nil { | ||
return err | ||
} | ||
return pe.putBytes(cgp.UserData) | ||
} | ||
|
||
func (cgp *ConsumerGroupProtocol) decodeGroupProtocol(pd packetDecoder) (err error) { | ||
cgp.ProtocolName, err = pd.getString() | ||
if err != nil { | ||
return | ||
} | ||
|
||
cgp.Version, err = pd.getInt16() | ||
if err != nil { | ||
return | ||
} | ||
|
||
cgp.Subscription, err = pd.getStringArray() | ||
if err != nil { | ||
return | ||
} | ||
|
||
cgp.UserData, err = pd.getBytes() | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package sarama | ||
|
||
type JoinGroupResponse struct { | ||
ErrorCode int16 | ||
GenerationId int32 | ||
GroupProtocol string | ||
LeaderId string | ||
MemberId string | ||
Members []*GroupMember | ||
} | ||
|
||
func (r *JoinGroupResponse) encode(pe packetEncoder) error { | ||
pe.putInt16(r.ErrorCode) | ||
pe.putInt32(r.GenerationId) | ||
|
||
if err := pe.putString(r.GroupProtocol); err != nil { | ||
return err | ||
} | ||
|
||
if err := pe.putString(r.LeaderId); err != nil { | ||
return err | ||
} | ||
|
||
if err := pe.putString(r.MemberId); err != nil { | ||
return err | ||
} | ||
|
||
if err := pe.putArrayLength(len(r.Members)); err != nil { | ||
return err | ||
} | ||
for _, member := range r.Members { | ||
if err := member.encode(pe); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *JoinGroupResponse) decode(pd packetDecoder) (err error) { | ||
r.ErrorCode, err = pd.getInt16() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r.GenerationId, err = pd.getInt32() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r.GroupProtocol, err = pd.getString() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r.LeaderId, err = pd.getString() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r.MemberId, err = pd.getString() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
n, err := pd.getArrayLength() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r.Members = make([]*GroupMember, n) | ||
for i := 0; i < n; i++ { | ||
r.Members[i] = new(GroupMember) | ||
if err := r.Members[i].decode(pd); err != nil { | ||
return nil | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type GroupMember struct { | ||
MemberId string | ||
MemberMetadata []byte | ||
} | ||
|
||
func (gm *GroupMember) encode(pe packetEncoder) error { | ||
if err := pe.putString(gm.MemberId); err != nil { | ||
return err | ||
} | ||
|
||
if err := pe.putBytes(gm.MemberMetadata); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (gm *GroupMember) decode(pd packetDecoder) (err error) { | ||
gm.MemberId, err = pd.getString() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
gm.MemberMetadata, err = pd.getBytes() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters