ratelimit icon indicating copy to clipboard operation
ratelimit copied to clipboard

Making it possible to use a connection pool for getting a Redis instance

Open mikebaldry opened this issue 8 years ago • 4 comments

In some (most production, I'd have thought) projects you have a pool of Redis connections from which you can checkout a connection, perform your actions then put it back in to the pool ready for the next thing to use.

This is not possible with your current implementation so I've added an option checkout_redis_with, you can pass in a lambda which yields with the Redis connection.

looks like

Ratelimit.new("abc", checkout_redis_with: -> (&block) { MyRedisPool.checkout(&block) })

or similar.

The original implementation still works the same, but checks out a connection when it needs to use one.

I've also updated it to use MULTI instead of pipelining as expressed in a previous unanswered PR.

mikebaldry avatar Sep 06 '16 15:09 mikebaldry

Coverage Status

Coverage increased (+0.1%) to 99.259% when pulling 7d1156f8e70d4a7c46aae12d63539310308b83ab on Buyapowa:master into aa8bd7a71e263a86451a75216589d408fa133342 on ejfinneran:master.

coveralls avatar Sep 06 '16 16:09 coveralls

Coverage Status

Coverage increased (+0.1%) to 99.259% when pulling 7d1156f8e70d4a7c46aae12d63539310308b83ab on Buyapowa:master into aa8bd7a71e263a86451a75216589d408fa133342 on ejfinneran:master.

coveralls avatar Sep 06 '16 16:09 coveralls

Are there other Redis libraries that use this pattern? I'm wondering if supporting https://github.com/mperham/connection_pool might be cleaner/easier.

ejfinneran avatar Sep 22 '16 05:09 ejfinneran

@ejfinneran I did it in a specific way that allows that, you could do this:

redis_pool = ConnectionPool.new(size: 5, timeout: 5) { Redis::Client.new }

Ratelimit.new("blah", checkout_redis_with: &redis_pool.method(:with))

or as in the PR comment:

Ratelimit.new("blah", checkout_redis_with: -> (&block) { redis_pool.with(&block) })

mikebaldry avatar Sep 26 '16 08:09 mikebaldry