Class: Dalli::Protocol::Meta::ResponseProcessor
- Inherits:
-
Object
- Object
- Dalli::Protocol::Meta::ResponseProcessor
- Defined in:
- lib/dalli/protocol/response_processor.rb
Overview
Class that encapsulates logic for processing meta protocol responses from memcached. Includes logic for pulling data from an IO source and parsing into local values. Handles errors on unexpected values.
Constant Summary collapse
- EN =
'EN'- END_TOKEN =
'END'- EX =
'EX'- HD =
'HD'- MN =
'MN'- NF =
'NF'- NS =
'NS'- OK =
'OK'- RESET =
'RESET'- STAT =
'STAT'- VA =
'VA'- VERSION =
'VERSION'- SERVER_ERROR =
'SERVER_ERROR'- T_OK =
[OK].freeze
- T_RESET =
[RESET].freeze
- T_EN_HD =
[EN, HD].freeze
- T_VERSION =
[VERSION].freeze
- T_VA_EN_HD =
[VA, EN, HD].freeze
- T_HD_NF_EX =
[HD, NF, EX].freeze
- T_HD_NS_NF_EX =
[HD, NS, NF, EX].freeze
- T_VA_NF_NS_EX =
[VA, NF, NS, EX].freeze
- T_END_TOKEN_STAT =
[END_TOKEN, STAT].freeze
Instance Method Summary collapse
- #bitflags_from_tokens(tokens) ⇒ Object
- #body_len_from_tokens(tokens) ⇒ Object
- #build_metadata_result(tokens) ⇒ Object
- #cas_from_tokens(tokens) ⇒ Object
- #consume_all_responses_until_mn ⇒ Object
- #decr_incr ⇒ Object
- #error_on_unexpected!(expected_codes) ⇒ Object
- #flush ⇒ Object
- #full_response_from_buffer(tokens, body, resp_size) ⇒ Object
-
#getk_response_from_buffer(buf, offset = 0) ⇒ Object
This method returns an array of values used in a pipelined getk process.
-
#hit_status_from_tokens(tokens) ⇒ Object
Returns true if item was previously hit, false if first access, nil if not requested The h flag returns h0 (first access) or h1 (previously accessed).
-
#initialize(io_source, value_marshaller) ⇒ ResponseProcessor
constructor
A new instance of ResponseProcessor.
- #key_from_tokens(tokens) ⇒ Object
-
#last_access_from_tokens(tokens) ⇒ Object
Returns seconds since last access, or nil if not requested The l flag returns l
. - #meta_delete ⇒ Object
-
#meta_get_with_metadata(cache_nils: false, return_hit_status: false, return_last_access: false, return_ttl_remaining: false) ⇒ Object
Returns a hash with all requested metadata: - :value - the cached value (or nil if miss) - :cas - the CAS value (if return_cas was requested) - :won_recache - true if client won the right to recache (W flag) - :stale - true if the item is stale (X flag) - :lost_recache - true if another client is already recaching (Z flag) - :hit_before - true/false if item was previously accessed (h flag, if requested) - :last_access - seconds since last access (l flag, if requested).
- #meta_get_with_value(cache_nils: false) ⇒ Object
- #meta_get_with_value_and_cas ⇒ Object
- #meta_get_without_value ⇒ Object
- #meta_set_append_prepend ⇒ Object
- #meta_set_with_cas ⇒ Object
- #next_line_to_tokens ⇒ Object
- #parse_value_from_tokens(tokens, cache_nils) ⇒ Object
-
#pipelined_delete_non_deletions ⇒ Object
Consumes the responses to a batch of quiet (pipelined) delete requests, which are terminated by a noop (MN).
- #read_data(data_size) ⇒ Object
- #read_line ⇒ Object
- #reset ⇒ Object
-
#stale_from_tokens(tokens) ⇒ Object
Detects the X presence flag, set when an item has been marked stale by a prior
md key I. - #stats ⇒ Object
-
#ttl_remaining_from_tokens(tokens) ⇒ Object
Returns seconds of TTL remaining; -1 when the item has no expiry.
- #value_from_tokens(tokens, flag) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(io_source, value_marshaller) ⇒ ResponseProcessor
Returns a new instance of ResponseProcessor.
36 37 38 39 |
# File 'lib/dalli/protocol/response_processor.rb', line 36 def initialize(io_source, value_marshaller) @io_source = io_source @value_marshaller = value_marshaller end |
Instance Method Details
#bitflags_from_tokens(tokens) ⇒ Object
238 239 240 |
# File 'lib/dalli/protocol/response_processor.rb', line 238 def bitflags_from_tokens(tokens) value_from_tokens(tokens, 'f').to_i end |
#body_len_from_tokens(tokens) ⇒ Object
284 285 286 |
# File 'lib/dalli/protocol/response_processor.rb', line 284 def body_len_from_tokens(tokens) value_from_tokens(tokens, 's').to_i end |
#build_metadata_result(tokens) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/dalli/protocol/response_processor.rb', line 86 def (tokens) { value: nil, cas: cas_from_tokens(tokens), won_recache: tokens.include?('W'), stale: tokens.include?('X'), lost_recache: tokens.include?('Z'), # Explicit miss marker: EN means the key does not exist. A tombstoned # item is NOT a miss -- it answers VA/HD with the X flag set -- and a # stored nil under cache_nils is not one either, so neither can be # inferred from value or cas alone. miss: tokens.first == EN } end |
#cas_from_tokens(tokens) ⇒ Object
242 243 244 |
# File 'lib/dalli/protocol/response_processor.rb', line 242 def cas_from_tokens(tokens) value_from_tokens(tokens, 'c').to_i end |
#consume_all_responses_until_mn ⇒ Object
160 161 162 163 164 165 |
# File 'lib/dalli/protocol/response_processor.rb', line 160 def consume_all_responses_until_mn tokens = next_line_to_tokens tokens = next_line_to_tokens while tokens.first != MN true end |
#decr_incr ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/dalli/protocol/response_processor.rb', line 125 def decr_incr tokens = error_on_unexpected!(T_VA_NF_NS_EX) return false if [NS, EX].include?(tokens.first) return nil if tokens.first == NF read_line.to_i end |
#error_on_unexpected!(expected_codes) ⇒ Object
228 229 230 231 232 233 234 235 236 |
# File 'lib/dalli/protocol/response_processor.rb', line 228 def error_on_unexpected!(expected_codes) tokens = next_line_to_tokens return tokens if expected_codes.include?(tokens.first) raise Dalli::ServerError, tokens.join(' ').to_s if tokens.first == SERVER_ERROR raise Dalli::DalliError, "Response error: #{tokens.first}" end |
#flush ⇒ Object
143 144 145 146 147 |
# File 'lib/dalli/protocol/response_processor.rb', line 143 def flush error_on_unexpected!(T_OK) true end |
#full_response_from_buffer(tokens, body, resp_size) ⇒ Object
184 185 186 187 |
# File 'lib/dalli/protocol/response_processor.rb', line 184 def full_response_from_buffer(tokens, body, resp_size) value = @value_marshaller.retrieve(body, bitflags_from_tokens(tokens)) [tokens.first == VA, cas_from_tokens(tokens), key_from_tokens(tokens), value, resp_size] end |
#getk_response_from_buffer(buf, offset = 0) ⇒ Object
This method returns an array of values used in a pipelined getk process. The first value is the number of bytes by which to advance the pointer in the buffer. If the complete response is found in the buffer, this will be the response size. Otherwise it is zero.
The remaining three values in the array are the ResponseHeader, key, and value.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/dalli/protocol/response_processor.rb', line 199 def getk_response_from_buffer(buf, offset = 0) # Find the header terminator starting from offset term_idx = buf.byteindex(TERMINATOR, offset) return [0] unless term_idx header = buf.byteslice(offset, term_idx - offset) tokens = header.split header_len = header.bytesize + TERMINATOR.length # The body len is removed from the tokens array body_len = body_len_from_tokens(tokens) # We have a complete response that has no body. # This is either the response to the terminating # noop or, if the status is not MN, an intermediate # error response that needs to be discarded. return [true, header_len] if body_len.zero? resp_size = header_len + body_len + TERMINATOR.length # The header is in the buffer, but the body is not. As we don't have # a complete response, don't advance the buffer return [0] unless buf.bytesize >= offset + resp_size # The full response is in our buffer, so parse it and return # the values body = buf.byteslice(offset + header_len, body_len) full_response_from_buffer(tokens, body, resp_size) end |
#hit_status_from_tokens(tokens) ⇒ Object
Returns true if item was previously hit, false if first access, nil if not requested The h flag returns h0 (first access) or h1 (previously accessed)
265 266 267 268 269 270 |
# File 'lib/dalli/protocol/response_processor.rb', line 265 def hit_status_from_tokens(tokens) hit_token = tokens.find { |t| t.start_with?('h') && t.length == 2 } return nil unless hit_token hit_token[1] == '1' end |
#key_from_tokens(tokens) ⇒ Object
254 255 256 257 258 259 260 261 |
# File 'lib/dalli/protocol/response_processor.rb', line 254 def key_from_tokens(tokens) encoded_key = value_from_tokens(tokens, 'k') if tokens.delete('b') KeyRegularizer.decode(encoded_key) else encoded_key end end |
#last_access_from_tokens(tokens) ⇒ Object
Returns seconds since last access, or nil if not requested
The l flag returns l
274 275 276 |
# File 'lib/dalli/protocol/response_processor.rb', line 274 def last_access_from_tokens(tokens) value_from_tokens(tokens, 'l').to_i end |
#meta_delete ⇒ Object
120 121 122 123 |
# File 'lib/dalli/protocol/response_processor.rb', line 120 def tokens = error_on_unexpected!(T_HD_NF_EX) tokens.first == HD end |
#meta_get_with_metadata(cache_nils: false, return_hit_status: false, return_last_access: false, return_ttl_remaining: false) ⇒ Object
Returns a hash with all requested metadata:
- :value - the cached value (or nil if miss)
- :cas - the CAS value (if return_cas was requested)
- :won_recache - true if client won the right to recache (W flag)
- :stale - true if the item is stale (X flag)
- :lost_recache - true if another client is already recaching (Z flag)
- :hit_before - true/false if item was previously accessed (h flag, if requested)
- :last_access - seconds since last access (l flag, if requested)
Used by meta_get for comprehensive metadata retrieval. Supports thundering herd protection (N/R flags) and metadata flags (h/l/u).
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dalli/protocol/response_processor.rb', line 75 def (cache_nils: false, return_hit_status: false, return_last_access: false, return_ttl_remaining: false) tokens = error_on_unexpected!(T_VA_EN_HD) result = (tokens) result[:hit_before] = hit_status_from_tokens(tokens) if return_hit_status result[:last_access] = last_access_from_tokens(tokens) if return_last_access result[:ttl_remaining] = ttl_remaining_from_tokens(tokens) if return_ttl_remaining result[:value] = parse_value_from_tokens(tokens, cache_nils) result end |
#meta_get_with_value(cache_nils: false) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/dalli/protocol/response_processor.rb', line 41 def (cache_nils: false) tokens = error_on_unexpected!(T_VA_EN_HD) return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN return true unless tokens.first == VA @value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens)) end |
#meta_get_with_value_and_cas ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/dalli/protocol/response_processor.rb', line 49 def tokens = error_on_unexpected!(T_VA_EN_HD) return [nil, 0] if tokens.first == EN cas = cas_from_tokens(tokens) return [nil, cas] unless tokens.first == VA [@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens)), cas] end |
#meta_get_without_value ⇒ Object
59 60 61 62 |
# File 'lib/dalli/protocol/response_processor.rb', line 59 def tokens = error_on_unexpected!(T_EN_HD) tokens.first == EN ? nil : true end |
#meta_set_append_prepend ⇒ Object
113 114 115 116 117 118 |
# File 'lib/dalli/protocol/response_processor.rb', line 113 def tokens = error_on_unexpected!(T_HD_NS_NF_EX) return false unless tokens.first == HD true end |
#meta_set_with_cas ⇒ Object
106 107 108 109 110 111 |
# File 'lib/dalli/protocol/response_processor.rb', line 106 def tokens = error_on_unexpected!(T_HD_NS_NF_EX) return false unless tokens.first == HD cas_from_tokens(tokens) end |
#next_line_to_tokens ⇒ Object
303 304 305 306 |
# File 'lib/dalli/protocol/response_processor.rb', line 303 def next_line_to_tokens line = read_line line&.split || [] end |
#parse_value_from_tokens(tokens, cache_nils) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/dalli/protocol/response_processor.rb', line 99 def parse_value_from_tokens(tokens, cache_nils) return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN return unless tokens.first == VA @value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens)) end |
#pipelined_delete_non_deletions ⇒ Object
Consumes the responses to a batch of quiet (pipelined) delete requests, which are terminated by a noop (MN). In quiet mode memcached suppresses the success response for each deleted key, so every line received before the terminator corresponds to a key that was NOT deleted -- a miss (NF) or an error. Returns that count so callers can derive the number of successful deletes as (keys_sent - non_deletions).
174 175 176 177 178 179 180 181 182 |
# File 'lib/dalli/protocol/response_processor.rb', line 174 def pipelined_delete_non_deletions non_deletions = 0 tokens = next_line_to_tokens until tokens.first == MN non_deletions += 1 tokens = next_line_to_tokens end non_deletions end |
#read_data(data_size) ⇒ Object
308 309 310 |
# File 'lib/dalli/protocol/response_processor.rb', line 308 def read_data(data_size) @io_source.read(data_size + TERMINATOR.bytesize)&.chomp!(TERMINATOR) end |
#read_line ⇒ Object
299 300 301 |
# File 'lib/dalli/protocol/response_processor.rb', line 299 def read_line @io_source.read_line&.chomp!(TERMINATOR) end |
#reset ⇒ Object
149 150 151 152 153 |
# File 'lib/dalli/protocol/response_processor.rb', line 149 def reset error_on_unexpected!(T_RESET) true end |
#stale_from_tokens(tokens) ⇒ Object
Detects the X presence flag, set when an item has been marked stale by a
prior md key I. Uses strict equality (Array#any? with a String pattern
compares with ==) so a future value-bearing flag beginning with X cannot
be mistaken for it.
250 251 252 |
# File 'lib/dalli/protocol/response_processor.rb', line 250 def stale_from_tokens(tokens) tokens.any?('X') end |
#stats ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/dalli/protocol/response_processor.rb', line 133 def stats tokens = error_on_unexpected!(T_END_TOKEN_STAT) values = {} while tokens.first != END_TOKEN values[tokens[1]] = tokens[2] tokens = next_line_to_tokens end values end |
#ttl_remaining_from_tokens(tokens) ⇒ Object
Returns seconds of TTL remaining; -1 when the item has no expiry.
The t flag returns t
280 281 282 |
# File 'lib/dalli/protocol/response_processor.rb', line 280 def ttl_remaining_from_tokens(tokens) value_from_tokens(tokens, 't').to_i end |
#value_from_tokens(tokens, flag) ⇒ Object
288 289 290 291 292 293 294 295 296 297 |
# File 'lib/dalli/protocol/response_processor.rb', line 288 def value_from_tokens(tokens, flag) # NB: as an optimization, we're mutating the matching token in place # so there is a baked assumption that we're only accessing each token at most once. index = tokens.find_index { |t| t.start_with?(flag) } if index tokens.delete_at(index).delete_prefix!(flag) else 0 end end |
#version ⇒ Object
155 156 157 158 |
# File 'lib/dalli/protocol/response_processor.rb', line 155 def version tokens = error_on_unexpected!(T_VERSION) tokens.last end |