Skip to content

Commit b427ed1

Browse files
committed
Write with a timeout non-blocking
1 parent d1a7f21 commit b427ed1

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

lib/redis/connection/ruby.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,34 @@ def _read_from_socket(nbytes)
8080
raise Errno::ECONNRESET
8181
end
8282

83-
# UNIXSocket and TCPSocket don't support write timeouts
84-
def write(*args)
85-
Timeout.timeout(@write_timeout, TimeoutError) { super }
83+
def _write_to_socket(data)
84+
begin
85+
write_nonblock(data)
86+
87+
rescue *NBIO_EXCEPTIONS
88+
if IO.select([self], nil, nil, @write_timeout)
89+
retry
90+
else
91+
raise Redis::TimeoutError
92+
end
93+
end
94+
95+
rescue EOFError
96+
raise Errno::ECONNRESET
97+
end
98+
99+
def write(data)
100+
return super(data) unless @write_timeout
101+
102+
length = data.bytesize
103+
total_count = 0
104+
loop do
105+
count = _write_to_socket(data)
106+
107+
total_count += count
108+
return total_count if total_count >= length
109+
data = data.byteslice(count..-1)
110+
end
86111
end
87112
end
88113

0 commit comments

Comments
 (0)