-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Add cleanupEmptyChildNamespaces server option #4602
Add cleanupEmptyChildNamespaces server option #4602
Conversation
- When a socket disconnects from a child namespace (those created by a ParentNamespace), if this option is enabled and there are no other sockets connected, it will call nsp.adapter.close() and remove the namespace. - The namespace can be connected to later (it will be recreated) - Added some tests to cover the new code changes
This commit adds a new option, "cleanupEmptyChildNamespaces". With this option enabled (disabled by default), when a socket disconnects from a dynamic namespace and if there are no other sockets connected to it then the namespace will be cleaned up and its adapter will be closed. Note: the namespace can be connected to later (it will be recreated) Related: socketio/socket.io-redis-adapter#480
Merged as 5d9220b. Thanks a lot 👍 |
This commit adds a new option, "cleanupEmptyChildNamespaces". With this option enabled (disabled by default), when a socket disconnects from a dynamic namespace and if there are no other sockets connected to it then the namespace will be cleaned up and its adapter will be closed. Note: the namespace can be connected to later (it will be recreated) Related: socketio/socket.io-redis-adapter#480
This commit adds a new option, "cleanupEmptyChildNamespaces". With this option enabled (disabled by default), when a socket disconnects from a dynamic namespace and if there are no other sockets connected to it then the namespace will be cleaned up and its adapter will be closed. Note: the namespace can be connected to later (it will be recreated) Related: socketio/socket.io-redis-adapter#480
Hello @stevebaum23 and @darrachequesne We have stumbled upon a similar issue, when using Redis adapter, tho not sure how should we go about fixing it. It appears that adapter cleanup isn't called when a socket.io middleware throws exception: I created a separate bug for this: #4772 I attempted a PR here but that does not seem to be the correct approach - we should only clean up when there are no more sockets Would you be able to help? |
While serving many sites it's best to close down namespace when last connection is closed. refer socketio/socket.io#4602
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.