Skip to content

Commit

Permalink
Merge pull request #239 from stevesg/compound-fix
Browse files Browse the repository at this point in the history
Fix for compound messages containing >255 messages.
  • Loading branch information
markan committed Jan 25, 2022
2 parents 3c0cdff + 8d2a27a commit 3902b1c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
6 changes: 3 additions & 3 deletions memberlist_test.go
Expand Up @@ -1285,9 +1285,9 @@ func TestMemberlist_UserData(t *testing.T) {

bindPort := m1.config.BindPort

bcasts := [][]byte{
[]byte("test"),
[]byte("foobar"),
bcasts := make([][]byte, 256)
for i := range bcasts {
bcasts[i] = []byte(fmt.Sprintf("%d", i))
}

// Create a second node
Expand Down
10 changes: 6 additions & 4 deletions state.go
Expand Up @@ -615,10 +615,12 @@ func (m *Memberlist) gossip() {
m.logger.Printf("[ERR] memberlist: Failed to send gossip to %s: %s", addr, err)
}
} else {
// Otherwise create and send a compound message
compound := makeCompoundMessage(msgs)
if err := m.rawSendMsgPacket(node.FullAddress(), &node, compound.Bytes()); err != nil {
m.logger.Printf("[ERR] memberlist: Failed to send gossip to %s: %s", addr, err)
// Otherwise create and send one or more compound messages
compounds := makeCompoundMessages(msgs)
for _, compound := range compounds {
if err := m.rawSendMsgPacket(node.FullAddress(), &node, compound.Bytes()); err != nil {
m.logger.Printf("[ERR] memberlist: Failed to send gossip to %s: %s", addr, err)
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions util.go
Expand Up @@ -152,6 +152,23 @@ OUTER:
return kNodes
}

// makeCompoundMessages takes a list of messages and packs
// them into one or multiple messages based on the limitations
// of compound messages (255 messages each).
func makeCompoundMessages(msgs [][]byte) []*bytes.Buffer {
const maxMsgs = 255
bufs := make([]*bytes.Buffer, 0, (len(msgs)+(maxMsgs-1))/maxMsgs)

for ; len(msgs) > maxMsgs; msgs = msgs[maxMsgs:] {
bufs = append(bufs, makeCompoundMessage(msgs[:maxMsgs]))
}
if len(msgs) > 0 {
bufs = append(bufs, makeCompoundMessage(msgs))
}

return bufs
}

// makeCompoundMessage takes a list of messages and generates
// a single compound message containing all of them
func makeCompoundMessage(msgs [][]byte) *bytes.Buffer {
Expand Down

0 comments on commit 3902b1c

Please sign in to comment.