Skip to content

Instantly share code, notes, and snippets.

@HoneyryderChuck
HoneyryderChuck / enumerable_reader.rb
Created January 18, 2019 13:30
Enumerable to reader draft
module Enumerable
class Reader
def initialize(enumerable)
@enumerable = enumerable
@buffer = "".b
end
def read(nsize, buffer = nil)
@iterator ||= @enumerable.enum_for(:each)
(buffer ||= @buffer).clear
@HoneyryderChuck
HoneyryderChuck / testframes.txt
Created October 22, 2017 17:46
server frames using internal client
This file has been truncated, but you can view the full file.
# client
> ruby test.rb 2>&1 | grep server | grep 'window_update\|data' > testframes.txt
# server output, 143243 lines
server (HTTP/2): {:type=>:data, :flags=>[], :payload=>40, :stream=>1}
server (HTTP/2): {:type=>:data, :flags=>[], :payload=>20, :stream=>1}
server (HTTP/2): {:type=>:data, :flags=>[], :payload=>312, :stream=>1}
server (HTTP/2): {:type=>:data, :flags=>[], :payload=>58, :stream=>1}
server (HTTP/2): {:type=>:data, :flags=>[], :payload=>22, :stream=>1}
server (HTTP/2): {:type=>:data, :flags=>[], :payload=>83, :stream=>1}
@HoneyryderChuck
HoneyryderChuck / nghttpframes.txt
Last active October 22, 2017 17:47
Server frames when using nghttp
Command:
# server
> bundle exec ruby test_script.rb 2>&1 | grep server | grep 'window_update\|data' > nghttpframes.txt
# client
> nghttp -vu http://#{host}:${port} | grep 'WINDOW_UPDATE\|DATA' | wc -l
7965
# server output, 7872
@HoneyryderChuck
HoneyryderChuck / ext_flatten.rb
Created August 1, 2016 13:07
alternative flatten
def flatten(a, elems=[])
a.each do |e|
case e
when Array then flatten(e, elems)
else elems << e
end
end
el
end
@HoneyryderChuck
HoneyryderChuck / chat.rb
Last active January 16, 2023 07:28
Simple Chat Application, proof of concept for hybrid of thread-server http with evented-server SSE.
# chat.rb
require 'sinatra/base'
# this also loads celluloid io, let's keep that in mind
require 'celluloid/current'
require 'reel'
# The chat server, an IO Event Loop held by the actor
# Collects connections (Reel Event Streams)
#
# Contrary to EventMachine, there is no event callback for
module SEM
module Celluloid
module IOExtensions
NEARZERO = 0.00001
def self.wait(t = nil)
if t
time = t.zero? ? NEARZERO : t
Celluloid.timeout(time) { yield }
else
yield
@HoneyryderChuck
HoneyryderChuck / celluloid_io_mysql2_patch.rb
Last active June 5, 2016 08:53
mysql patch to make it work with celluloid IO
require 'celluloid/io'
require 'mysql2'
module Mysql2
module Celluloid
# https://github.com/brianmario/mysql2/blob/master/lib/mysql2/em.rb
# Almost direct copy of the eventmachine driver for mysql2. It's even less verbose, which can only be a plus.
class Client < ::Mysql2::Client
def query(sql, opts={})
if ::Celluloid::IO.evented?
require 'celluloid/io'
require 'celluloid/autostart'
class Server
include Celluloid::IO
finalizer :shutdown
def initialize(host, port)
puts "*** Starting server"
@server = TCPServer.new(host, port)
async.run