Class: Raptor::Http2::Writer

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

Overview

Lock-free per-connection frame writer.

Serializes concurrent socket writes from multiple stream workers without blocking any of them.

Constant Summary collapse

IDLE =

Returns:

  • (::Symbol)
:idle

Instance Method Summary collapse

Constructor Details

#initializeWriter

Creates a new Writer.



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

def initialize
  @state = Atom.new(IDLE)
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



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
74
# File 'lib/raptor/http2.rb', line 45

def write_frames(socket, frames)
  return if frames.nil? || 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|
      socket.write(frame) rescue nil
    end
  end
end