toy-rpc icon indicating copy to clipboard operation
toy-rpc copied to clipboard

`Ack` for PubSub message delivery

Open minghuaw opened this issue 4 years ago • 1 comments

Why?

As of 0.8.0-alpha.4, the PubSub message deliveries are not Acked by the subscriber. TCP itself could suffice the delivery or message safety requirement for most cases, but I would like to make it configurable for the cases where explicit Ack is preferred. The proposed implementation is like follows

Proposal

There will be three different AckModes.

  1. AckModeNone, no Ack message will be sent back
  2. AckModeAuto, an Ack will be sent back automatically upon message delivery to the client
  3. AckModeManual, instead of receiving Result<Topic::Item, Error> from the Subscriber stream, the user will receive Result<Delivery<Topic::Item>, Error> from the Subscriber stream. The message content (Topic::Item) can be obtained by calling the .ack() method on the Delivery<_> object.

A ClientBuilder will be added where the AckMode can be configured, and AckModeNone will be the default if the AckMode is not configured. Below shows the proposed example usage

// Configure `AckMode`
let builder = Client::builder() // `AckModeNone` is set by default
    .set_ack_mode_none() // this will explicitly set the `AckMode` to `AckModeNone`
    .set_ack_mode_auto() // set the `AckMode` to `AckModeAuto`
    .set_ack_mode_manual(); // set the `AckMode` to `AckModeManual`

After the builder is configured, use the builder to dial to the server. The usage from this point on for AckModeNone and AckModeAuto will remain the same as before. Thus the code below will only show the proposed usage of AckModeManual

let client = builder.dial(address).await.unwrap();
let subscriber: Subscriber<Topic, AckModeManual> = client.subscriber::<Topic>(cap).unwrap();

while let Some(result)  = subscriber.next().await {
    let delivery = result.unwrap()
    let item = delivery.ack().unwrap(); // item is of type `Topic::Item`
    // .. do something with the item
}

Progress

In no particular order

  • [x] Code cleanup
  • [x] Add type state to Client and Server
  • [x] Verify AckModeNone just works with existing tests and examples
  • [x] Add implementation of setting max retries
  • [x] Add implementation of AckModeAuto
  • [x] Add implementation of AckModeManual
    • [x] Add struct Delivery { .. }

minghuaw avatar Jul 21 '21 06:07 minghuaw

Initial implementation is added in 0.8.0-beta.0 (PR https://github.com/minghuaw/toy-rpc/pull/17). This issue will remain open for issues related to Ack

minghuaw avatar Jul 28 '21 09:07 minghuaw