taiki45/mixed_gauge

View on GitHub
lib/mixed_gauge/routing.rb

Summary

Maintainability
A
0 mins
Test Coverage
require 'digest/md5'

module MixedGauge
  # Routing from key to database connection
  class Routing
    # @param [ClusterConfig] cluster_config
    def initialize(cluster_config)
      @cluster_config = cluster_config
    end

    # slot can be one of (0..slot_size - 1)
    # @param [String] dist_key
    # @return [String] connection name
    def route(key)
      slot = hash_f(key) % @cluster_config.slot_size
      @cluster_config.fetch(slot)
    end

    # @param [String] key
    # @return [Integer]
    def hash_f(key)
      MixedGauge.config.hash_proc.call(key)
    end
  end
end