dahomey-technologies / rustis

An asynchronous Redis client for Rust

Home Page:https://docs.rs/rustis

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unsubscribe method?

Demy076 opened this issue · comments

I see rustis lacks the unsubscribe method. Yes there is close, but it is not remotely close to unsubscribing a pubsub from a specific queue.

So I went ahead and integrated it myself for now. I thought I'd help u out and share the code.

I see that my code is not correctly highlighted, but for some reason I am not doing it correct, that's what I could come up with.

pub_sub_stream.rs

    pub async fn unsubscribe<C, CC>(&mut self, channels: CC) -> Result<()>
    where
        C: SingleArg + Send,
        CC: SingleArgCollection<C>,
    {
        let channels = CommandArgs::default().arg(channels).build();

        self.client
            .unsubscribe_from_pub_sub_sender(&channels, &self.sender)
            .await?;

        let mut existing_channels = CommandArgs::default();
        std::mem::swap(&mut existing_channels, &mut self.channels);
        self.channels = existing_channels.arg(channels).build();

        Ok(())
    }

client.rs

    pub(crate) async fn unsubscribe_from_pub_sub_sender(
        &self,
        channels: &CommandArgs,
        pub_sub_sender: &PubSubSender,
    ) -> Result<()> {
        let (result_sender, result_receiver): (ResultSender, ResultReceiver) = oneshot::channel();

        let pub_sub_senders = channels
            .into_iter()
            .map(|c| (c.to_vec(), pub_sub_sender.clone()))
            .collect::<Vec<_>>();

        let message = Message::pub_sub(
            cmd("UNSUBSCRIBE").arg(channels.clone()),
            result_sender,
            pub_sub_senders,
        );

        self.send_message(message)?;

        result_receiver.await??.to::<()>()
    }

unblock also need impl for blocking client when stop

unblock also need impl for blocking client when stop

Could you elaborate on this?

@Demy076 to create a code block in markdown, you need to use the tag ``` at the begining and at the end of your code block