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

Use block parameter to pipeline in Redis#multi #68

Merged
merged 4 commits into from Apr 30, 2022

Conversation

capripot
Copy link
Contributor

In preparation of Redis 5, as info message alerts.

Pipelining commands on a Redis instance is deprecated and will be removed in Redis 5.0.0.

redis.multi do
redis.get("key")
end

should be replaced by

redis.multi do |pipeline|
pipeline.get("key")
end

(called from /app/vendor/bundle/ruby/2.7.0/gems/kredis-1.0.1/lib/kredis/types/proxy.rb:13:in `multi'}>

Using block parameter is compatible down to redis gem version 3.0, current dependency to redis is defined as ~> 4.2.

In preparation of Redis 5, as info message alerts.

> Pipelining commands on a Redis instance is deprecated and will be removed in Redis 5.0.0.
>
> redis.multi do
>   redis.get("key")
> end
>
> should be replaced by
>
> redis.multi do |pipeline|
>   pipeline.get("key")
> end
>
> (called from /app/vendor/bundle/ruby/2.7.0/gems/kredis-1.0.1/lib/kredis/types/proxy.rb:13:in `multi'}>
@dhh
Copy link
Member

dhh commented Feb 19, 2022

Looks like tests are failing?

@capripot capripot force-pushed the use-block-var-to-pipeline-in-multi branch from e176a1e to b2ee8e4 Compare February 21, 2022 08:17
@capripot
Copy link
Contributor Author

Ha true! My bad. I just fixed the calls adding the key to them, now bin/test is all green! ✅

multi do
remove elements
multi do |pipeline|
types_to_strings(elements, typed).each { |element| pipeline.lrem key, 0, element }
Copy link
Contributor Author

@capripot capripot Feb 21, 2022

Choose a reason for hiding this comment

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

It repeats List#remove's code, but making remove accept a pipeline optional parameter looked equally unideal.

Copy link
Member

Choose a reason for hiding this comment

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

Man, this is a pretty nasty API regression. Need to take a look and see what else we can come up with.

lib/kredis/types/counter.rb Outdated Show resolved Hide resolved
add members
multi do |pipeline|
pipeline.del key
pipeline.sadd key, types_to_strings(members, typed) if members.flatten.any?
Copy link
Member

Choose a reason for hiding this comment

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

Need to extract types_to_strings(members, typed) if members.flatten.any? into a method. It's repeated three times.

multi do
remove elements
multi do |pipeline|
types_to_strings(elements, typed).each { |element| pipeline.lrem key, 0, element }
Copy link
Member

Choose a reason for hiding this comment

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

Man, this is a pretty nasty API regression. Need to take a look and see what else we can come up with.

@capripot capripot force-pushed the use-block-var-to-pipeline-in-multi branch 2 times, most recently from 87cfd29 to e164798 Compare February 21, 2022 17:44
@capripot capripot force-pushed the use-block-var-to-pipeline-in-multi branch from e164798 to 8a40c02 Compare February 21, 2022 17:46
@capripot
Copy link
Contributor Author

capripot commented Feb 21, 2022

Here is what I came up with:

  • using a new proxy within Kredis#multi as Kredis::Types::Proxy.new(pipeline, key), it has the advantage of having same interface as with redis
  • for "shared" methods, I used an optional keyword argument

We could also have an optional keyword argument on all proxied methods, that would create less objects, but could be a little inelegant as we would need to pass pipeline: pipeline everywhere, beyond "shared" methods.

class Kredis::Types::Proxy
  def method_missing(method, *args, **kwargs)
    Kredis.instrument :proxy, **log_message(method, *args, **kwargs) do
      failsafe do
        (kwargs.delete(:pipeline) || redis).public_send method, key, *args, **kwargs
      end
    end
  end

Let me know if you have any other ideas!

end
alias << add

def remove(*members)
srem types_to_strings(members, typed) if members.flatten.any?
def remove(*members, pipeline: nil)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For consistency

@lewispb
Copy link
Contributor

lewispb commented Mar 1, 2022

Played with this and came up with something a little simpler: #78

@dhh
Copy link
Member

dhh commented Apr 30, 2022

Going to merge this for now. @lewispb we can take a swing at the ivar version if we determine that's a thread safe approach afterwards.

@dhh dhh merged commit 88b8c29 into rails:main Apr 30, 2022
lewispb added a commit to lewispb/kredis that referenced this pull request May 2, 2022
* main:
  Bump version for 1.2.0
  Use pipeline for migration too
  Ensure to pass the pipeline to super
  Use block parameter to pipeline in Redis#multi (rails#68)
  Note tested / supported versions of Redis in the Readme (rails#79)
  Return counter value after incrementing/decrementing
  Enum Bang setter (rails#82)
  Add reset to Cycle after_change callbacks
  Add example in Readme.md
  Support configuring custom keys via method invocation
  Use bundle add instead (rails#81)
lewispb added a commit to lewispb/kredis that referenced this pull request May 2, 2022
lewispb added a commit to lewispb/kredis that referenced this pull request May 26, 2022
* main:
  Bump version for 1.2.0
  Use pipeline for migration too
  Ensure to pass the pipeline to super
  Use block parameter to pipeline in Redis#multi (rails#68)
  Note tested / supported versions of Redis in the Readme (rails#79)
  Return counter value after incrementing/decrementing
  Enum Bang setter (rails#82)
  Add reset to Cycle after_change callbacks
  Add example in Readme.md
  Support configuring custom keys via method invocation
  Use bundle add instead (rails#81)
lewispb added a commit to lewispb/kredis that referenced this pull request Jul 1, 2022
* main:
  Coalesce "current pipeline or redis" into the redis method itself
  Pefer a thread_mattr_accessor over a thread local variable
  Delete list of keys in batch (rails#90)
  Use a thread-local variable for pipeline
  Revert "Use block parameter to pipeline in Redis#multi (rails#68)"
  Resolve pipelining deprecation warnings
lewispb added a commit to lewispb/kredis that referenced this pull request Jul 1, 2022
* main:
  Coalesce "current pipeline or redis" into the redis method itself
  Pefer a thread_mattr_accessor over a thread local variable
  Delete list of keys in batch (rails#90)
  Use a thread-local variable for pipeline
  Revert "Use block parameter to pipeline in Redis#multi (rails#68)"
  Resolve pipelining deprecation warnings
lewispb added a commit to lewispb/kredis that referenced this pull request Jun 19, 2023
* main: (21 commits)
  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
  Allow Redis 5.x
  Add ltrim to lists
  Coalesce "current pipeline or redis" into the redis method itself
  Pefer a thread_mattr_accessor over a thread local variable
  Delete list of keys in batch (rails#90)
  Use a thread-local variable for pipeline
  Revert "Use block parameter to pipeline in Redis#multi (rails#68)"
  ...
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.

None yet

3 participants