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

Avoid possible redundant database initialization from multiple threads #56

Merged
merged 2 commits into from
Aug 31, 2023

Conversation

ikaronen-relex
Copy link
Contributor

Based on discussion at #50 (comment)

Comment on lines +54 to +57
@db = nil

def self.db
@db || LOCK.synchronize { @db ||= new }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@db = nil
def self.db
@db || LOCK.synchronize { @db ||= new }
def self.db
@db ||= LOCK.synchronize { @db ||= new }

If you want to avoid the warning on older rubies. But both works I guess.

Copy link
Contributor Author

@ikaronen-relex ikaronen-relex Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean a different warning than the "instance variable not initialized" one? The @db = nil line above ought to avoid that one, or at least it did when I tested this on ruby 2.6.10p210.

(The double ||= would indeed work too, but I'd rather avoid that since it makes the already somewhat complicated idiom even more confusing for the unfamiliar reader.)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I meant alternative solution to that same warning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am ok with the implementation, its a bit of hair splitting going one way vs the other.

@SamSaffron SamSaffron merged commit 574c3be into discourse:main Aug 31, 2023
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants