mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-01-07 15:23:08 +00:00
Merge pull request #53 from libp2p/fix/atomic-counter
Use atomic counter for message seqnos
This commit is contained in:
commit
979ae33fc7
10
floodsub.go
10
floodsub.go
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
pb "github.com/libp2p/go-floodsub/pb"
|
pb "github.com/libp2p/go-floodsub/pb"
|
||||||
@ -57,6 +58,9 @@ type PubSub struct {
|
|||||||
seenMessages *timecache.TimeCache
|
seenMessages *timecache.TimeCache
|
||||||
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
|
||||||
|
// atomic counter for seqnos
|
||||||
|
counter uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
@ -414,8 +418,10 @@ func (p *PubSub) GetTopics() []string {
|
|||||||
|
|
||||||
// Publish publishes data under the given topic
|
// Publish publishes data under the given topic
|
||||||
func (p *PubSub) Publish(topic string, data []byte) error {
|
func (p *PubSub) Publish(topic string, data []byte) error {
|
||||||
seqno := make([]byte, 8)
|
seqno := make([]byte, 16)
|
||||||
binary.BigEndian.PutUint64(seqno, uint64(time.Now().UnixNano()))
|
counter := atomic.AddUint64(&p.counter, 1)
|
||||||
|
binary.BigEndian.PutUint64(seqno[:8], uint64(time.Now().UnixNano()))
|
||||||
|
binary.BigEndian.PutUint64(seqno[8:], counter)
|
||||||
|
|
||||||
p.publish <- &Message{
|
p.publish <- &Message{
|
||||||
&pb.Message{
|
&pb.Message{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user