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

Allow redis connection to be injected #188

Open
allcentury opened this issue Oct 23, 2019 · 1 comment
Open

Allow redis connection to be injected #188

allcentury opened this issue Oct 23, 2019 · 1 comment

Comments

@allcentury
Copy link

It be easier to scale up our redis usage if we were able to pass around a redis connection to BayesRedisBackend rather than let that class hold it's own connection. In fact, I think everyone benefits from this because this would also allow folks to use other redis gems as long as it conformed to the interface.

The idea here might look like this:

class MyJob < ApplicationJob
  def perform
    ClassifierReborn::BayesRedisBackend.new(
      host: ENV["REDIS_HOST"],
      port: ENV["REDIS_PORT"].to_i,
      db: ENV["REDIS_DB_EVIDENCE_CLASSIFIER_DB"].to_i,
    )
    @trainer = ClassifierReborn::Bayes.new(backend: backend)
  end
end

# => now N concurrency = N connections

Instead:

class RedisPool
  def self.connections
    @c ||= ConnectionPool.new(size: 5, timeout: 5) do
      Redis.new(
        host: ENV["REDIS_HOST"],
        port: ENV["REDIS_PORT"].to_i,
        db: ENV["REDIS_DB_EVIDENCE_CLASSIFIER_DB"].to_i,
      )
    end
  end
end

class MyJob < ApplicationJob
  def perform
    RedisPool.connections.with_conn do |conn|
      backend = ClassifierReborn::BayesRedisBackend.new(client: conn)
      @trainer = ClassifierReborn::Bayes.new(backend: backend)
    end
  end
end

Would you be open to a PR for this?

@Ch4s3
Copy link
Member

Ch4s3 commented Oct 28, 2019

Submit a PR and I'll take a look as soon as I can

allcentury pushed a commit to allcentury/classifier-reborn that referenced this issue Nov 1, 2019
See issue jekyll#188 for details.

Prior to this change, pooled redis connections were hard to share across.

Now you can inject your own redis connection into the intiailizer via
the `redis_conn` parameter.
allcentury pushed a commit to allcentury/classifier-reborn that referenced this issue Nov 4, 2019
See issue jekyll#188 for details.

Prior to this change, pooled redis connections were hard to share across.

Now you can inject your own redis connection into the intiailizer via
the `redis_conn` parameter.
allcentury pushed a commit to allcentury/classifier-reborn that referenced this issue Nov 4, 2019
See issue jekyll#188 for details.

Prior to this change, pooled redis connections were hard to share across.

Now you can inject your own redis connection into the intiailizer via
the `redis_conn` parameter.
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

No branches or pull requests

2 participants