From 95dce910baf3cd459ea5d47eb89698421d5061f0 Mon Sep 17 00:00:00 2001 From: vyzo Date: Wed, 3 Jan 2018 20:30:28 +0200 Subject: [PATCH] narrow the message seqno to 64bit again Uses the atomic counter initialized at instantiation time, which saves a few bytes and a syscall per message. --- floodsub.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/floodsub.go b/floodsub.go index 5a559b2..52a93b9 100644 --- a/floodsub.go +++ b/floodsub.go @@ -95,6 +95,7 @@ func NewFloodSub(ctx context.Context, h host.Host) *PubSub { topics: make(map[string]map[peer.ID]struct{}), peers: make(map[peer.ID]chan *RPC), seenMessages: timecache.NewTimeCache(time.Second * 30), + counter: uint64(time.Now().UnixNano()), } h.SetStreamHandler(ID, ps.handleNewStream) @@ -418,10 +419,9 @@ func (p *PubSub) GetTopics() []string { // Publish publishes data under the given topic func (p *PubSub) Publish(topic string, data []byte) error { - seqno := make([]byte, 16) + seqno := make([]byte, 8) counter := atomic.AddUint64(&p.counter, 1) - binary.BigEndian.PutUint64(seqno[:8], uint64(time.Now().UnixNano())) - binary.BigEndian.PutUint64(seqno[8:], counter) + binary.BigEndian.PutUint64(seqno, counter) p.publish <- &Message{ &pb.Message{