class Proxy

Routes HTTP requests through the registry's global admission queue.

Definitions

def initialize(registry)

Initialize an HTTP proxy.

Signature

parameter registry Registry

The backend registry.

Implementation

def initialize(registry)
	@registry = registry
end

def call(request)

Route a request to the next available backend.

Signature

parameter request Protocol::HTTP::Request

The downstream request.

returns Protocol::HTTP::Response

The upstream or generated response.

Implementation

def call(request)
	unless backend = @registry.acquire
		return Protocol::HTTP::Response[503, {"content-type" => "text/plain"}, ["No backends available.\n"]]
	end
	
	reservation = :processing
	upstream_request = build_request(request)
	response = backend.call(upstream_request)
	backend.processed
	reservation = :exchange
	
	if body = response.body
		response.body = ResponseBody.new(body){backend.release}
	else
		backend.release
	end
	reservation = nil
	
	return response
rescue => error
	case reservation
	when :processing
		backend.failed
	when :exchange
		backend.release
	end
	
	return Protocol::HTTP::Response[502, {"content-type" => "text/plain"}, ["Bad Gateway: #{error.class}\n"]]
end