Skip to content

Commit

Permalink
Add mutexes to client examples to avoid race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidpieper committed Jan 13, 2021
1 parent 8ec79e8 commit 7dade60
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 6 additions & 3 deletions example/client.rb
Expand Up @@ -42,10 +42,13 @@
stream = conn.new_stream
log = Logger.new(stream.id)

conn_mutex = Mutex.new # Synchronize writing to socket
conn.on(:frame) do |bytes|
# puts "Sending bytes: #{bytes.unpack("H*").first}"
sock.print bytes
sock.flush
conn_mutex.synchronize { # Make sure that only one frame is sent at a time
# puts "Sending bytes: #{bytes.unpack("H*").first}"
sock.print bytes
sock.flush
}
end
conn.on(:frame_sent) do |frame|
puts "Sent frame: #{frame.inspect}"
Expand Down
7 changes: 5 additions & 2 deletions example/upgrade_client.rb
Expand Up @@ -21,9 +21,12 @@ def request_header_hash
end
end

conn_mutex = Mutex.new # Synchronize writing to socket
conn.on(:frame) do |bytes|
sock.print bytes
sock.flush
conn_mutex.synchronize { # Make sure that only one frame is sent at a time
sock.print bytes
sock.flush
}
end
conn.on(:frame_sent) do |frame|
puts "Sent frame: #{frame.inspect}"
Expand Down

0 comments on commit 7dade60

Please sign in to comment.