Class: Raptor::Http2::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/raptor/http2.rb,
sig/generated/raptor/http2.rbs

Overview

Serialises concurrent frame writes on a single HTTP/2 connection so exactly one thread is writing at any moment.

Constant Summary collapse

IDLE =

Returns:

  • (::Symbol)
:idle

Instance Method Summary collapse

Constructor Details

#initialize(write_timeout:) ⇒ Writer

Creates a new Writer.

Parameters:



31
32
33
34
# File 'lib/raptor/http2.rb', line 31

def initialize(write_timeout:)
  @state = Atom.new(IDLE)
  @write_timeout = write_timeout
end

Instance Method Details

#write_frames(socket, frames) ⇒ void

This method returns an undefined value.

Writes frames to the socket, coordinating with concurrent writers so that exactly one thread is actively writing at any time.

Parameters:

  • socket (OpenSSL::SSL::SSLSocket)

    the connection socket

  • frames (Array<String>)

    frame bytes to write in order



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/raptor/http2.rb', line 44

def write_frames(socket, frames)
  return if !frames || frames.empty?

  claimed = false
  @state.swap do |current|
    if current.equal?(IDLE)
      claimed = true
      frames
    else
      claimed = false
      current + frames
    end
  end

  return unless claimed

  loop do
    pending = nil
    @state.swap do |current|
      pending = current
      current.empty? ? IDLE : []
    end

    break if pending.empty?

    pending.each do |frame|
      Http.socket_write(socket, frame, timeout: @write_timeout) rescue nil
    end
  end
end