redis-client 0.26.4 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 154a0c259fc2b3ccd21836d769541db74daef95be9d2e235a36c15b8b6d68d3b
4
- data.tar.gz: 5f27693c2903da092547ad8f4362a1573e8527d9ef38266074c389574523971b
3
+ metadata.gz: 286c927e4067dd45e1fcee65daab6e11af6d3b4d79604e1fc0b2a7a930f75201
4
+ data.tar.gz: 899fab7bb4ce71f7bbf68ce00f9a008816690ff0ea8302b92edc2333a478d9e3
5
5
  SHA512:
6
- metadata.gz: ca23aa033f2142cd608cf6197a35b0e32c89f1240344853be148b9eefbec330bbf1a8507277c822482455a8e7198abec888ce73c1f842382d5ebc826c417c5d2
7
- data.tar.gz: e5f2086185d04e12462dc176a3c90084d7a7b04c520f906dcdb6024a1a42a6d7b418d5c94951628062a746ed0a0426add0970d877587f6b3d25843baf2c0a780
6
+ metadata.gz: 4c900ff2f7bea9662273bcb0666c78a44adb6c7b34fa1d7e10d5d56cac6e2f868de8811d769f42ea52a8b70304cacf650dea7aadcc31b3ee88a4eb2dfc6f28bc
7
+ data.tar.gz: fa6093d8d2101a120164f6d86857343d84cc69c077290ce6b41b80626918165045190ae3a004e8defc1f61c5fc72c623d0acf121a3b2efe32a4d14fb71787f74
data/CHANGELOG.md CHANGED
@@ -1,8 +1,13 @@
1
1
  # Unreleased
2
2
 
3
+ # 0.27.0
4
+
5
+ - Added `idle_timeout` to revalidate connections that haven't been successfuly used in a long time. Defaults to 60 seconds.
6
+ - Added `driver_info` configuration, to issue `CLIENT SETINFO` during connection prelude.
7
+
3
8
  # 0.26.4
4
9
 
5
- - Further improve `rediss://` URLs used with Redis sentinel. Now avoid override explictly set `ssl:` paramter.
10
+ - Further improve `rediss://` URLs used with Redis sentinel. Now avoid override explictly set `ssl:` parameter.
6
11
  - Fix compatibility with `redis-rb` in sentinel mode.
7
12
 
8
13
  # 0.26.3
data/README.md CHANGED
@@ -83,6 +83,7 @@ redis.call("GET", "mykey")
83
83
  - `connect_timeout`: The connection timeout, takes precedence over the general timeout when connecting to the server.
84
84
  - `read_timeout`: The read timeout, takes precedence over the general timeout when reading responses from the server.
85
85
  - `write_timeout`: The write timeout, takes precedence over the general timeout when sending commands to the server.
86
+ - `idle_timeout`: Amount of time after which an idle connection has to be revalidated with a PING command. Defaults to `60` seconds.
86
87
  - `reconnect_attempts`: Specify how many times the client should retry to send queries. Defaults to `0`. Makes sure to read the [reconnection section](#reconnection) before enabling it.
87
88
  - `circuit_breaker`: A Hash with circuit breaker configuration. Defaults to `nil`. See the [circuit breaker section](#circuit-breaker) for details.
88
89
  - `protocol:` The version of the RESP protocol to use. Default to `3`.
@@ -10,11 +10,12 @@ class RedisClient
10
10
  DEFAULT_PORT = 6379
11
11
  DEFAULT_USERNAME = "default"
12
12
  DEFAULT_DB = 0
13
+ DEFAULT_IDLE_TIMEOUT = 30.0
13
14
 
14
15
  module Common
15
16
  attr_reader :db, :id, :ssl, :ssl_params, :command_builder, :inherit_socket,
16
17
  :connect_timeout, :read_timeout, :write_timeout, :driver, :protocol,
17
- :middlewares_stack, :custom, :circuit_breaker
18
+ :middlewares_stack, :custom, :circuit_breaker, :driver_info, :idle_timeout
18
19
 
19
20
  alias_method :ssl?, :ssl
20
21
 
@@ -27,6 +28,7 @@ class RedisClient
27
28
  read_timeout: timeout,
28
29
  write_timeout: timeout,
29
30
  connect_timeout: timeout,
31
+ idle_timeout: DEFAULT_IDLE_TIMEOUT,
30
32
  ssl: nil,
31
33
  custom: {},
32
34
  ssl_params: nil,
@@ -37,7 +39,8 @@ class RedisClient
37
39
  inherit_socket: false,
38
40
  reconnect_attempts: false,
39
41
  middlewares: false,
40
- circuit_breaker: nil
42
+ circuit_breaker: nil,
43
+ driver_info: nil
41
44
  )
42
45
  @username = username
43
46
  @password = password && !password.respond_to?(:call) ? ->(_) { password } : password
@@ -54,6 +57,7 @@ class RedisClient
54
57
  @connect_timeout = connect_timeout
55
58
  @read_timeout = read_timeout
56
59
  @write_timeout = write_timeout
60
+ @idle_timeout = idle_timeout
57
61
 
58
62
  @driver = driver ? RedisClient.driver(driver) : RedisClient.default_driver
59
63
 
@@ -84,6 +88,8 @@ class RedisClient
84
88
  end
85
89
  end
86
90
  @middlewares_stack = middlewares_stack
91
+
92
+ @driver_info = driver_info
87
93
  end
88
94
 
89
95
  def connection_prelude
@@ -107,6 +113,13 @@ class RedisClient
107
113
  prelude << ["SELECT", @db.to_s]
108
114
  end
109
115
 
116
+ # Add CLIENT SETINFO commands for lib-name and lib-ver
117
+ # These commands are supported in Redis 7.2+
118
+ if @driver_info
119
+ prelude << ["CLIENT", "SETINFO", "LIB-NAME", build_lib_name]
120
+ prelude << ["CLIENT", "SETINFO", "LIB-VER", RedisClient::VERSION]
121
+ end
122
+
110
123
  # Deep freeze all the strings and commands
111
124
  prelude.map! do |commands|
112
125
  commands = commands.map { |str| str.frozen? ? str : str.dup.freeze }
@@ -123,6 +136,25 @@ class RedisClient
123
136
  @username || DEFAULT_USERNAME
124
137
  end
125
138
 
139
+ # Build the library name for CLIENT SETINFO LIB-NAME.
140
+ #
141
+ # @param driver_info [String, Array<String>, nil] Upstream driver info
142
+ # @return [String] Library name with optional upstream driver info
143
+ # @raise [ArgumentError] if driver_info is not a String or Array
144
+ def build_lib_name
145
+ return "redis-client" if @driver_info.nil?
146
+
147
+ info = case @driver_info
148
+ when String then @driver_info
149
+ when Array then @driver_info.join(";")
150
+ else raise ArgumentError, "driver_info must be a String or Array of Strings"
151
+ end
152
+
153
+ return "redis-client" if info.empty?
154
+
155
+ "redis-client(#{info})"
156
+ end
157
+
126
158
  def resolved?
127
159
  true
128
160
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class RedisClient
4
- VERSION = "0.26.4"
4
+ VERSION = "0.27.0"
5
5
  end
data/lib/redis_client.rb CHANGED
@@ -269,6 +269,10 @@ class RedisClient
269
269
  config.read_timeout
270
270
  end
271
271
 
272
+ def idle_timeout
273
+ config.idle_timeout
274
+ end
275
+
272
276
  def db
273
277
  config.db
274
278
  end
@@ -758,7 +762,9 @@ class RedisClient
758
762
  @retry_attempt = config.retriable?(tries) ? tries : nil
759
763
  connection = raw_connection
760
764
  if block_given?
761
- yield connection
765
+ result = yield connection
766
+ @last_used_at = RedisClient.now
767
+ result
762
768
  else
763
769
  connection
764
770
  end
@@ -785,7 +791,9 @@ class RedisClient
785
791
  begin
786
792
  @disable_reconnection = true
787
793
  @raw_connection.retry_attempt = nil
788
- yield connection
794
+ result = yield connection
795
+ @last_used_at = RedisClient.now
796
+ result
789
797
  rescue ConnectionError, ProtocolError
790
798
  close
791
799
  raise
@@ -799,6 +807,16 @@ class RedisClient
799
807
  if @raw_connection.nil? || !@raw_connection.revalidate
800
808
  connect
801
809
  end
810
+
811
+ if config.idle_timeout && (@last_used_at + config.idle_timeout) < RedisClient.now
812
+ @middlewares.call(["PING"], config) do
813
+ @raw_connection.call(["PING"], nil)
814
+ rescue ConnectionError, ProtocolError
815
+ close
816
+ connect
817
+ end
818
+ end
819
+
802
820
  @raw_connection.retry_attempt = @retry_attempt
803
821
  @raw_connection
804
822
  end
@@ -820,6 +838,7 @@ class RedisClient
820
838
  )
821
839
  end
822
840
  end
841
+ @last_used_at = RedisClient.now
823
842
  @raw_connection.retry_attempt = @retry_attempt
824
843
 
825
844
  prelude = config.connection_prelude.dup
@@ -856,6 +875,9 @@ class RedisClient
856
875
  if error.message.match?(/ERR unknown command ['`]HELLO['`]/)
857
876
  raise UnsupportedServer,
858
877
  "redis-client requires Redis 6+ with HELLO command available (#{config.server_url})"
878
+ # Ignore CLIENT SETINFO errors (Redis < 7.2 doesn't support it)
879
+ elsif error.message.match?(/unknown subcommand.*setinfo/i)
880
+ # Silently ignore - CLIENT SETINFO is not supported on Redis < 7.2
859
881
  else
860
882
  raise
861
883
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.26.4
4
+ version: 0.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier