nats-io / nats.rs

Rust client for NATS, the cloud native messaging system.
Apache License 2.0
1.07k stars 169 forks source link

[DRAFT] Add backpressure to publish #1286

Open Jarema opened 3 months ago

Jarema commented 3 months ago

Overview

Clients that are missing backpressure for async publishing of JetStream messages can negatively impac the NATS servers and clusters.

Rust client separates publish with ack, as two separate futures, which is fine and very flexible, but does not enforce any backpressure, or enable easier handling of it. That is especially relevant for users dropping PublishAckFuture and ignoring the Ack entirely.

This PR addresses this in two ways:

  1. Error on Context::publish if the threshold for in-flight acks is reached
  2. Spin a task that awaits for acks for PubAckFuture that were dropped.

Challenges

  1. A new background task was needed. This should be fine, as it idles if user does not drop acks.
  2. Increased memory footprint of Context.