Skip to content

Allow redis connection to be injected #188

@allcentury

Description

@allcentury

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions