Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do we idiomatically clean up resources after a stream is done being consumed? #110

Open
jedwards1211 opened this issue Jul 26, 2024 · 0 comments

Comments

@jedwards1211
Copy link

jedwards1211 commented Jul 26, 2024

Admittedly I'm a beginner at Rust, but it seems like Streams don't provide any way to signal when the consumer is done, so I'm wondering how I would clean up resources after that?

As a hand-wavy example let's say I subscribe to a redis topic while a client is subscribed:

    pub fn handle_client_subscription(mut self) -> impl Stream<Item = crate::Result<Message>> {
        let subscriber = self.mini_redis_client.subscribe(vec!["numbers".to_string()]).await?;
        return subscriber.into_stream();
        // how to self.mini_redis_client.unsubscribe once the consumer is done with this stream?
    }

Is there an idiomatic way I could make self.mini_redis_client unsubscribe from the topic once the stream is no longer being consumed (e.g. after a websocket handler receives an unsubscribe message from the client and stops consuming this stream)? Do people typically acquire resources via something that implements the Drop trait so that those resources automatically get cleaned up when they go out of scope (I'm assuming ownership of those resources would get moved into the stream, and they would get dropped when the stream goes out of scope, etc?)

My colleague expected a design where subscription methods return an implementation with an explicit method that gets called when the subscription is done. But Streams aren't designed this way so I'm hoping there's some other idiom for cleaning up once the stream is no longer consumed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant