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

#initialize(write_timeout:) ⇒ Writer

Creates a new Writer.

Parameters:



38
39
40
41
# File 'lib/raptor/http2.rb', line 38

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/raptor/http2.rb', line 51

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|
      Http.socket_write(socket, frame, timeout: @write_timeout) rescue nil
    end
  end
end