-
Notifications
You must be signed in to change notification settings - Fork 78
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
Default values on initialization #119
Conversation
…tialize * origin/main: (22 commits) Add kredis_ordered_set for OrderedSet usage in models Add a development console Bump version for 1.5.0 Fix ordered set prepend bug (rails#115) Unique list with sorted set (rails#114) Eliminating Ruby Warnings (rails#112) CI against Redis 7, Ruby 3.1, and Ruby 3.2 (rails#113) Bump version for 1.4.0 Update nokogiri for compatibility Revert "Improved version of UniqueList: OrderedSet (rails#76)" (rails#111) Add `last` to lists (rails#97) Improved version of UniqueList: OrderedSet (rails#76) Return Time objects instead of deprecated DateTime (rails#106) Fix possible deserialization of untrusted data Typecast return of Set#take (rails#105) Declare Active Model dependency (rails#107) Address LogSubscriber deprecation (rails#98) Account for time zones in DateTime serializations (rails#102) Add sample to set (rails#100) Bump version for 1.3.0 ...
efa429b
to
6faf4d0
Compare
* main: Run rubocop autocorrect Set up RuboCop with CI Make boolean storage more efficient Support line filtering in tests Update Readme on local development and debugging Set up ruby/debug in place of byebug for dev and test
I like this a lot. Nice simplification 👍 |
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.
Enum is a bit of an oddball re. defaults out of the enumerated set and nil
defaults in particular. From HEY:
kredis_enum :status, values: %w[ waiting confirmed ], default: nil, key: …
Defaults out of the enumerated set should probably be considered a bug, with a special case for default: nil
behaving how an optional: true
argument would.
Thanks @jeremy, agree, and updated to raise an error in that case. |
write the specified default value. | ||
|
||
This means that using default values in a typical Rails app additional Redis calls (WATCH, EXISTS, UNWATCH) will be | ||
executed for each Kredis attribute with a default value read or written during a request. |
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 occur every request, or only when a value does not exist?
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.
In the context of a Rails web request, as the Kredis object is initialized, the key is checked with WATCH, EXISTS, UNWATCH once, but then subsequent operations within the request do not incur any Redis command overhead. If no default value is specified, no additional Redis command overhead is incurred at all.
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.
To clarify:
Scenario 1: Default NOT defined
(no additional overhead)
Scenario 2: Default defined, value NOT exists
WATCH, EXISTS, UNWATCH
Scenario 3: Default defined, value exists
(no additional overhead?)
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.
Actually, it's:
Scenario 1: Default NOT defined
(no additional overhead)
Scenario 2: Default defined, value NOT exists
WATCH, EXISTS, write default (depending on type), UNWATCH
Scenario 3: Default defined, value exists
WATCH, EXISTS, UNWATCH
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.
It seems a more efficient approach would be to only add extra overhead of default is defined AND value doesn't exist. In other words:
- Get Value
- Value doesn't exist
- Execute Default Logic
This avoids extra overhead if the value has been set already.
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.
I agree @tleish. That's exactly what we do here, but with the added WATCH / UNWATCH.
To illustrate the problem that the WATCH / UNWATCH solves check this test case:
kredis/test/types/counter_test.rb
Lines 113 to 121 in b1a818a
test "concurrent initialization with default" do | |
5.times.map do | |
Thread.new do | |
Kredis.counter("mycounter", default: 5).increment | |
end | |
end.each(&:join) | |
assert_equal 10, Kredis.counter("mycounter").value | |
end |
Without the WATCH / UNWATCH, the value existing check is prone to a race condition.
I noticed that the default value cannot be set in kredis_integer in the current release version 1.5.0.
When will kredis be released with this PR? |
1.6.0 released 👍 |
I built upon what was started in #89 but attempted a simpler implementation, only setting default values upon type initialization.
Closes #66 and #89
Almost all the same tests pass, with one or two minor differences.
Todo