minghuaw / toy-rpc

An async RPC in rust-lang that mimics golang's net/rpc
29 stars 2 forks source link

`Ack` for PubSub message delivery #16

Open minghuaw opened 3 years ago

minghuaw commented 3 years ago

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

minghuaw commented 3 years ago

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