Class: PulsarSdk::Producer::Router
- Inherits:
-
Object
- Object
- PulsarSdk::Producer::Router
- Includes:
- Tweaks::CleanInspect
- Defined in:
- lib/pulsar_sdk/producer/router.rb
Instance Method Summary collapse
-
#initialize(scheme = :string_hash) ⇒ Router
constructor
A new instance of Router.
- #murmur3_hash ⇒ Object
- #route(key, total, delay = 0) ⇒ Object
-
#string_hash ⇒ Object
将hash值限制在32位内,防止key过长导致过多内存占用.
Methods included from Tweaks::CleanInspect
Constructor Details
#initialize(scheme = :string_hash) ⇒ Router
Returns a new instance of Router.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/pulsar_sdk/producer/router.rb', line 6 def initialize(scheme = :string_hash) case scheme.to_sym when :string_hash @handler = string_hash when :murmur_hash @handler = murmur3_hash else raise "Unknown hash scheme #{scheme}" end end |
Instance Method Details
#murmur3_hash ⇒ Object
38 39 40 41 42 43 |
# File 'lib/pulsar_sdk/producer/router.rb', line 38 def murmur3_hash Proc.new do |key, seed = 31| # Using 0x7fffffff was maintain compatibility with values used in Java client Murmur3.hash(key, seed) & 0x7fffffff end end |
#route(key, total, delay = 0) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/pulsar_sdk/producer/router.rb', line 17 def route(key, total, delay = 0) return 0 if total <= 1 return (@handler.call(key) % total) unless key.to_s.empty? Murmur3.int32_hash(TimeX.now.) % total end |
#string_hash ⇒ Object
将hash值限制在32位内,防止key过长导致过多内存占用
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pulsar_sdk/producer/router.rb', line 26 def string_hash max_mod = 1 << 32 Proc.new do |key| h = 0 key.to_s.each_byte do |x| h = (31*h)%max_mod + x end h end end |