-
Notifications
You must be signed in to change notification settings - Fork 8
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
Create RactorLocalSingleton #4
Conversation
I'm not sure we should be encouraging the Singleton design pattern. We should probably encourage thread-local singletons which are more robust and safe. |
lib/singleton.rb
Outdated
@@ -93,21 +93,25 @@ | |||
# | |||
module Singleton | |||
VERSION = "0.1.1" | |||
VERSION.freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intentional? I think VERSION = "0.1.1".freeze
is the correct change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why aren't we just using frozen_string_literal: true
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you both! I meant to clean that up. I've changed it, and it should be fixed now!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Does this imply that singletons are no longer singleton? Does this mean all users of |
I agree that RactorLocalSingletons would not be true Singletons—they are Singleton only in the context of their own Ractor. If users want to be able to use an existing Singleton-based class within a Ractor, they would just change “include Singleton” to “include RactorLocalSingleton”. I don’t think any other change would necessary in most cases. I think that the main use case would be for classes that include Singleton only for the sake of syntax and organization (rather than the condition of being completely singular). For example, the Prime library currently defines the Prime class using Singleton. I believe that the reason is solely to provide a specific structure for calling Prime-related methods. In order to make Prime completely Ractor-compatible, it cannot be a true Singleton. Rewriting Prime so that it doesn’t use Singleton at all would probably change too many properties of the library. By just using RactorLocalSingleton instead, it can become Ractor-compatible while retaining its essential properties. Again, I agree that this is not a true Singleton. But I think that having this option may encourage more libraries to become Ractor-compatible. |
Why wouldn't you just change |
I don’t think Singleton itself can be made Ractor-compatible—if it were, then there would be multiple instances of it (one per Ractor), so it wouldn’t be a true Singleton anymore. This could potentially be problematic for some classes relying on this attribute. Moving a class from Singleton to RactorLocalSingleton should allow the class to become usable in Ractors. But if the class doesn’t need to be used in Ractors, I don’t think it needs to be changed. |
How can "class doesn't need to be used in Ractors" be known ahead of time? Surely with the introduction of Ractor, ALL usage of Singleton is now broken and need to be updated/changed? |
This proposal is to introduce "Singleton object per Ractor". |
On 2.4 it seems failed. @rm155 could you check it? |
I believe this PR needs more discussion. Is there a bugs.ruby-lang.org issue?
@ko1 Where is the proposal? |
https://bugs.ruby-lang.org/issues/18127 |
Classes that include Singleton cannot be used within Ractors, since the instance can only exist within the main Ractor. By creating RactorLocalSingleton, classes have the option of instead having one instance per Ractor.