How to clean up resources when subscriptions end?
See original GitHub issueI’m submitting a…
[ ] Regression
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
Suppose there is a Subscription defined, which sets up an interval that polls a remote service every X seconds. I’m not sure how this interval could be cleared when the subscription is done, either via forced unsubscription from a client, or a disconnect of the underlying websocket.
I have looked through upstream graphql subscriptions for an answer but it seems unclear whether this is possible at all.
Expected behavior
Have a way to do cleanup when a subscription ends.
Minimal reproduction of the problem with instructions
@Subscription(returns => Something)
someSubscription() {
setInterval(async () => {
pubSub.publish('someSubscription', {
someSubscription: (await fetchDataFromRemoteService())
});
}, 5000);
return pubSub.asyncIterator('someSubscription');
}
If the client disappears, this interval would keep firing forever. With multiple clients, this would quickly end up in a huge memory leak and potential DoS on the remote service.
How would one clean up the interval? (or could be RxJS observable, or anything else)
What is the motivation / use case for changing the behavior?
Environment
Nest version: 6.0.1
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Clean up your subscription - step by step tutorial from DevOps ...
Cleanup, Removing unnecessary resources from Microsoft Azure cloud. Don't pay if you don't use, step by step tutorial by DevOps Architect.
Read more >Terminate active AWS resources that you no longer need
Open the AWS Management Console. Open the console for the service that contains the resources that you want to terminate (for example, Amazon ......
Read more >Clean Up instructions at the end of the module to stop ...
In the Azure portal, select Resource groups on the far left. · From the list, select the resource group that you created. ·...
Read more >Tips for keeping your Azure Subscriptions Clean
Automatically remove unused resources from your Azure subscription · 1. Tag resources to be cleaned up · 2. Create a script that cleans...
Read more >How to clean up subscriptions in react components using ...
One of the most common bug in react component where developers forget to clean up resources properly after unmount. So how do i...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Leaving this here as it may help someone:
Your observable’s teardown logic will be called when subscriptions end.
Yup, it’s custom, but I was expecting something built-in.
Everything seems to be abstracted in various ways with decorators already by Nest. So perhaps it could allow returning a RxJS Observable from the Subscription handler, and if an Observable is returned, wrap it in an AsyncIterable itself.
Basically turning this:
into this:
I feel it would enforce better practices and help with discoverability, because one would know that standard RxJS Observable teardown logic would be available.