Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor @writing logic

This fixes cases where a lot of data is being sent; previously all write
attempts would bump @retries even if we're already @writing and should skip
the rest of the body. Now we only call send_data_buffered if we're not already
@writing.

Change-Id: I68fbccdec2ab3d51994b7278aba2eb7aeb8e4c43
  • Loading branch information...
commit bd041817abb25a2f46f099a49fecbe03b1b57c84 1 parent b266ab6
@vito vito authored
View
18 lib/caldecott/client/http_tunnel.rb
@@ -189,7 +189,7 @@ def initialize(log, uri, conn, auth_token)
def send_data(data)
@write_buffer << data
- send_data_buffered
+ send_data_buffered unless @writing
end
def close
@@ -197,15 +197,17 @@ def close
end
def send_data_buffered
+ return if @closing
+
if (@retries += 1) > MAX_RETRIES
@conn.trigger_on_close
return
end
- return if @closing
- data, @write_buffer = @write_buffer, "" unless @writing
-
@writing = true
+
+ data, @write_buffer = @write_buffer, ""
+
uri = "#{@uri}/#{@seq}"
@log.debug "put #{uri}"
req = EM::HttpRequest.new(uri).put :body => data, :head => { "Auth-Token" => @auth_token }
@@ -219,10 +221,14 @@ def send_data_buffered
@log.debug "put #{uri} #{req.response_header.status}"
case req.response_header.status
when 200, 202, 204
- @writing = false
@seq += 1
@retries = 0
- send_data_buffered unless @write_buffer.empty?
+
+ if @write_buffer.empty?
+ @writing = false
+ else
+ send_data_buffered
+ end
when 404
@conn.trigger_on_close
else
View
35 spec/client/http_tunnel_spec.rb
@@ -244,5 +244,40 @@
end
end
end
+
+ it 'should not consume retries when already writing' do
+ with_em_timeout do
+ writer = Caldecott::Client::HttpTunnel::Writer.new(@log, @uri, @conn, @auth_token)
+
+ spam = Caldecott::Client::HttpTunnel::MAX_RETRIES * 10
+
+ got = ""
+ finished = {}
+ spam.times do |i|
+ stub_request(:put, "#{@uri}/#{i + 1}").with do |request|
+ # for some reason this callback gets executed twice,
+ # even though only one request is sent
+ unless finished[i]
+ finished[i] = true
+ got << request.body
+ end
+
+ true
+ end
+ end
+
+ expected = ""
+ spam.times do |i|
+ EM.next_tick do
+ expected << "data #{i}\n"
+ writer.send_data "data #{i}\n"
+ end
+ end
+
+ @validate = lambda do
+ got.should == expected
+ end
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.