Class: Iron::Content::DownloadBudget

Inherits:
Object
  • Object
show all
Defined in:
app/models/iron/content/download_budget.rb

Overview

A single write request shares one download budget: a cap on how many _file directives it may ingest and a wall-clock deadline across all of its downloads. Per-read timeouts alone would let a hostile server drip a byte per interval and hold a worker indefinitely.

Constant Summary collapse

MAX_FILES =
25
MAX_SECONDS =
60

Instance Method Summary collapse

Constructor Details

#initialize(files: MAX_FILES, seconds: MAX_SECONDS) ⇒ DownloadBudget

Returns a new instance of DownloadBudget.



11
12
13
14
# File 'app/models/iron/content/download_budget.rb', line 11

def initialize(files: MAX_FILES, seconds: MAX_SECONDS)
  @files = files
  @deadline = now + seconds
end

Instance Method Details

#charge_file!Object

Raises:



16
17
18
19
# File 'app/models/iron/content/download_budget.rb', line 16

def charge_file!
  @files -= 1
  raise Error, "too many _file directives in one request (max #{MAX_FILES})" if @files.negative?
end

#verify_deadline!(url) ⇒ Object

Raises:



21
22
23
# File 'app/models/iron/content/download_budget.rb', line 21

def verify_deadline!(url)
  raise Error, "could not download #{url}: download deadline exceeded" if now > @deadline
end