Class: Terminalwire::V2::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/terminalwire/v2/window.rb

Overview

The flow-control credit rule, as a pure ledger — no threads, no I/O. This is the protocol part of flow control: how much output may be in flight, and how window_adjust extends it. The blocking behaviour when credit runs out is an implementation concern layered on top (see Server::FlowController); the rule itself is deterministic and identical across implementations, so it is exercised by the language-neutral flow corpus in ../../conformance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Window

Returns a new instance of Window.



13
14
15
# File 'lib/terminalwire/v2/window.rb', line 13

def initialize(size)
  @available = size > Protocol::MAX_WINDOW ? Protocol::MAX_WINDOW : size
end

Instance Attribute Details

#availableObject (readonly)

Returns the value of attribute available.



11
12
13
# File 'lib/terminalwire/v2/window.rb', line 11

def available
  @available
end

Instance Method Details

#grant(bytes) ⇒ Object

Extend the window (a window_adjust arrived), clamped to the protocol ceiling (Protocol::MAX_WINDOW) so a peer can't grow the window without bound.



28
29
30
31
# File 'lib/terminalwire/v2/window.rb', line 28

def grant(bytes)
  @available += bytes
  @available = Protocol::MAX_WINDOW if @available > Protocol::MAX_WINDOW
end

#take(want) ⇒ Object

The number of bytes that may be sent right now toward a request for want: min(want, available). Decrements the window by that amount and returns it.



19
20
21
22
23
24
# File 'lib/terminalwire/v2/window.rb', line 19

def take(want)
  amount = want < @available ? want : @available
  amount = 0 if amount.negative?
  @available -= amount
  amount
end