Open arilotter opened 2 months ago
pub struct SubscribeOpts { pub bootstrap: BTreeSet<NodeId>, pub subscription_capacity: usize, pub filter: Option<Box<dyn Fn(&SubscribeResponse) -> bool + Send + Sync>>, }
I'd say it's not unlikely that the filter function will want to do asynchronous work. E.g. when you want to do a quick DB lookup.
The obvious way would be to just make filter
a Fn
that returns a BoxFuture
, but another way would be to turn the interface inside out, in the external iterators vs. internal iterators kind of sense. I.e. when you receive a message, you get a continuation with it. So unless you call SubscribeResponse::continue_rebroadcast
, it's essentially filtering out the message.
This way you have the context in which you accept subscriptions at your service (variables in scope, an async context, etc.). But it's also a way bigger change & needs some thinking in terms of what this means for the gossip's actor loop, and you can wreak so much more havoc this way.
I was reading thru the codebase and it seems that subscribeopts ends up getting serialized and sent over rpc, which presents a big problem for a function :sweat_smile:
In that case we could split up the subscribe
function into two functions as I described above: One that streams in events of arrived messages, and then the another on the messages themselves to allow explicitly rebroadcasting them.
We could also still keep the original subscribe
function for backwards compatibility.
That said, this is getting out of my depth here, so I'd love to see what some others think. Perhaps @rklaehn ?
We will move gossip to its own repo. We are planning iroh 1.0, and gossip will not be part of that.
Having gossip in a separate repo should make it easier to add such functionality. We will move the issue to the new repo once we do the reorg.
Feature Request: Content-based filtering for Gossip client rebroadcasts
Current Behavior
The Gossip client in Iroh's Node currently rebroadcasts all messages associated with a topic that the node is subscribed to, without any content-based filtering.
Proposed Enhancement
Add the ability to filter rebroadcasts based on message content, not just the topic. This would allow nodes to drop messages that are not relevant for some reason, reducing traffic that would be dropped anyways when read by well-behaved nodes. This could reduce bandwidth for applications where messages can be incorrect.
Use Cases
Dropping malformed messages
Dropping stale messages after some expiry
Dropping messages from unauthenticated users
Proposed Implementation
Current API
The current API unconditionally rebroadcasts any messages with the same gossip topic.
Proposed API
Perhaps we could add filter options to the
SubscribeOpts
struct, letting consumers set a filter when callingsubscribe_with_opts
: