Sha256: 8f89a873084634fd74efe3b710ef54bdccadefd26a9d6fa112a3ec894fa7dc58

Contents?: true

Size: 1.21 KB

Versions: 14

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require "protocol/http/body/wrapper"
require "async/variable"

module Async
	module HTTP
		module Protocol
			module HTTP1
				# Keeps track of whether a body is being read, and if so, waits for it to be closed.
				class Finishable < ::Protocol::HTTP::Body::Wrapper
					def initialize(body)
						super(body)
						
						@closed = Async::Variable.new
						@error = nil
						
						@reading = false
					end
					
					def reading?
						@reading
					end
					
					def read
						@reading = true
						
						super
					end
					
					def close(error = nil)
						super
						
						unless @closed.resolved?
							@error = error
							@closed.value = true
						end
					end
					
					def wait(persistent = true)
						if @reading
							@closed.wait
						elsif persistent
							# If the connection can be reused, let's gracefully discard the body:
							self.discard
						else
							# Else, we don't care about the body, so we can close it immediately:
							self.close
						end
					end
					
					def inspect
						"#<#{self.class} closed=#{@closed} error=#{@error}> | #{super}"
					end
				end
			end
		end
	end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
async-http-0.87.0 lib/async/http/protocol/http1/finishable.rb
async-http-0.86.0 lib/async/http/protocol/http1/finishable.rb
async-http-0.85.0 lib/async/http/protocol/http1/finishable.rb
async-http-0.84.0 lib/async/http/protocol/http1/finishable.rb
async-http-0.83.1 lib/async/http/protocol/http1/finishable.rb
async-http-0.83.0 lib/async/http/protocol/http1/finishable.rb
async-http-0.82.3 lib/async/http/protocol/http1/finishable.rb
async-http-0.82.2 lib/async/http/protocol/http1/finishable.rb
async-http-0.82.1 lib/async/http/protocol/http1/finishable.rb
async-http-0.82.0 lib/async/http/protocol/http1/finishable.rb
async-http-0.81.0 lib/async/http/protocol/http1/finishable.rb
async-http-0.80.1 lib/async/http/protocol/http1/finishable.rb
async-http-0.80.0 lib/async/http/protocol/http1/finishable.rb
async-http-0.79.0 lib/async/http/protocol/http1/finishable.rb