Class: Iron::Content::DownloadBudget
- Inherits:
-
Object
- Object
- Iron::Content::DownloadBudget
- 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
- #charge_file! ⇒ Object
-
#initialize(files: MAX_FILES, seconds: MAX_SECONDS) ⇒ DownloadBudget
constructor
A new instance of DownloadBudget.
- #verify_deadline!(url) ⇒ Object
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
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
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 |