pub fn notification_sender(
		&self,
		target: PeerId,
		protocol: Cow<'static, str>,
	) -> Result<NotificationSender, NotificationSenderError> {
		// We clone the `NotificationsSink` in order to be able to unlock the network-wide
		// `peers_notifications_sinks` mutex as soon as possible.
		let sink = {
			let peers_notifications_sinks = self.peers_notifications_sinks.lock();
			if let Some(sink) = peers_notifications_sinks.get(&(target, protocol.clone())) {
				sink.clone()
			} else {
				return Err(NotificationSenderError::Closed);
			}
		};

		let notification_size_metric = self.notifications_sizes_metric.as_ref().map(|histogram| {
			histogram.with_label_values(&["out", &protocol])
		});

		Ok(NotificationSender {
			sink,
			protocol_name: protocol,
			notification_size_metric,
		})
	}
/// Obtains a [`NotificationSender`] for a connected peer, if it exists.
///
/// A `NotificationSender` is scoped to a particular connection to the peer that holds
/// a receiver. With a `NotificationSender` at hand, sending a notification is done in two steps:
///
/// 1.  [`NotificationSender::ready`] is used to wait for the sender to become ready
/// for another notification, yielding a [`NotificationSenderReady`] token.
/// 2.  [`NotificationSenderReady::send`] enqueues the notification for sending. This operation
/// can only fail if the underlying notification substream or connection has suddenly closed.
///
/// An error is returned by [`NotificationSenderReady::send`] if there exists no open
/// notifications substream with that combination of peer and protocol, or if the remote
/// has asked to close the notifications substream. If that happens, it is guaranteed that an
/// [`Event::NotificationStreamClosed`] has been generated on the stream returned by
/// [`NetworkService::event_stream`].
///
/// If the remote requests to close the notifications substream, all notifications successfully
/// enqueued using [`NotificationSenderReady::send`] will finish being sent out before the
/// substream actually gets closed, but attempting to enqueue more notifications will now
/// return an error. It is however possible for the entire connection to be abruptly closed,
/// in which case enqueued notifications will be lost.
///
/// The protocol must have been registered with
/// [`NetworkConfiguration::notifications_protocols`](crate::config::NetworkConfiguration::notifications_protocols).
///
/// # Usage
///
/// This method returns a struct that allows waiting until there is space available in the
/// buffer of messages towards the given peer. If the peer processes notifications at a slower
/// rate than we send them, this buffer will quickly fill up.
///
/// As such, you should never do something like this:
///
/// ```ignore
/// // Do NOT do this
/// for peer in peers {
/// 	if let Ok(n) = network.notification_sender(peer, ...) {
///			if let Ok(s) = n.ready().await {
///				let _ = s.send(...);
///			}
/// 	}
/// }
/// ```
///
/// Doing so would slow down all peers to the rate of the slowest one. A malicious or
/// malfunctioning peer could intentionally process notifications at a very slow rate.
///
/// Instead, you are encouraged to maintain your own buffer of notifications on top of the one
/// maintained by `sc-network`, and use `notification_sender` to progressively send out
/// elements from your buffer. If this additional buffer is full (which will happen at some
/// point if the peer is too slow to process notifications), appropriate measures can be taken,
/// such as removing non-critical notifications from the buffer or disconnecting the peer
/// using [`NetworkService::disconnect_peer`].
///
///
/// Notifications              Per-peer buffer
///   broadcast    +------->   of notifications   +-->  `notification_sender`  +-->  Internet
///                    ^       (not covered by
///                    |         sc-network)
///                    +
///      Notifications should be dropped
///             if buffer is full
///
///
/// See also the [`gossip`](crate::gossip) module for a higher-level way to send
/// notifications.
///
Obtains a [`NotificationSender`] for a connected peer, if it exists.
A `NotificationSender` is scoped to a particular connection to the peer that holds
a receiver. With a `NotificationSender` at hand, sending a notification is done in two steps.

1.  [`NotificationSender::ready`] is used to wait for the sender to become ready
for another notification, yielding a [`NotificationSenderReady`] token.
2.  [`NotificationSenderReady::send`] enqueues the notification for sending. This operation
can only fail if the underlying notification substream or connection has suddenly closed.

An error is returned by [`NotificationSenderReady::send`] if there exists no open
notifications substream with that combination of peer and protocol, or if the remote
has asked to close the notifications substream. If that happens, it is guaranteed that an
[`Event::NotificationStreamClosed`] has been generated on the stream returned by
[`NetworkService::event_stream`].

If the remote requests to close the notifications substream, all notifications successfully
enqueued using [`NotificationSenderReady::send`] will finish being sent out before the
substream actually gets closed, but attempting to enqueue more notifications will now
return an error. It is however possible for the entire connection to be abruptly closed,
in which case enqueued notifications will be lost.


Obtains a [ ` NotificationSender ` ] for a connected peer if it exists . A ` NotificationSender ` is scoped to a particular connection to the peer that holds a receiver . With a ` NotificationSender ` at hand sending a notification is done in two steps . 1 . [ ` NotificationSender :: ready ` ] is used to wait for the sender to become ready for another notification yielding a [ ` NotificationSenderReady ` ] token . 2 . [ ` NotificationSenderReady :: send ` ] enqueues the notification for sending . This operation can only fail if the underlying notification substream or connection has suddenly closed . An error is returned by [ ` NotificationSenderReady :: send ` ] if there exists no open notifications substream with that combination of peer and protocol or if the remote has asked to close the notifications substream . If that happens it is guaranteed that an [ ` Event :: NotificationStreamClosed ` ] has been generated on the stream returned by [ ` NetworkService :: event_stream ` ] . If the remote requests to close the notifications substream all notifications successfully enqueued using [ ` NotificationSenderReady :: send ` ] will finish being sent out before the substream actually gets closed but attempting to enqueue more notifications will now return an error . It is however possible for the entire connection to be abruptly closed in which case enqueued notifications will be lost .
pub fn notification_sender(
		&self,
		target: PeerId,
		protocol: Cow<'static, str>,
	) -> Result<NotificationSender, NotificationSenderError> {
		// We clone the `NotificationsSink` in order to be able to unlock the network-wide
		// `peers_notifications_sinks` mutex as soon as possible.
		let sink = {
			let peers_notifications_sinks = self.peers_notifications_sinks.lock();
			if let Some(sink) = peers_notifications_sinks.get(&(target, protocol.clone())) {
				sink.clone()
			} else {
				return Err(NotificationSenderError::Closed);
			}
		};
		let notification_size_metric = self.notifications_sizes_metric.as_ref().map(|histogram| {
			histogram.with_label_values(&["out", &protocol])
		});
		Ok(NotificationSender {
			sink,
			protocol_name: protocol,
			notification_size_metric,
		})
	}
pub fn notification_sender ( & self , target : PeerId , protocol : Cow < ' static , str > , ) -> Result < NotificationSender , NotificationSenderError > { // We clone the `NotificationsSink` in order to be able to unlock the network-wide // `peers_notifications_sinks` mutex as soon as possible. let sink = { let peers_notifications_sinks = self . peers_notifications_sinks . lock ( ) ; if let Some ( sink ) = peers_notifications_sinks . get ( & ( target , protocol . clone ( ) ) ) { sink . clone ( ) } else { return Err ( NotificationSenderError :: Closed ) ; } } ; let notification_size_metric = self . notifications_sizes_metric . as_ref ( ) . map ( | histogram | { histogram . with_label_values ( & [ " " , & protocol ] ) } ) ; Ok ( NotificationSender { sink , protocol_name : protocol , notification_size_metric , } ) }
Obtains a [`NotificationSender`] for a connected peer, if it exists.
Obtains a [ ` NotificationSender ` ] for a connected peer if it exists .
&self
target
PeerId
protocol
Cow<'static, str>
&self
target
PeerId
protocol
Cow<'static, str>
usage
This method returns a struct that allows waiting until there is space available in the
buffer of messages towards the given peer. If the peer processes notifications at a slower
rate than we send them, this buffer will quickly fill up.

As such, you should never do something like this.

```ignore
Do NOT do this
for peer in peers {
if let Ok(n) = network.notification_sender(peer, ...) {
if let Ok(s) = n.ready().await {
let _ = s.send(...);
}
}
}
```

Doing so would slow down all peers to the rate of the slowest one. A malicious or
malfunctioning peer could intentionally process notifications at a very slow rate.

Instead, you are encouraged to maintain your own buffer of notifications on top of the one
maintained by `sc-network`, and use `notification_sender` to progressively send out
elements from your buffer. If this additional buffer is full (which will happen at some
point if the peer is too slow to process notifications), appropriate measures can be taken,
such as removing non-critical notifications from the buffer or disconnecting the peer
using [`NetworkService::disconnect_peer`].

Notifications              Per-peer buffer
broadcast    +>   of notifications   +-->  `notification_sender`  +-->  Internet
^       (not covered by
|         sc-network)

Notifications should be dropped
if buffer is full

See also the [`gossip`](crate::gossip) module for a higher-level way to send
notifications.
This method returns a struct that allows waiting until there is space available in the buffer of messages towards the given peer . If the peer processes notifications at a slower rate than we send them this buffer will quickly fill up . As such you should never do something like this . ` ` ` ignore Do NOT do this for peer in peers { if let Ok ( n ) = network . notification_sender ( peer ... ) { if let Ok ( s ) = n . ready () . await { let _ = s . send ( ... ) ; } } } ` ` ` Doing so would slow down all peers to the rate of the slowest one . A malicious or malfunctioning peer could intentionally process notifications at a very slow rate . Instead you are encouraged to maintain your own buffer of notifications on top of the one maintained by ` sc - network ` and use ` notification_sender ` to progressively send out elements from your buffer . If this additional buffer is full ( which will happen at some point if the peer is too slow to process notifications ) appropriate measures can be taken such as removing non - critical notifications from the buffer or disconnecting the peer using [ ` NetworkService :: disconnect_peer ` ] . Notifications Per - peer buffer broadcast + > of notifications + -- > ` notification_sender ` + -- > Internet ^ ( not covered by | sc - network ) Notifications should be dropped if buffer is full See also the [ ` gossip ` ] ( crate :: gossip ) module for a higher - level way to send notifications .