-
Notifications
You must be signed in to change notification settings - Fork 186
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
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...)...) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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