Class: Raptor::DetachedBody::Budget
- Inherits:
-
Object
- Object
- Raptor::DetachedBody::Budget
- Defined in:
- lib/raptor/detached_body.rb,
sig/generated/raptor/detached_body.rbs
Overview
Tracks buffered bytes shared by a group of detached bodies.
Instance Method Summary collapse
-
#initialize(max_size) ⇒ Budget
constructor
Creates a shared budget with the given byte limit.
-
#release(bytes) ⇒ void
Releases bytes previously charged to the shared limit.
-
#reserve(bytes) ⇒ Boolean
Reserves bytes when they fit within the shared limit.
Constructor Details
#initialize(max_size) ⇒ Budget
Creates a shared budget with the given byte limit.
25 26 27 28 |
# File 'lib/raptor/detached_body.rb', line 25 def initialize(max_size) @max_size = max_size @size = Atom.new(0) end |
Instance Method Details
#release(bytes) ⇒ void
This method returns an undefined value.
Releases bytes previously charged to the shared limit.
51 52 53 |
# File 'lib/raptor/detached_body.rb', line 51 def release(bytes) @size.swap { |size| size - bytes } end |
#reserve(bytes) ⇒ Boolean
Reserves bytes when they fit within the shared limit.
36 37 38 39 40 41 42 43 |
# File 'lib/raptor/detached_body.rb', line 36 def reserve(bytes) reserved = false @size.swap do |size| reserved = size + bytes <= @max_size reserved ? size + bytes : size end reserved end |