Class: Gruf::Balancer::Client
- Inherits:
-
Object
- Object
- Gruf::Balancer::Client
- Defined in:
- lib/gruf/balancer/client.rb
Overview
Thread-safe class that allows percentage-based balancing of calls to a given pool of clients
Instance Method Summary collapse
-
#add_client(percentage:, options: {}, client_options: {}, client_class: nil) ⇒ Object
Create a client and add it to the pool at a given percentage of requests.
-
#call(*args, &block) ⇒ Gruf::Response
Delegate the call to the sampled client.
-
#initialize(service:, options: {}, client_options: {}) ⇒ Client
constructor
A new instance of Client.
-
#pick ⇒ ::Gruf::Client
Pick a sampled client from the pool based on the weighted percentages entered.
Constructor Details
#initialize(service:, options: {}, client_options: {}) ⇒ Client
Returns a new instance of Client.
30 31 32 33 34 35 36 37 |
# File 'lib/gruf/balancer/client.rb', line 30 def initialize(service:, options: {}, client_options: {}) super() @service = service @options = @client_options = @pool = ::Concurrent::Hash.new @sampler = ->(pool) { pool.max_by { |_, w| rand**(1.0 / w) }.first } end |
Instance Method Details
#add_client(percentage:, options: {}, client_options: {}, client_class: nil) ⇒ Object
Create a client and add it to the pool at a given percentage of requests. If the percentage given is outside of 0-100, it will automatically constrain it to the max of 100.0 or minimum of 0.0.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/gruf/balancer/client.rb', line 49 def add_client(percentage:, options: {}, client_options: {}, client_class: nil) client_class ||= ::Gruf::Client percentage = percentage > 100.0 ? 100.0 : percentage.to_f percentage = percentage < 0.0 ? 0.0 : percentage cl = client_class.new( service: @service, options: @options.merge(), client_options: @client_options.merge() ) @pool[cl] = percentage.to_f / 100.0 end |
#call(*args, &block) ⇒ Gruf::Response
Delegate the call to the sampled client
75 76 77 |
# File 'lib/gruf/balancer/client.rb', line 75 def call(*args, &block) pick.call(*args, &block) end |
#pick ⇒ ::Gruf::Client
Pick a sampled client from the pool based on the weighted percentages entered
66 67 68 |
# File 'lib/gruf/balancer/client.rb', line 66 def pick @sampler[@pool] end |