dalli 5.4.0 → 5.5.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 +4 -4
- data/CHANGELOG.md +15 -0
- data/README.md +13 -2
- data/lib/dalli/client.rb +34 -9
- data/lib/dalli/key_grouping.rb +15 -5
- data/lib/dalli/pipelined_deleter.rb +5 -2
- data/lib/dalli/pipelined_setter.rb +4 -1
- data/lib/dalli/protocol/connection_manager.rb +87 -14
- data/lib/dalli/protocol/meta.rb +17 -5
- data/lib/dalli/socket.rb +56 -1
- data/lib/dalli/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e6ce508461bbd9a0e55caf7b0fdc822e838327aac1763bf1a490a44ff7ce9b9a
|
|
4
|
+
data.tar.gz: 65686d408cd3f5a797d115b529e653d1a9b53cfad3ca30be60968b16ea5ee2a4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ab5dc6d113ef9f3ca1474ca94d34d235967875d2b9d2f95e41910562120cad91f30c3733f2db5ccac61f4027399c97929f815c40cf6f5b4161fd1d1fe9f5a9ef
|
|
7
|
+
data.tar.gz: 896c1ffcf6b6d032631a64cf0b5e4f3510f07e22bc6da9328dff2b0f3e498cd80902304cd97ef940ba5609ed7f4b51bd48325a652c8cf3b60210dff2e8f5ac31
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,21 @@ Dalli Changelog
|
|
|
4
4
|
Unreleased
|
|
5
5
|
==========
|
|
6
6
|
|
|
7
|
+
5.5.0
|
|
8
|
+
==========
|
|
9
|
+
|
|
10
|
+
Changes:
|
|
11
|
+
|
|
12
|
+
- `set_multi` and `delete_multi` now raise `Dalli::RingError` when some keys have no server available, as `set` and `delete` do, after writing or deleting the keys that do. Before, without `failover` the keys for a server already down when the call started were skipped without an error, as were all keys when no server was left with `failover`. `get_multi` and the other reads still report such keys as misses (#1342)
|
|
13
|
+
|
|
14
|
+
Performance:
|
|
15
|
+
|
|
16
|
+
- A value of 64 KB or more is no longer copied into a new String with its request line before it's sent: over TCP and Unix sockets it's written from the caller's String, so a large `set`, `add`, `replace`, `append` or `prepend` allocates about its own size less. A 700 KB `set` went from about 7.4k to 8.7k per second locally. TLS connections are unchanged (#1343)
|
|
17
|
+
|
|
18
|
+
New features:
|
|
19
|
+
|
|
20
|
+
- TCP keepalive timing can be set per client with `keepalive_idle`, `keepalive_interval` and `keepalive_count`, which set `TCP_KEEPIDLE` (`TCP_KEEPALIVE` on macOS), `TCP_KEEPINTVL` and `TCP_KEEPCNT` where the platform supports them. By default Dalli now lowers the idle time before the first probe to 120 seconds, leaving a lower host setting alone; Linux and macOS wait 7200 seconds, longer than EC2 Nitro v6 connection tracking (350 seconds by default) and many firewalls keep an idle connection, so pooled connections were dropped silently and the next request waited out `socket_timeout`. `keepalive_idle: false` keeps the kernel's idle time (#1341, proposed by @KieranP in #1340)
|
|
21
|
+
|
|
7
22
|
5.4.0
|
|
8
23
|
==========
|
|
9
24
|
|
data/README.md
CHANGED
|
@@ -116,7 +116,18 @@ A request that failed after some of it was sent may have been applied by memcach
|
|
|
116
116
|
|
|
117
117
|
A connection that sat unused for over a second is checked before the next request. If memcached (with `idle_timeout`), a load balancer or a firewall closed it meanwhile, Dalli reconnects before sending, so the request isn't lost and doesn't count as a failure. A connection dropped without the other end closing it can't be detected this way, and a request on it fails as above.
|
|
118
118
|
|
|
119
|
-
|
|
119
|
+
TCP keepalive (`keepalive: true`, the default) keeps idle connections from being dropped that way. Linux and macOS wait 7200 seconds before the first keepalive probe, longer than many firewalls, NAT gateways and EC2 security group connection tracking (350 seconds by default on Nitro v6 instance types) keep an idle connection. So Dalli lowers that wait to 120 seconds on its own sockets, leaving it alone where the host already has it lower. The timing can be set per client:
|
|
120
|
+
|
|
121
|
+
```ruby
|
|
122
|
+
Dalli::Client.new('localhost:11211',
|
|
123
|
+
keepalive_idle: 60, # seconds idle before the first probe (TCP_KEEPIDLE, TCP_KEEPALIVE on macOS); false keeps the kernel's
|
|
124
|
+
keepalive_interval: 15, # seconds between probes (TCP_KEEPINTVL); the kernel's by default
|
|
125
|
+
keepalive_count: 4) # unanswered probes before the connection is closed (TCP_KEEPCNT); the kernel's by default
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Where the engine or platform doesn't support one of these, it's skipped and the kernel's timing applies. On Linux, CRuby, JRuby and TruffleRuby support all three. On macOS, JRuby supports none of them and TruffleRuby has no `keepalive_idle`.
|
|
129
|
+
|
|
130
|
+
The multi-key reads (`get_multi`, `get_multi_cas` and `get_multi_with_metadata`) skip a server that is already down when they start: its keys are missing from the result, or go to another server with `failover`. `set_multi` and `delete_multi` write or delete the keys that have a server, then raise `Dalli::RingError` if any had none (a server down when they start, without `failover`, or no server left with it), as `set` and `delete` do for a single key. A server that fails during the operation is retried. If the retry can't connect to it, then without `failover` the operation raises `Dalli::NetworkError`, rather than returning that server's keys as misses or leaving them unwritten, and with `failover` its keys go to another server, or are left out of a read when no server is left. If the retry connects but the server fails again, it's marked down partway through the operation, which raises `Dalli::NetworkError` with or without `failover`.
|
|
120
131
|
|
|
121
132
|
A retried `get_multi` or `get_multi_cas` starts the batch again, so a block passed to either can be yielded a key it was already yielded before the error. Make the block safe to call twice for a key, or call the method without a block and use the Hash it returns. `get_multi_with_metadata` yields only after every server has replied, so its block sees each key once.
|
|
122
133
|
|
|
@@ -149,7 +160,7 @@ See the [5.0-Upgrade.md](5.0-Upgrade.md) guide for upgrade information.
|
|
|
149
160
|
|
|
150
161
|
### Counters and verify_reply_keys
|
|
151
162
|
|
|
152
|
-
memcached
|
|
163
|
+
memcached 1.6.45 and earlier name an item made by auto-create for a base64-encoded key (one with whitespace, control or non-ASCII characters) by its raw key bytes in later replies. memcached 1.6.46 fixes this. Since 5.2.3, Dalli creates counters for such keys with an add rather than auto-create, but items created before then, or by other clients, can still be named that way on 1.6.45 and earlier. If your servers run 1.6.45 or earlier and keys passed to `incr` or `decr` with an initial value can contain untrusted input, upgrade memcached or set `verify_reply_keys: true`: multi-gets then ignore replies for keys they didn't request, at a cost of about 15% per multi-get.
|
|
153
164
|
|
|
154
165
|
## TLS
|
|
155
166
|
|
data/lib/dalli/client.rb
CHANGED
|
@@ -536,9 +536,12 @@ module Dalli
|
|
|
536
536
|
# A transient network error is retried automatically. If a server stops
|
|
537
537
|
# during the call and is still unreachable after retrying, raises
|
|
538
538
|
# Dalli::NetworkError only with `failover: false`; with failover (the
|
|
539
|
-
# default) its keys go to another server
|
|
540
|
-
#
|
|
541
|
-
#
|
|
539
|
+
# default) its keys go to another server. Keys with no server available
|
|
540
|
+
# (a server down when the call starts, without failover, or no server
|
|
541
|
+
# left with it) aren't written: the other keys are, then
|
|
542
|
+
# Dalli::RingError is raised, as #set raises for a key with no server.
|
|
543
|
+
# See "Network Errors and Retries" in the README. Keys already written to
|
|
544
|
+
# other servers before an error are not rolled back.
|
|
542
545
|
#
|
|
543
546
|
# @param hash [Hash] key-value pairs to set
|
|
544
547
|
# @param ttl [Integer] time-to-live in seconds (optional, uses default if not provided)
|
|
@@ -547,6 +550,8 @@ module Dalli
|
|
|
547
550
|
# @return [void]
|
|
548
551
|
# @raise [Dalli::NetworkError] if a server stops during the call and is
|
|
549
552
|
# still unreachable after retrying, without failover
|
|
553
|
+
# @raise [Dalli::RingError] if some keys had no server available, after
|
|
554
|
+
# writing the others
|
|
550
555
|
#
|
|
551
556
|
# Example:
|
|
552
557
|
# client.set_multi({ 'key1' => 'value1', 'key2' => 'value2' }, 300)
|
|
@@ -661,10 +666,12 @@ module Dalli
|
|
|
661
666
|
# is whichever one the caller asked for. This is best-effort: a transient
|
|
662
667
|
# network error is retried automatically, and keys handled before the
|
|
663
668
|
# error are not recounted, so the result may under-report when a retry
|
|
664
|
-
# occurs. A server that stops during the call
|
|
665
|
-
# #set_multi.
|
|
669
|
+
# occurs. A server that stops during the call, and keys with no server
|
|
670
|
+
# available, are handled as in #set_multi.
|
|
666
671
|
# @raise [Dalli::NetworkError] if a server stops during the call and is
|
|
667
672
|
# still unreachable after retrying, without failover
|
|
673
|
+
# @raise [Dalli::RingError] if some keys had no server available, after
|
|
674
|
+
# deleting the others
|
|
668
675
|
#
|
|
669
676
|
# Example:
|
|
670
677
|
# client.delete_multi(['key1', 'key2', 'key3'])
|
|
@@ -909,8 +916,9 @@ module Dalli
|
|
|
909
916
|
# propagates to the caller rather than being swallowed into a silently
|
|
910
917
|
# wrong result, including (without failover) the server going down before
|
|
911
918
|
# the retry, which single_server raises for. Otherwise finding no live
|
|
912
|
-
# server
|
|
913
|
-
# key it can't route
|
|
919
|
+
# server returns no results for the reads, as Ring#keys_grouped_by_server
|
|
920
|
+
# drops a key it can't route, and raises Dalli::RingError for the writes,
|
|
921
|
+
# as the pipelined writes do (see KeyGrouping#raise_for_unrouted!). Each
|
|
914
922
|
# method sets retried before it retries; a local keeps its value across
|
|
915
923
|
# a retry.
|
|
916
924
|
def single_server_get_multi(keys, req_options = nil)
|
|
@@ -960,7 +968,7 @@ module Dalli
|
|
|
960
968
|
def single_server_set_multi(hash, ttl, req_options)
|
|
961
969
|
retried ||= false
|
|
962
970
|
pairs = hash.transform_keys { |k| @key_manager.validate_key(k.to_s) }
|
|
963
|
-
|
|
971
|
+
server = single_server(retried: retried) or raise Dalli::RingError, 'No server available'
|
|
964
972
|
|
|
965
973
|
server.request(:write_multi_req, pairs, ttl, req_options)
|
|
966
974
|
rescue Dalli::RetryableNetworkError => e
|
|
@@ -973,7 +981,7 @@ module Dalli
|
|
|
973
981
|
def single_server_delete_multi(keys, req_options = nil)
|
|
974
982
|
retried ||= false
|
|
975
983
|
validated_keys = keys.map { |k| @key_manager.validate_key(k.to_s) }
|
|
976
|
-
|
|
984
|
+
server = single_server(retried: retried) or raise Dalli::RingError, 'No server available'
|
|
977
985
|
|
|
978
986
|
server.request(:delete_multi_req, validated_keys, req_options)
|
|
979
987
|
rescue Dalli::RetryableNetworkError => e
|
|
@@ -1202,9 +1210,26 @@ module Dalli
|
|
|
1202
1210
|
def normalize_options(options)
|
|
1203
1211
|
opts = options.except(:username, :password)
|
|
1204
1212
|
opts[:expires_in] = ttl_to_i(:expires_in, opts[:expires_in]) if opts[:expires_in]
|
|
1213
|
+
validate_keepalive_options!(opts)
|
|
1205
1214
|
opts
|
|
1206
1215
|
end
|
|
1207
1216
|
|
|
1217
|
+
# The largest values Linux accepts (MAX_TCP_KEEPIDLE, MAX_TCP_KEEPINTVL,
|
|
1218
|
+
# MAX_TCP_KEEPCNT). A value it rejects would fail every connect, so it's
|
|
1219
|
+
# checked when the client is created.
|
|
1220
|
+
KEEPALIVE_OPTION_MAXIMUMS = { keepalive_idle: 32_767, keepalive_interval: 32_767, keepalive_count: 127 }.freeze
|
|
1221
|
+
private_constant :KEEPALIVE_OPTION_MAXIMUMS
|
|
1222
|
+
|
|
1223
|
+
def validate_keepalive_options!(opts)
|
|
1224
|
+
KEEPALIVE_OPTION_MAXIMUMS.each do |name, max|
|
|
1225
|
+
value = opts[name]
|
|
1226
|
+
next if value.nil? || (value == false && name == :keepalive_idle)
|
|
1227
|
+
next if value.is_a?(Integer) && value.between?(1, max)
|
|
1228
|
+
|
|
1229
|
+
raise ArgumentError, "#{name} must be an Integer between 1 and #{max}, got #{value.inspect}"
|
|
1230
|
+
end
|
|
1231
|
+
end
|
|
1232
|
+
|
|
1208
1233
|
REMOVED_OPTIONS = {
|
|
1209
1234
|
protocol: 'Dalli 5.0 only supports the meta protocol. The :protocol option has been removed.',
|
|
1210
1235
|
username: 'Dalli 5.0 removed SASL authentication support. The :username option is ignored.',
|
data/lib/dalli/key_grouping.rb
CHANGED
|
@@ -6,10 +6,12 @@ module Dalli
|
|
|
6
6
|
# PipelinedSetter and PipelinedDeleter. Each of them is created for one
|
|
7
7
|
# operation, and retries the whole operation after a RetryableNetworkError.
|
|
8
8
|
#
|
|
9
|
-
# A key with no server to go to is left out
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
9
|
+
# A key with no server to go to is left out: a server that was down before
|
|
10
|
+
# the operation started is skipped, and with failover its keys go to
|
|
11
|
+
# another server. get_multi reports such keys as misses. set_multi and
|
|
12
|
+
# delete_multi write the other keys, then raise Dalli::RingError (see
|
|
13
|
+
# #raise_for_unrouted!), as set and delete do for a key with no server.
|
|
14
|
+
# Without failover a retry is different. The server was there for the first attempt and failed while
|
|
13
15
|
# serving it, so if the retry finds it unreachable, the operation raises
|
|
14
16
|
# Dalli::NetworkError rather than returning that server's keys as misses or
|
|
15
17
|
# leaving them unwritten. With failover the keys go to another server, or
|
|
@@ -24,7 +26,7 @@ module Dalli
|
|
|
24
26
|
# action names the operation in the debug log ('get', 'set', 'delete')
|
|
25
27
|
def groups_for_keys(keys, action)
|
|
26
28
|
groups = @ring.keys_grouped_by_server(keys)
|
|
27
|
-
unrouted = groups.delete(nil)
|
|
29
|
+
@unrouted = unrouted = groups.delete(nil)
|
|
28
30
|
check_unrouted!(unrouted) unless @ring.failover?
|
|
29
31
|
if unrouted
|
|
30
32
|
Dalli.logger.debug do
|
|
@@ -34,6 +36,14 @@ module Dalli
|
|
|
34
36
|
groups
|
|
35
37
|
end
|
|
36
38
|
|
|
39
|
+
# For the writes: once the keys that had a server are done, raises if any
|
|
40
|
+
# had none. verb says what didn't happen to them ('written', 'deleted').
|
|
41
|
+
def raise_for_unrouted!(verb)
|
|
42
|
+
return unless @unrouted
|
|
43
|
+
|
|
44
|
+
raise Dalli::RingError, "No server available for #{@unrouted.length} keys, which were not #{verb}"
|
|
45
|
+
end
|
|
46
|
+
|
|
37
47
|
# The first attempt records the keys that already had no server; a retry
|
|
38
48
|
# raises for any other key that has none now
|
|
39
49
|
def check_unrouted!(unrouted)
|
|
@@ -29,15 +29,18 @@ module Dalli
|
|
|
29
29
|
# error are not recounted, so the result may under-report when a retry
|
|
30
30
|
# occurs. If a server stops during the call and is still unreachable
|
|
31
31
|
# after retrying, raises Dalli::NetworkError without failover (see
|
|
32
|
-
# KeyGrouping).
|
|
32
|
+
# KeyGrouping). Keys with no server available are not deleted; the
|
|
33
|
+
# others are, then Dalli::RingError is raised.
|
|
33
34
|
##
|
|
34
35
|
def process(keys, req_options = nil)
|
|
35
36
|
return 0 if keys.empty?
|
|
36
37
|
|
|
37
|
-
@ring.lock do
|
|
38
|
+
deleted = @ring.lock do
|
|
38
39
|
groups = setup_requests(keys, req_options)
|
|
39
40
|
finish_requests(groups)
|
|
40
41
|
end
|
|
42
|
+
raise_for_unrouted!('deleted')
|
|
43
|
+
deleted
|
|
41
44
|
rescue Dalli::RetryableNetworkError => e
|
|
42
45
|
Dalli.logger.debug { e.inspect }
|
|
43
46
|
Dalli.logger.debug { 'retrying pipelined deletes because of network error' }
|
|
@@ -19,7 +19,9 @@ module Dalli
|
|
|
19
19
|
#
|
|
20
20
|
# A transient network error is retried automatically. If a server stops
|
|
21
21
|
# during the call and is still unreachable after retrying, raises
|
|
22
|
-
# Dalli::NetworkError without failover (see KeyGrouping).
|
|
22
|
+
# Dalli::NetworkError without failover (see KeyGrouping). Keys with no
|
|
23
|
+
# server available are not written; the others are, then
|
|
24
|
+
# Dalli::RingError is raised.
|
|
23
25
|
#
|
|
24
26
|
# A value that can't be marshalled or is over value_max_bytes is skipped;
|
|
25
27
|
# the other keys are still stored, then the first such error is raised.
|
|
@@ -37,6 +39,7 @@ module Dalli
|
|
|
37
39
|
servers = setup_requests(hash, ttl, req_options)
|
|
38
40
|
finish_requests(servers)
|
|
39
41
|
end
|
|
42
|
+
raise_for_unrouted!('written')
|
|
40
43
|
raise @value_error if @value_error
|
|
41
44
|
rescue Dalli::RetryableNetworkError => e
|
|
42
45
|
Dalli.logger.debug { e.inspect }
|
|
@@ -331,7 +331,9 @@ module Dalli
|
|
|
331
331
|
# TruffleRuby's gets with a limit doesn't find a two-byte separator whose
|
|
332
332
|
# bytes arrive in separate reads: it waits for more, or returns the line
|
|
333
333
|
# without them. Every reply line ends in CRLF, checked after the read,
|
|
334
|
-
# so reading to the LF finds the same lines.
|
|
334
|
+
# so reading to the LF finds the same lines. Fixed upstream after 40.0.0
|
|
335
|
+
# (truffleruby/truffleruby#4583); keep this while 40.0.0 and earlier are
|
|
336
|
+
# supported.
|
|
335
337
|
LINE_SEPARATOR = RUBY_ENGINE == 'truffleruby' ? "\n" : "\r\n"
|
|
336
338
|
|
|
337
339
|
def read_line
|
|
@@ -429,6 +431,24 @@ module Dalli
|
|
|
429
431
|
error_on_request!(e)
|
|
430
432
|
end
|
|
431
433
|
|
|
434
|
+
# A value at least this large is buffered as it is, between its request
|
|
435
|
+
# line and terminator, rather than copied into one String with them.
|
|
436
|
+
# It's past WRITE_BUFFER_FLUSH_BYTES, so it's sent before the call
|
|
437
|
+
# returns and the buffer never keeps the caller's String.
|
|
438
|
+
LARGE_VALUE_BYTES = WRITE_BUFFER_FLUSH_BYTES
|
|
439
|
+
VALUE_TERMINATOR = "\r\n"
|
|
440
|
+
|
|
441
|
+
# Writes a storage request: the request line, then value and its
|
|
442
|
+
# terminator. Only for a value of LARGE_VALUE_BYTES or more; a smaller
|
|
443
|
+
# one costs less copied into the request.
|
|
444
|
+
def write_request_with_value(request, value)
|
|
445
|
+
@write_buffer << request << value << VALUE_TERMINATOR
|
|
446
|
+
@write_buffer_bytes += request.bytesize + value.bytesize + VALUE_TERMINATOR.bytesize
|
|
447
|
+
flush_write_buffer
|
|
448
|
+
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, IOError => e
|
|
449
|
+
error_on_request!(e)
|
|
450
|
+
end
|
|
451
|
+
|
|
432
452
|
# Non-blocking read. Here to support the operation
|
|
433
453
|
# of the get_multi operation. Held replies come first.
|
|
434
454
|
def read_available(...)
|
|
@@ -587,12 +607,20 @@ module Dalli
|
|
|
587
607
|
|
|
588
608
|
# Set before writing: a write that fails may still have sent some bytes
|
|
589
609
|
@request_sent = true if @request_in_progress
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
if @
|
|
593
|
-
|
|
610
|
+
# A large value is sent from its own String outside TLS, which gains
|
|
611
|
+
# nothing from it, as each write becomes its own TLS record
|
|
612
|
+
if @write_buffer_bytes >= LARGE_VALUE_BYTES && @write_buffer.size > 1 &&
|
|
613
|
+
!@sock.is_a?(OpenSSL::SSL::SSLSocket)
|
|
614
|
+
@unanswered_bytes += @write_buffer_bytes
|
|
615
|
+
send_chunks_while_reading(write_buffer_chunks)
|
|
594
616
|
else
|
|
595
|
-
|
|
617
|
+
data = @write_buffer.size == 1 ? @write_buffer.first : joined_write_buffer
|
|
618
|
+
@unanswered_bytes += data.bytesize
|
|
619
|
+
if @unanswered_bytes <= BLOCKING_WRITE_BYTES
|
|
620
|
+
write_blocking(data)
|
|
621
|
+
else
|
|
622
|
+
send_while_reading(data)
|
|
623
|
+
end
|
|
596
624
|
end
|
|
597
625
|
discard_write_buffer
|
|
598
626
|
end
|
|
@@ -613,10 +641,22 @@ module Dalli
|
|
|
613
641
|
def send_while_reading(data)
|
|
614
642
|
return write_blocking(data) if HALF_DUPLEX_TLS && @sock.is_a?(OpenSSL::SSL::SSLSocket)
|
|
615
643
|
|
|
644
|
+
send_chunk_while_reading(data, nil, false)
|
|
645
|
+
end
|
|
646
|
+
|
|
647
|
+
# As #send_while_reading, for the buffer in several Strings (see
|
|
648
|
+
# #write_buffer_chunks), sent in order
|
|
649
|
+
def send_chunks_while_reading(chunks)
|
|
650
|
+
counted = false
|
|
651
|
+
chunks.each { |chunk| counted = send_chunk_while_reading(chunk, chunks, counted) }
|
|
652
|
+
end
|
|
653
|
+
|
|
654
|
+
# Sends data, one of chunks when that's given. Returns whether the
|
|
655
|
+
# lines sent in this flush have been counted.
|
|
656
|
+
def send_chunk_while_reading(data, chunks, counted)
|
|
616
657
|
total = data.bytesize
|
|
617
658
|
sent = 0
|
|
618
659
|
deadline = nil
|
|
619
|
-
counted = false
|
|
620
660
|
while sent < total
|
|
621
661
|
rest = sent.zero? ? data : data.byteslice(sent, total - sent)
|
|
622
662
|
result = @sock.write_nonblock(rest, exception: false)
|
|
@@ -625,11 +665,18 @@ module Dalli
|
|
|
625
665
|
deadline = nil
|
|
626
666
|
else
|
|
627
667
|
# Replies can only be held from here, so the lines are counted now
|
|
628
|
-
counted ||= (
|
|
668
|
+
counted ||= count_lines_sent(chunks || [data])
|
|
629
669
|
deadline ||= write_deadline
|
|
630
670
|
wait_to_send(result, deadline)
|
|
631
671
|
end
|
|
632
672
|
end
|
|
673
|
+
counted
|
|
674
|
+
end
|
|
675
|
+
|
|
676
|
+
def count_lines_sent(chunks)
|
|
677
|
+
held = (@held_replies ||= HeldReplies.new(max_reply_value_bytes))
|
|
678
|
+
chunks.each { |chunk| held.count_lines(chunk) }
|
|
679
|
+
true
|
|
633
680
|
end
|
|
634
681
|
|
|
635
682
|
# Waits until the socket takes more, holding any replies that arrive.
|
|
@@ -709,22 +756,48 @@ module Dalli
|
|
|
709
756
|
error_on_request!("Reply line without CRLF in the first #{MAX_LINE_BYTES} bytes")
|
|
710
757
|
end
|
|
711
758
|
|
|
759
|
+
def joined_write_buffer
|
|
760
|
+
joined_parts(@write_buffer, @write_buffer_bytes)
|
|
761
|
+
end
|
|
762
|
+
|
|
712
763
|
# Joined as bytes: requests can mix UTF-8 and binary strings, which
|
|
713
764
|
# String#<< refuses to combine when both hold non-ASCII bytes
|
|
714
765
|
if String.method_defined?(:append_as_bytes)
|
|
715
|
-
def
|
|
716
|
-
out = String.new(capacity:
|
|
717
|
-
|
|
766
|
+
def joined_parts(parts, bytes)
|
|
767
|
+
out = String.new(capacity: bytes, encoding: Encoding::BINARY)
|
|
768
|
+
parts.each { |part| out.append_as_bytes(part) }
|
|
718
769
|
out
|
|
719
770
|
end
|
|
720
771
|
else
|
|
721
|
-
def
|
|
722
|
-
out = String.new(capacity:
|
|
723
|
-
|
|
772
|
+
def joined_parts(parts, bytes)
|
|
773
|
+
out = String.new(capacity: bytes, encoding: Encoding::BINARY)
|
|
774
|
+
parts.each { |part| out << part.b }
|
|
724
775
|
out
|
|
725
776
|
end
|
|
726
777
|
end
|
|
727
778
|
|
|
779
|
+
# The buffer as Strings to send in order: each large value as it is, and
|
|
780
|
+
# the parts between them joined, so small requests still go out together
|
|
781
|
+
def write_buffer_chunks
|
|
782
|
+
chunks = []
|
|
783
|
+
small = []
|
|
784
|
+
@write_buffer.each do |part|
|
|
785
|
+
if part.bytesize < LARGE_VALUE_BYTES
|
|
786
|
+
small << part
|
|
787
|
+
else
|
|
788
|
+
chunks << joined_small_parts(small) unless small.empty?
|
|
789
|
+
small = []
|
|
790
|
+
chunks << part
|
|
791
|
+
end
|
|
792
|
+
end
|
|
793
|
+
chunks << joined_small_parts(small) unless small.empty?
|
|
794
|
+
chunks
|
|
795
|
+
end
|
|
796
|
+
|
|
797
|
+
def joined_small_parts(parts)
|
|
798
|
+
parts.size == 1 ? parts.first : joined_parts(parts, parts.sum(&:bytesize))
|
|
799
|
+
end
|
|
800
|
+
|
|
728
801
|
def discard_write_buffer
|
|
729
802
|
@write_buffer.clear
|
|
730
803
|
@write_buffer_bytes = 0
|
data/lib/dalli/protocol/meta.rb
CHANGED
|
@@ -176,7 +176,7 @@ module Dalli
|
|
|
176
176
|
ttl: ttl, mode: mode, quiet: quiet,
|
|
177
177
|
**routing_token_kwargs(options))
|
|
178
178
|
end
|
|
179
|
-
|
|
179
|
+
write_request_with_value(req, value)
|
|
180
180
|
finish_write(quiet)
|
|
181
181
|
end
|
|
182
182
|
# rubocop:enable Metrics/ParameterLists
|
|
@@ -197,7 +197,7 @@ module Dalli
|
|
|
197
197
|
req = RequestFormatter.meta_set(key: key, value: value,
|
|
198
198
|
cas: cas, ttl: ttl, mode: mode, quiet: quiet?,
|
|
199
199
|
**routing_token_kwargs(options))
|
|
200
|
-
|
|
200
|
+
write_request_with_value(req, value)
|
|
201
201
|
finish_write(quiet?)
|
|
202
202
|
end
|
|
203
203
|
|
|
@@ -205,6 +205,16 @@ module Dalli
|
|
|
205
205
|
# UTF-16 value, or a routing token with non-ASCII characters next to a
|
|
206
206
|
# binary value. memcached stores bytes, so join the bytes then. Joining
|
|
207
207
|
# the Strings first keeps the common case free of extra checks.
|
|
208
|
+
# A large value is buffered as it is rather than copied into the
|
|
209
|
+
# request (see ConnectionManager#write_request_with_value)
|
|
210
|
+
def write_request_with_value(req, value)
|
|
211
|
+
if value.bytesize >= ConnectionManager::LARGE_VALUE_BYTES
|
|
212
|
+
@connection_manager.write_request_with_value(req, value)
|
|
213
|
+
else
|
|
214
|
+
write(request_with_value(req, value))
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
208
218
|
def request_with_value(req, value)
|
|
209
219
|
"#{req}#{value}#{TERMINATOR}"
|
|
210
220
|
rescue Encoding::CompatibilityError
|
|
@@ -253,9 +263,11 @@ module Dalli
|
|
|
253
263
|
# rubocop:disable-next Metrics/ParameterLists
|
|
254
264
|
def decr_incr(incr, key, delta, ttl, initial, options = nil)
|
|
255
265
|
ttl = initial ? TtlSanitizer.sanitize(ttl) : nil # Only set a TTL if we want to set a value on miss
|
|
256
|
-
# memcached
|
|
257
|
-
# base64-encoded key without its binary-key
|
|
258
|
-
# name it with the raw key bytes. Create such
|
|
266
|
+
# memcached 1.6.45 and earlier store an item created by ma's
|
|
267
|
+
# auto-create (N/J) for a base64-encoded key without its binary-key
|
|
268
|
+
# flag, and later replies name it with the raw key bytes. Create such
|
|
269
|
+
# counters with an add. Fixed in 1.6.46; keep this while earlier
|
|
270
|
+
# versions are supported.
|
|
259
271
|
return decr_incr_with_add(incr, key, delta, ttl, initial, options) if initial && KeyRegularizer.required?(key)
|
|
260
272
|
|
|
261
273
|
write(RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: initial, incr: incr, ttl: ttl,
|
data/lib/dalli/socket.rb
CHANGED
|
@@ -218,7 +218,62 @@ module Dalli
|
|
|
218
218
|
|
|
219
219
|
def self.configure_tcp_options(sock, options)
|
|
220
220
|
sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true)
|
|
221
|
-
|
|
221
|
+
return unless options[:keepalive]
|
|
222
|
+
|
|
223
|
+
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_KEEPALIVE, true)
|
|
224
|
+
configure_keepalive_timing(sock, options)
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# The idle time before the first keepalive probe is TCP_KEEPIDLE on
|
|
228
|
+
# Linux and TCP_KEEPALIVE on macOS. Each is nil where the engine doesn't
|
|
229
|
+
# define it (JRuby on macOS defines only TCP_KEEPALIVE, and rejects it).
|
|
230
|
+
KEEPALIVE_IDLE = if ::Socket.const_defined?(:TCP_KEEPIDLE)
|
|
231
|
+
::Socket::TCP_KEEPIDLE
|
|
232
|
+
elsif ::Socket.const_defined?(:TCP_KEEPALIVE)
|
|
233
|
+
::Socket::TCP_KEEPALIVE
|
|
234
|
+
end
|
|
235
|
+
KEEPALIVE_INTERVAL = (::Socket::TCP_KEEPINTVL if ::Socket.const_defined?(:TCP_KEEPINTVL))
|
|
236
|
+
KEEPALIVE_COUNT = (::Socket::TCP_KEEPCNT if ::Socket.const_defined?(:TCP_KEEPCNT))
|
|
237
|
+
private_constant :KEEPALIVE_IDLE, :KEEPALIVE_INTERVAL, :KEEPALIVE_COUNT
|
|
238
|
+
|
|
239
|
+
# Seconds a connection may sit idle before the first keepalive probe,
|
|
240
|
+
# unless keepalive_idle says otherwise. Linux and macOS wait 7200, longer
|
|
241
|
+
# than many firewalls, NAT gateways and EC2 connection tracking (350 on
|
|
242
|
+
# Nitro v6) keep an idle connection, so they forget it and the next
|
|
243
|
+
# request on it waits out socket_timeout.
|
|
244
|
+
DEFAULT_KEEPALIVE_IDLE = 120
|
|
245
|
+
|
|
246
|
+
# keepalive_idle unset lowers the idle time to DEFAULT_KEEPALIVE_IDLE
|
|
247
|
+
# without raising one the host has set lower; false leaves the kernel's.
|
|
248
|
+
# keepalive_interval and keepalive_count are left to the kernel unless set.
|
|
249
|
+
def self.configure_keepalive_timing(sock, options)
|
|
250
|
+
idle = options[:keepalive_idle]
|
|
251
|
+
if idle.nil?
|
|
252
|
+
lower_keepalive_option(sock, KEEPALIVE_IDLE, DEFAULT_KEEPALIVE_IDLE)
|
|
253
|
+
elsif idle
|
|
254
|
+
set_keepalive_option(sock, KEEPALIVE_IDLE, idle)
|
|
255
|
+
end
|
|
256
|
+
set_keepalive_option(sock, KEEPALIVE_INTERVAL, options[:keepalive_interval]) if options[:keepalive_interval]
|
|
257
|
+
set_keepalive_option(sock, KEEPALIVE_COUNT, options[:keepalive_count]) if options[:keepalive_count]
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def self.set_keepalive_option(sock, optname, value)
|
|
261
|
+
return unless optname
|
|
262
|
+
|
|
263
|
+
sock.setsockopt(::Socket::IPPROTO_TCP, optname, value)
|
|
264
|
+
rescue Errno::ENOPROTOOPT, Errno::EOPNOTSUPP
|
|
265
|
+
# The engine or platform doesn't support it; keepalive still works
|
|
266
|
+
# with the kernel's timing
|
|
267
|
+
nil
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def self.lower_keepalive_option(sock, optname, value)
|
|
271
|
+
return unless optname
|
|
272
|
+
|
|
273
|
+
current = sock.getsockopt(::Socket::IPPROTO_TCP, optname).int
|
|
274
|
+
sock.setsockopt(::Socket::IPPROTO_TCP, optname, value) if current > value
|
|
275
|
+
rescue Errno::ENOPROTOOPT, Errno::EOPNOTSUPP
|
|
276
|
+
nil
|
|
222
277
|
end
|
|
223
278
|
|
|
224
279
|
def self.configure_socket_buffers(sock, options)
|
data/lib/dalli/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dalli
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 5.
|
|
4
|
+
version: 5.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Peter M. Goldstein
|
|
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
96
96
|
- !ruby/object:Gem::Version
|
|
97
97
|
version: "0"
|
|
98
98
|
requirements: []
|
|
99
|
-
rubygems_version: 4.1.
|
|
99
|
+
rubygems_version: 4.1.1
|
|
100
100
|
specification_version: 4
|
|
101
101
|
summary: High performance memcached client for Ruby
|
|
102
102
|
test_files: []
|