Skip to content

feat: add WithSelfNotification option to filter self-published messages#678

Open
laciferin2024 wants to merge 6 commits intolibp2p:masterfrom
ideo-org:feat/filter-self-messages
Open

feat: add WithSelfNotification option to filter self-published messages#678
laciferin2024 wants to merge 6 commits intolibp2p:masterfrom
ideo-org:feat/filter-self-messages

Conversation

@laciferin2024
Copy link

Summary

Adds a WithSelfNotification(enabled bool) subscribe option that allows subscribers to opt out of receiving messages published by the local node. This addresses a common use case where applications need to avoid processing their own published messages.

Closes #537

Changes

  • Added WithSelfNotification(enabled bool) as a new SubOpt for Topic.Subscribe()
  • When set to false, messages published by the local host are filtered out in notifySubs before delivery
  • Default behavior is unchanged — subscriptions still receive all messages, including self-published ones
  • Added test TestWithSelfNotification covering:
    • Self-published messages are skipped on filtered subscriptions
    • Self-published messages still arrive on default subscriptions
    • Messages from remote peers still arrive on filtered subscriptions

Design

The filtering happens at the notifySubs level rather than in Next(), so self-published messages never enter the subscription channel buffer. This avoids wasting buffer space and prevents unnecessary drops when the subscriber is slow.

Each subscription stores the local host's peer ID at creation time and compares it against msg.ReceivedFrom during delivery.

…hed messages

Add a SubOpt that allows subscribers to opt out of receiving messages
published by the local node. This is useful for applications where
processing self-published messages is unnecessary overhead.

The default behavior is unchanged - subscriptions still receive all
messages including self-published ones. Passing WithSelfNotification(false)
filters them out at the delivery level in notifySubs.

Closes libp2p#537
@MarcoPolo
Copy link
Contributor

Did you need this feature? Why was skipping the message on the application side not good enough?

@laciferin2024
Copy link
Author

Did you need this feature? Why was skipping the message on the application side not good enough?

The issue with filtering on the application side (i.e. in the handler after Next()) is that the message has already been delivered into the subscription's channel buffer. For slow subscribers this means the buffer fills up with self-published messages that the app will just discard, which can cause legitimate remote messages to be dropped. Filtering at notifySubs prevents self-published messages from ever entering the buffer, so the buffer capacity is fully available for messages the subscriber actually wants. That was the main reason to do it at this layer rather than leaving it to the caller.

@MarcoPolo
Copy link
Contributor

Realistically a single equality check by the handler is not going to slow the subscriber enough to cause it to drop messages. Did you see this issue in production?

@laciferin2024 laciferin2024 force-pushed the feat/filter-self-messages branch from 798fd35 to ee12f38 Compare March 3, 2026 05:41
@laciferin2024
Copy link
Author

Realistically a single equality check by the handler is not going to slow the subscriber enough to cause it to drop messages. Did you see this issue in production?

With tight buffer sizes yes, this filter auto drops self messages.

Just to give u my story:

  1. Processing in heart beat intervals with 'N' goroutines . A large fraction of those goroutines are consumed by self messages that are dropped.

@MarcoPolo
Copy link
Contributor

MarcoPolo commented Mar 3, 2026

Let's change this to be an option that sets a message subscription filter function that can be passed in as a SubOpt. Then the application can filter out messages where ReceivedFrom == self, and this can happen in notifySubs

@vyzo
Copy link
Collaborator

vyzo commented Mar 3, 2026

precedent from IP Multicast here too, this is a sockopt in POSIX.

@twenty-tools twenty-tools force-pushed the feat/filter-self-messages branch from 464d6f7 to c3b94c8 Compare March 3, 2026 22:41
@laciferin2024
Copy link
Author

Let's change this to be an option that sets a message subscription filter function that can be passed in as a SubOpt. Then the application can filter out messages where ReceivedFrom == self, and this can happen in notifySubs

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Next function returns own messages

3 participants