Add cleanupEmptyChildNamespaces server option #4602
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The kind of change this PR does introduce
Current behavior
Currently, child namespaces (those created by a
ParentNamespace
, maybe dynamic namespace is the correct term?) exist forever while the server is running. This can cause issues with certain adapters (like redis) where it continues to subscribe and never unsubscribes, as pointed out in this issue.New behavior
This PR adds a new server option,
cleanupEmptyChildNamespaces
. With this option enabled, when a client socket disconnects, if there are no other sockets connected to the namespace, the following with occur:nsp.adapter.close()
_nsps
)ParentNamespace.children
)Calling
adapter.close()
will allow the adapters to clean up any subscriptions.Removing the namespace from
_nsps
is necessary, otherwise the adapters won't re-subscribe when the namespace is connected to again.Removing the namespace from the
ParentNamespace.children
is to prevent memory leaks, since the old namespace is no longer used.Other information (e.g. related issues)
One thing I'm unsure of is how this will affect the other adapters. I only use the redis adapter, which doesn't implement close yet (I will be creating a new PR to implement it). I don't know if calling close on the other adapters would have some bad side effects.