Merge pull request #54 from libp2p/feat/better-atomic-counter

Narrow the message seqno to 64bit
This commit is contained in:
vyzo 2018-01-03 22:43:49 +02:00 committed by GitHub
commit 0ec8f2fa08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 3 deletions

View File

@ -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{