class Builder

Builds a queue policy using the configuration DSL.

Definitions

def initialize(name)

Signature

parameter name Symbol | String

The stable queue name.

Implementation

def initialize(name)
	@name = name
	@matcher = nil
	@eligibility = nil
	@admission = nil
	@balance_policy = Balance::Spread.new
	@depth_limit = nil
	@wait_limit = nil
	@shed_status = 429
	@shed_headers = {}
end

def match(&block)

Set the request classifier for this queue.

Signature

yields {|request| ...}

Whether a request belongs to this queue.

Implementation

def match(&block)
	raise ArgumentError, "A matcher block is required!" unless block
	@matcher = block
end

def eligible(&block)

Restrict the backends which may serve this queue.

Signature

yields {|backend, request| ...}

Whether the backend is eligible.

Implementation

def eligible(&block)
	raise ArgumentError, "An eligibility block is required!" unless block
	@eligibility = block
end

def admit(policy = nil, &block)

Set an application admission policy.

Signature

parameter policy #admit? | #call | Nil

The admission policy object.

yields {|request, queue:, pending:| ...}

Whether the request can wait.

Implementation

def admit(policy = nil, &block)
	@admission = policy || block
	raise ArgumentError, "An admission policy is required!" unless @admission
end

def balance(policy, **options)

Set the soft backend balance policy.

Signature

parameter policy Symbol | #select

A built-in name or application policy.

parameter options Hash

Options for a built-in policy.

Implementation

def balance(policy, **options)
	@balance_policy = Balance.coerce(policy, **options)
end

def depth_limit(value)

Set the maximum number of requests waiting in this queue.

Signature

parameter value Integer

The maximum queue depth.

Implementation

def depth_limit(value)
	value = Integer(value)
	raise ArgumentError, "Depth limit must not be negative!" if value.negative?
	@depth_limit = value
end

def wait_limit(value)

Set the maximum time a request may wait for a permit.

Signature

parameter value Numeric

The maximum wait in seconds.

Implementation

def wait_limit(value)
	value = Float(value)
	raise ArgumentError, "Wait limit must be positive!" unless value.positive?
	@wait_limit = value
end

def shed(status: 429, retry_after: nil, headers: {})

Configure the response used when admission is rejected.

Signature

parameter status Integer

The HTTP response status.

parameter retry_after Numeric | String | Nil

An optional Retry-After value.

parameter headers Hash

Additional response headers.

Implementation

def shed(status: 429, retry_after: nil, headers: {})
	@shed_status = Integer(status)
	@shed_headers = headers.transform_keys(&:to_s)
	@shed_headers["retry-after"] = retry_after.to_s if retry_after
end

def build

Build the immutable queue policy.

Signature

returns Queue

The configured queue.

Implementation

def build
	Queue.new(
		@name,
		matcher: @matcher,
		eligibility: @eligibility,
		admission: @admission,
		balance_policy: @balance_policy,
		depth_limit: @depth_limit,
		wait_limit: @wait_limit,
		shed_status: @shed_status,
		shed_headers: @shed_headers.dup.freeze,
	).freeze
end