Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reduce code duplication for reading headers

  • Loading branch information...
commit 152c24c29885cd55bcd7729b13bdca0b5bb7a817 1 parent d4dfa87
@koraktor koraktor authored
Showing with 14 additions and 21 deletions.
  1. +14 −21 lib/dalli/server.rb
View
35 lib/dalli/server.rb
@@ -429,9 +429,7 @@ def deserialize(value, flags)
end
def data_cas_response
- header = read(24)
- raise Dalli::NetworkError, 'No response' if !header
- (extras, _, status, count, _, cas) = header.unpack(CAS_HEADER)
+ (extras, _, status, count, _, cas) = read_header.unpack(CAS_HEADER)
data = read(count) if count > 0
if status == 1
nil
@@ -459,9 +457,7 @@ def guard_max_value(key, value)
end
def generic_response(unpack=false)
- header = read(24)
- raise Dalli::NetworkError, 'No response' if !header
- (extras, _, status, count) = header.unpack(NORMAL_HEADER)
+ (extras, _, status, count) = read_header.unpack(NORMAL_HEADER)
data = read(count) if count > 0
if status == 1
nil
@@ -479,9 +475,7 @@ def generic_response(unpack=false)
end
def cas_response
- header = read(24)
- raise Dalli::NetworkError, 'No response' if !header
- (_, _, status, count, _, cas) = header.unpack(CAS_HEADER)
+ (_, _, status, count, _, cas) = read_header.unpack(CAS_HEADER)
read(count) if count > 0 # this is potential data that we don't care about
if status == 1
nil
@@ -497,9 +491,7 @@ def cas_response
def keyvalue_response
hash = {}
loop do
- header = read(24)
- raise Dalli::NetworkError, 'No response' if !header
- (key_length, _, body_length, _) = header.unpack(KV_HEADER)
+ (key_length, _, body_length, _) = read_header.unpack(KV_HEADER)
return hash if key_length == 0
key = read(key_length)
value = read(body_length - key_length) if body_length - key_length > 0
@@ -510,9 +502,7 @@ def keyvalue_response
def multi_response
hash = {}
loop do
- header = read(24)
- raise Dalli::NetworkError, 'No response' if !header
- (key_length, _, body_length, _) = header.unpack(KV_HEADER)
+ (key_length, _, body_length, _) = read_header.unpack(KV_HEADER)
return hash if key_length == 0
flags = read(4).unpack('N')[0]
key = read(key_length)
@@ -543,6 +533,12 @@ def read(count)
end
end
+ def read_header
+ header = read(24)
+ raise Dalli::NetworkError, 'No response' if !header
+ header
+ end
+
def connect
Dalli.logger.debug { "Dalli::Server#connect #{hostname}:#{port}" }
@@ -656,9 +652,8 @@ def sasl_authentication
# negotiate
req = [REQUEST, OPCODES[:auth_negotiation], 0, 0, 0, 0, 0, 0, 0].pack(FORMAT[:noop])
write(req)
- header = read(24)
- raise Dalli::NetworkError, 'No response' if !header
- (extras, type, status, count) = header.unpack(NORMAL_HEADER)
+
+ (extras, type, status, count) = read_header.unpack(NORMAL_HEADER)
raise Dalli::NetworkError, "Unexpected message format: #{extras} #{count}" unless extras == 0 && count > 0
content = read(count)
return (Dalli.logger.debug("Authentication not required/supported by server")) if status == 0x81
@@ -671,9 +666,7 @@ def sasl_authentication
req = [REQUEST, OPCODES[:auth_request], mechanism.bytesize, 0, 0, 0, mechanism.bytesize + msg.bytesize, 0, 0, mechanism, msg].pack(FORMAT[:auth_request])
write(req)
- header = read(24)
- raise Dalli::NetworkError, 'No response' if !header
- (extras, type, status, count) = header.unpack(NORMAL_HEADER)
+ (extras, type, status, count) = read_header.unpack(NORMAL_HEADER)
raise Dalli::NetworkError, "Unexpected message format: #{extras} #{count}" unless extras == 0 && count > 0
content = read(count)
return Dalli.logger.info("Dalli/SASL: #{content}") if status == 0
Please sign in to comment.
Something went wrong with that request. Please try again.