Skip to content

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

Open
mikebaldry wants to merge 5 commits intoejfinneran:masterfrom
Buyapowa:master
Open

Making it possible to use a connection pool for getting a Redis instance#21
mikebaldry wants to merge 5 commits intoejfinneran:masterfrom
Buyapowa:master

Conversation

@mikebaldry
Copy link
Copy Markdown

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.

…dis from a connection pool. Using MULTI instead of pipelining the requests, so they are atomic
@coveralls
Copy link
Copy Markdown

coveralls commented Sep 6, 2016

Coverage Status

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

1 similar comment
@coveralls
Copy link
Copy Markdown

Coverage Status

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

@ejfinneran
Copy link
Copy Markdown
Owner

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

@mikebaldry
Copy link
Copy Markdown
Author

mikebaldry commented Sep 26, 2016

@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) })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants