-
Notifications
You must be signed in to change notification settings - Fork 190
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
WithWaitUntilQueued option for topic.Publish #232
Closed
aarshkshah1992
wants to merge
3
commits into
libp2p:master
from
aarshkshah1992:feat/Publish-WaitUntilQueued
+320
−37
Closed
Changes from 2 commits
Commits
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
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
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
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
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 |
---|---|---|
|
@@ -6,14 +6,21 @@ import ( | |
"fmt" | ||
"sync" | ||
|
||
logging "github.com/ipfs/go-log" | ||
pb "github.com/libp2p/go-libp2p-pubsub/pb" | ||
|
||
"github.com/libp2p/go-libp2p-core/peer" | ||
pkgerrors "github.com/pkg/errors" | ||
) | ||
|
||
// ErrTopicClosed is returned if a Topic is utilized after it has been closed | ||
var ErrTopicClosed = errors.New("this Topic is closed, try opening a new one") | ||
|
||
// ErrFailedToAddToPeerQueue is returned if we fail to achieve the desired target for WithWaitUntilQueued because of full outbound peer queues | ||
var ErrFailedToAddToPeerQueue = errors.New("failed to achieve desired WithWaitUntilQueued target") | ||
|
||
var topicHandleLog = logging.Logger("topicHandle") | ||
|
||
// Topic is the handle for a pubsub topic | ||
type Topic struct { | ||
p *PubSub | ||
|
@@ -125,7 +132,8 @@ func (t *Topic) Subscribe(opts ...SubOpt) (*Subscription, error) { | |
type RouterReady func(rt PubSubRouter, topic string) (bool, error) | ||
|
||
type PublishOptions struct { | ||
ready RouterReady | ||
ready RouterReady | ||
nQueuedNotifs int | ||
} | ||
|
||
type PubOpt func(pub *PublishOptions) error | ||
|
@@ -166,12 +174,110 @@ func (t *Topic) Publish(ctx context.Context, data []byte, opts ...PubOpt) error | |
t.p.disc.Bootstrap(ctx, t.topic, pub.ready) | ||
} | ||
|
||
var waitForMsgQueuedNotifications bool | ||
var msgQueuedTargetAchieved chan error | ||
// setup for receiving notifications when a message is added to a peer's outbound queue | ||
if pub.nQueuedNotifs != 0 { | ||
waitForMsgQueuedNotifications = true | ||
|
||
// create & register the listener | ||
listenerContext, cancel := context.WithCancel(ctx) | ||
notifChan := make(chan *msgQueuedNotification) | ||
listener := &msgQueuedEventListener{listenerContext, notifChan} | ||
|
||
done := make(chan struct{}, 1) | ||
select { | ||
case t.p.eval <- func() { | ||
t.p.msgQueuedEventListeners[msgID(m)] = listener | ||
done <- struct{}{} | ||
}: | ||
case <-t.p.ctx.Done(): | ||
return t.p.ctx.Err() | ||
} | ||
<-done | ||
|
||
// remove the listener & cancel the listener context before we return | ||
defer func() { | ||
cancel() | ||
|
||
done := make(chan struct{}, 1) | ||
select { | ||
case t.p.eval <- func() { | ||
delete(t.p.msgQueuedEventListeners, msgID(m)) | ||
done <- struct{}{} | ||
}: | ||
case <-t.p.ctx.Done(): | ||
return | ||
} | ||
<-done | ||
}() | ||
|
||
// start listening to notifications | ||
msgQueuedTargetAchieved = make(chan error, 1) | ||
go func() { | ||
nSuccess := 0 | ||
var failedPeers []peer.ID | ||
|
||
for { | ||
select { | ||
case <-listenerContext.Done(): | ||
return | ||
case notif, ok := <-notifChan: | ||
if !ok { | ||
// notification channel is closed | ||
|
||
if pub.nQueuedNotifs == -1 { | ||
if len(failedPeers) == 0 { | ||
msgQueuedTargetAchieved <- nil | ||
} else { | ||
topicHandleLog.Warningf("Publish: failed on the -1/ALL option: failed to add messageID %s to queues for peers %v", | ||
msgID(m), failedPeers) | ||
|
||
msgQueuedTargetAchieved <- ErrFailedToAddToPeerQueue | ||
} | ||
} else { | ||
topicHandleLog.Warningf("Publish: did not achieve desired count: "+ | ||
"failed to add messageID %s to queues for peers %v, success count is %d", msgID(m), failedPeers, nSuccess) | ||
|
||
msgQueuedTargetAchieved <- ErrFailedToAddToPeerQueue | ||
} | ||
return | ||
} else { | ||
if !notif.success { | ||
failedPeers = append(failedPeers, notif.peer) | ||
} else { | ||
nSuccess++ | ||
if pub.nQueuedNotifs != -1 { | ||
if nSuccess == pub.nQueuedNotifs { | ||
msgQueuedTargetAchieved <- nil | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}() | ||
} | ||
|
||
select { | ||
case t.p.publish <- &Message{m, id, nil}: | ||
case <-t.p.ctx.Done(): | ||
return t.p.ctx.Err() | ||
} | ||
|
||
// wait for msg queued notifications | ||
if waitForMsgQueuedNotifications { | ||
select { | ||
case err := <-msgQueuedTargetAchieved: | ||
return err | ||
case <-ctx.Done(): | ||
return pkgerrors.Wrap(ctx.Err(), "context expired while waiting for msg queued notifs") | ||
case <-t.p.ctx.Done(): | ||
return pkgerrors.Wrap(t.p.ctx.Err(), "pubsub context expired while waiting for msg queued notifs") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -184,6 +290,23 @@ func WithReadiness(ready RouterReady) PubOpt { | |
} | ||
} | ||
|
||
// WithWaitUntilQueued blocks the publish until the message has been added to the outbound message queue for | ||
// as many peers as the arg indicates | ||
// A value of -1 means all peers in mesh/fanout for gossipsub & all subscribed peers in floodsub | ||
// Please note that if nPeers is -1, the behavior is not fail fast | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does a "non fail fast" behaviour make sense here ? If the caller is willing to wait for the message to be added to the queues for all peers, I figured we might as well wait till the end so we can log all failures for better debugging later. |
||
// If we fail to achieve the desired target because of full outbound peer queues, Publish will return ErrFailedToAddToPeerQueues | ||
// However, the message could still have been added to the outbound queue for other peers | ||
func WithWaitUntilQueued(nPeers int) PubOpt { | ||
return func(pub *PublishOptions) error { | ||
if nPeers < -1 { | ||
return errors.New("nPeers should be greater than or equal to -1, please refer to the docs for WithWaitUntilQueued") | ||
} | ||
|
||
pub.nQueuedNotifs = nPeers | ||
return nil | ||
} | ||
} | ||
|
||
// Close closes down the topic. Will return an error unless there are no active event handlers or subscriptions. | ||
// Does not error if the topic is already closed. | ||
func (t *Topic) Close() error { | ||
|
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.
This is way too heavy, it is going to spam with events for every message sent.
Also note that the channel could block the event loop, which is a no,no.
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.
@vyzo
I see what you mean.
We could use a buffered channel here & drop the event if the buffer is full.
But, that could lead to lost events & Publish would end up erroneously reporting that it wasn't able to fulfil the target.
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.
So yeah, this seems quite impossible to implement correctly.
We can't block the event loop and we don't want to erroneously report non-delivery (or not report at all).
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.
@raulk @aschmahmann Wdyt ? Can we use a buffered channel here & do the
WaitUntilQueued
on a "best effort" basis ?Or, is there a smarter way to do this ?
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.
we could launch a goroutine for every channel put, which would side-step the problem. But I worry about potentially unbounded number of goroutines running if the receiver is stalling.
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.
@vyzo @aschmahmann Aha, here is one thing that can work:
For WaitUntilNQueued:
Buffer size = N
& the event loop ONLY writes success cases to the channelFor WaitiUntillAllQueued:
This is slightly tricky as I've explained here. But, we can do it like so:
Buffer size = len(pubsub.topics[topic])
(number of peers interested in the topic) when Publish is calledLet me know what you think :)
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.
Hey @aschmahmann @vyzo
Please take a look at this when you can.