From 33ba02232c3e9280bd8936fe4dbc4fdd44e48a18 Mon Sep 17 00:00:00 2001 From: Philipp Date: Tue, 7 May 2024 16:13:43 +0200 Subject: [PATCH] Add host_to_name_map option to Dalli::Protocol::ConnectionManager --- lib/dalli/client.rb | 2 ++ lib/dalli/protocol/connection_manager.rb | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/dalli/client.rb b/lib/dalli/client.rb index a590e311..eeda2332 100644 --- a/lib/dalli/client.rb +++ b/lib/dalli/client.rb @@ -45,6 +45,8 @@ class Client # useful for injecting a FIPS compliant hash object. # - :protocol - one of either :binary or :meta, defaulting to :binary. This sets the protocol that Dalli uses # to communicate with memcached. + # - :host_to_name_map - a hash mapping "host:port" to a name. Useful for providing more descriptive names or + # tuning the ring-continuum e.g. { 'localhost:11211' => 'local' } # def initialize(servers = nil, options = {}) @normalized_servers = ::Dalli::ServersArgNormalizer.normalize_servers(servers) diff --git a/lib/dalli/protocol/connection_manager.rb b/lib/dalli/protocol/connection_manager.rb index fe8bd911..87ecbc7f 100644 --- a/lib/dalli/protocol/connection_manager.rb +++ b/lib/dalli/protocol/connection_manager.rb @@ -23,7 +23,9 @@ class ConnectionManager # amount of time to sleep between retries when a failure occurs socket_failure_delay: 0.1, # Set keepalive - keepalive: true + keepalive: true, + # map a "host:port" to a specific name + host_to_name_map: {} }.freeze attr_accessor :hostname, :port, :socket_type, :options @@ -45,7 +47,13 @@ def name if socket_type == :unix hostname else - "#{hostname}:#{port}" + name = "#{hostname}:#{port}" + + if options[:host_to_name_map][name].nil? + name + else + options[:host_to_name_map][name] + end end end