Skip to content

Commit

Permalink
Raise CSOT error
Browse files Browse the repository at this point in the history
  • Loading branch information
comandeo-mongo committed Mar 4, 2024
1 parent 9d988c2 commit 1f13d90
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/mongo/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,18 @@ def read_with_timeout(length, timeout)
map_exceptions do
socket_timeout = deadline - Utils.monotonic_time
if socket_timeout <= 0
raise Errno::ETIMEDOUT, "Took more than #{_timeout} seconds to receive data"
raise Mongo::Error::TimeoutError
end
data = read_from_socket(length, socket_timeout: socket_timeout)
data = read_from_socket(length, socket_timeout: socket_timeout, csot: true)
unless (data.length > 0 || length == 0)
raise IOError, "Expected to read > 0 bytes but read 0 bytes"
end
while data.length < length
socket_timeout = deadline - Utils.monotonic_time
if socket_timeout <= 0
raise Errno::ETIMEDOUT, "Took more than #{_timeout} seconds to receive data"
raise Mongo::Error::TimeoutError
end
chunk = read_from_socket(length - data.length, socket_timeout: socket_timeout)
chunk = read_from_socket(length - data.length, socket_timeout: socket_timeout, csot: true)
unless (chunk.length > 0 || length == 0)
raise IOError, "Expected to read > 0 bytes but read 0 bytes"
end
Expand All @@ -305,7 +305,7 @@ def read_without_timeout(length, socket_timeout = nil)
end
end

def read_from_socket(length, socket_timeout: nil)
def read_from_socket(length, socket_timeout: nil, csot: false)
# Just in case
if length == 0
return ''.force_encoding('BINARY')
Expand All @@ -316,7 +316,7 @@ def read_from_socket(length, socket_timeout: nil)
if _timeout > 0
deadline = Utils.monotonic_time + _timeout
elsif _timeout < 0
raise Errno::ETIMEDOUT, "Negative timeout #{_timeout} given to socket"
raise_timeout_error!("Negative timeout #{_timeout} given to socket", csot)
end
end

Expand Down Expand Up @@ -371,7 +371,7 @@ def read_from_socket(length, socket_timeout: nil)
if deadline
select_timeout = deadline - Utils.monotonic_time
if select_timeout <= 0
raise Errno::ETIMEDOUT, "Took more than #{_timeout} seconds to receive data"
raise_timeout_error!("Took more than #{_timeout} seconds to receive data", csot)
end
end
pipe = options[:pipe]
Expand Down Expand Up @@ -413,11 +413,11 @@ def read_from_socket(length, socket_timeout: nil)
if deadline
select_timeout = deadline - Utils.monotonic_time
if select_timeout <= 0
raise Errno::ETIMEDOUT, "Took more than #{_timeout} seconds to receive data"
raise_timeout_error!("Took more than #{_timeout} seconds to receive data", csot)
end
end
elsif rv.nil?
raise Errno::ETIMEDOUT, "Took more than #{_timeout} seconds to receive data (select call timed out)"
raise_timeout_error!("Took more than #{_timeout} seconds to receive data (select call timed out)", csot)
end
retry
end
Expand Down Expand Up @@ -478,7 +478,7 @@ def write_without_timeout(*args)

def write_with_timeout(*args, timeout:)
raise ArgumentError, 'timeout cannot be nil' if timeout.nil?
raise Errno::ETIMEDOUT, "Negative timeout #{timeout} given to socket" if timeout < 0
raise_timeout_error!("Negative timeout #{timeout} given to socket", true) if timeout < 0

written = 0
args.each do |buf|
Expand Down Expand Up @@ -510,11 +510,11 @@ def write_chunk(chunk, timeout)
if deadline
select_timeout = deadline - Utils.monotonic_time
if select_timeout <= 0
raise Errno::ETIMEDOUT, "Took more than #{timeout} seconds to receive data"
raise_timeout_error!("Took more than #{timeout} seconds to receive data", true)
end
end
elsif rv.nil?
raise Errno::ETIMEDOUT, "Took more than #{timeout} seconds to receive data (select call timed out)"
raise_timeout_error!("Took more than #{timeout} seconds to receive data (select call timed out)", true)
end
retry
end
Expand Down Expand Up @@ -575,5 +575,13 @@ def map_exceptions
def human_address
raise NotImplementedError
end

def raise_timeout_error!(message = nil, csot = false)
if csot
raise Mongo::Error::TimeoutError
else
raise Errno::ETIMEDOUT, message
end
end
end
end

0 comments on commit 1f13d90

Please sign in to comment.