diff --git a/nameserver/entry.go b/nameserver/entry.go index cd18f6de..cb0830fa 100644 --- a/nameserver/entry.go +++ b/nameserver/entry.go @@ -247,11 +247,7 @@ type GossipData struct { func (g *GossipData) Merge(o mesh.GossipData) mesh.GossipData { other := o.(*GossipData) - gossip := &GossipData{ - Entries: make(Entries, len(g.Entries)), - Timestamp: g.Timestamp, - } - copy(gossip.Entries, g.Entries) + gossip := g.copy() gossip.Entries.merge(other.Entries) if gossip.Timestamp < other.Timestamp { gossip.Timestamp = other.Timestamp @@ -270,9 +266,7 @@ func (g *GossipData) Decode(msg []byte) error { } func (g *GossipData) Encode() [][]byte { - // Make a copy so we can sort: all outgoing data is sent in case-sensitive order - g2 := GossipData{Timestamp: g.Timestamp, Entries: make(Entries, len(g.Entries))} - copy(g2.Entries, g.Entries) + g2 := g.copy() sort.Sort(CaseSensitive(g2.Entries)) buf := &bytes.Buffer{} if err := gob.NewEncoder(buf).Encode(g2); err != nil { @@ -280,3 +274,9 @@ func (g *GossipData) Encode() [][]byte { } return [][]byte{buf.Bytes()} } + +func (g *GossipData) copy() *GossipData { + g2 := &GossipData{Timestamp: g.Timestamp, Entries: make(Entries, len(g.Entries))} + copy(g2.Entries, g.Entries) + return g2 +}