class Gruf::Balancer::Client
Thread-safe class that allows percentage-based balancing of calls to a given pool of clients
Public Class Methods
@param [GRPC::GenericService] service The gRPC Service stub to use when creating clients in this pool @param [Hash] options A default set of options to pass through to all Gruf::Clients in this balanced pool @param [Hash] client_options A default set of gRPC client options to pass through to all Gruf::Clients in this
balanced pool
# File lib/gruf/balancer/client.rb, line 30 def initialize(service:, options: {}, client_options: {}) super() @service = service @options = options @client_options = client_options @pool = ::Concurrent::Hash.new @sampler = ->(pool) { pool.max_by { |_, w| rand**(1.0 / w) }.first } end
Public Instance Methods
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.
@param [Float] percentage The percentage of requests to weight by (0-100) @param [Hash] options Options to pass-through to the Gruf::Client @param [Hash] client_options gRPC Client
Options to pass-through to the Gruf::Client @param [Class] client_class The client class to use. Useful if wanting to create a Gruf::SynchronizedClient or
other derivative client
# 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(options), client_options: @client_options.merge(client_options) ) @pool[cl] = percentage.to_f / 100.0 end
Delegate the call to the sampled client
@return [Gruf::Response]
# File lib/gruf/balancer/client.rb, line 75 def call(*args, &block) pick.call(*args, &block) end
Pick a sampled client from the pool based on the weighted percentages entered
@return [::Gruf::Client]
# File lib/gruf/balancer/client.rb, line 66 def pick @sampler[@pool] end