datafuselabs / openraft

rust raft with improvements
Apache License 2.0
1.36k stars 150 forks source link

Uniform `AsyncRuntime` channel interfaces #1198

Closed SteveLauC closed 1 month ago

SteveLauC commented 1 month ago

Trait AsyncRuntime exposes various associated channel types:

Currently, both MPSC and Watch are exposed through traits:

https://github.com/datafuselabs/openraft/blob/ec284cb4813019d8b3a43a8cd74f3e1aa39eb882/openraft/src/type_config/async_runtime/mod.rs#L120-L122

Oneshot is different, it is scattered in the trait:

pub trait AsyncRuntime: Debug + Default + PartialEq + Eq + OptionalSend + OptionalSync + 'static {
    // Other fields are omitted

    type OneshotSender<T: OptionalSend>: OneshotSender<T> + OptionalSend + OptionalSync + Sized;
    type OneshotReceiverError: std::error::Error + OptionalSend;
    type OneshotReceiver<T: OptionalSend>: OptionalSend
        + OptionalSync
        + Future<Output = Result<T, Self::OneshotReceiverError>>
        + Unpin;
    fn oneshot<T>() -> (Self::OneshotSender<T>, Self::OneshotReceiver<T>)
    where T: OptionalSend;
}

To provide a consistent interface, I kinda think we should expose Oneshot in the way used by MPSC and Watch, i.e., something like:

pub trait Oneshot {
    type Sender<T: OptionalSend>: OneshotSender<T>;
    type Receiver<T: OptionalSend>: OptionalSend
        + OptionalSync
        + Future<Output = Result<T, Self::ReceiverError>>
        + Unpin;
    type ReceiverError: std::error::Error + OptionalSend;

    fn channel<T>() -> (Self::Sender<T>, Self::Receiver<T>)
    where T: OptionalSend;
}

pub trait AsyncRuntime: Debug + Default + PartialEq + Eq + OptionalSend + OptionalSync + 'static {
    // Other fields are omitted

    type MpscUnbounded: MpscUnbounded; 
    type Watch: Watch; 
    type Oneshot: Oneshot;
}
github-actions[bot] commented 1 month ago

👋 Thanks for opening this issue!

Get help or engage by:

SteveLauC commented 1 month ago

/assignme