Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added subscription specific configurations to PubSub #171

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions gossipconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package pubsub

import (
peer "github.com/libp2p/go-libp2p-peer"
protocol "github.com/libp2p/go-libp2p-protocol"

pb "github.com/libp2p/go-libp2p-pubsub/pb"
)

//HandleRPC, handleIHave, handleIWant, Publish, pushGossip

type MessageCacher interface {
MessageCacheReader
Put(msg *pb.Message)
}

type ClassicGossipSubConfiguration struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up for a better name if you've got one

mcache MessageCacher
supportedProtocols []protocol.ID
protocol protocol.ID
}

func (gs *ClassicGossipSubConfiguration) GetCacher() MessageCacheReader {
return gs.mcache
}

func (gs *ClassicGossipSubConfiguration) SupportedProtocols() []protocol.ID {
return gs.supportedProtocols
}
func (gs *ClassicGossipSubConfiguration) Protocol() protocol.ID {
return gs.protocol
}

func (gs *ClassicGossipSubConfiguration) Publish(rt *GossipConfigurableRouter, from peer.ID, msg *pb.Message) {
gs.mcache.Put(msg)

tosend := make(map[peer.ID]struct{})
rt.AddGossipPeers(tosend, msg.GetTopicIDs(), true)

_, ok := tosend[from]
if ok {
delete(tosend, from)
}

calculatedFrom := peer.ID(msg.GetFrom())
_, ok = tosend[calculatedFrom]
if ok {
delete(tosend, calculatedFrom)
}

rt.PropagateMSG(tosend, msg)

}
76 changes: 76 additions & 0 deletions gossiplww.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package pubsub

import (
"context"

host "github.com/libp2p/go-libp2p-host"
protocol "github.com/libp2p/go-libp2p-protocol"

pb "github.com/libp2p/go-libp2p-pubsub/pb"
)

type LWWMessageCache struct {
IsNewerThan func(currentMsg, incomingMsg *pb.Message) bool
ComputeID func(msg *pb.Message) string
topicMsgIDMap map[string]string
IDMsgMap map[string]*pb.Message
}

func NewLWWMessageCache(IsNewerThan func(currentMsg, incomingMsg *pb.Message) bool, ComputeID func(msg *pb.Message) string) *LWWMessageCache {
return &LWWMessageCache{
topicMsgIDMap: make(map[string]string),
IDMsgMap: make(map[string]*pb.Message),
IsNewerThan: IsNewerThan,
ComputeID: ComputeID,
}
}

func (mc *LWWMessageCache) Put(msg *pb.Message) {
mid := mc.ComputeID(msg)
_, ok := mc.IDMsgMap[mid]
if !ok {
mc.IDMsgMap[mid] = msg
}

for _, topic := range msg.TopicIDs {
lastMsgID, ok := mc.topicMsgIDMap[topic]
if !ok {
mc.topicMsgIDMap[topic] = mid
continue
}
lastMsg, ok := mc.IDMsgMap[lastMsgID]
if !ok {
continue
}
if mc.IsNewerThan(msg, lastMsg) {
mc.topicMsgIDMap[topic] = mid
}
}
}

func (mc *LWWMessageCache) Get(mid string) (*pb.Message, bool) {
m, ok := mc.IDMsgMap[mid]
return m, ok
}

func (mc *LWWMessageCache) GetGossipIDs(topic string) []string {
mid, ok := mc.topicMsgIDMap[topic]
if ok {
return []string{mid}
}
return []string{}
}

func (mc *LWWMessageCache) Shift() {}

var _ MessageCacheReader = (*LWWMessageCache)(nil)

// NewGossipBaseSub returns a new PubSub object using GossipSubRouter as the router.
func NewGossipSyncLWW(ctx context.Context, h host.Host, mcache *LWWMessageCache, protocolID protocol.ID, opts ...Option) (*PubSub, error) {
rt := NewGossipConfigurableRouter(&ClassicGossipSubConfiguration{
mcache: mcache,
supportedProtocols: []protocol.ID{protocolID},
protocol: protocolID,
})
return NewPubSub(ctx, h, rt, append([]Option{WithRouterConfiguration(rt)}, opts...)...)
}
Loading