Skip to content

Instantly share code, notes, and snippets.

@kinnalru
kinnalru / http4.rb
Created February 25, 2021 08:22
StartWebrickDocker
class StartWebrickDocker < Base
def start
cmd = "
require \"webrick\"
require \"logger\"
WEBrick::HTTPServer.new(Host: \"0.0.0.0\", Port: 80, MaxClients: 3000, ShutdownSocketWithoutClose: true, Logger: Logger.new(IO::NULL), AccessLog: []).start"
exec "docker run --rm -p #{@port}:80 --name http ruby:2.5-alpine /usr/local/bin/ruby -e '#{cmd}'"
end
def stop(_pid)
system("docker rm -f http &> /dev/null")
@kinnalru
kinnalru / http3.rb
Created February 25, 2021 08:21
StartWebrick
class StartWebrick < Base
def start
server = WEBrick::HTTPServer.new Host: '0.0.0.0', Port: @port, MaxClients: 3000, ShutdownSocketWithoutClose: true, Logger: Logger.new(IO::NULL), AccessLog: []
server.mount_proc('/') { |req, res| res.body = 'hello' }
server.start
end
def stop(pid)
Process.kill("KILL", pid)
end
end
@kinnalru
kinnalru / http2.rb
Created February 25, 2021 08:20
make_requests_single
def make_requests_single(uri, duration)
count = 0
t = Process.clock_gettime(Process::CLOCK_MONOTONIC)
Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.max_retries = 0
while (Process.clock_gettime(Process::CLOCK_MONOTONIC) - t) < duration do
data = "/#{count}"
http.get(uri + data).body
count += 1
end
@kinnalru
kinnalru / http1.rb
Created February 25, 2021 08:17
make_requests_each
def make_requests_each(uri, duration)
count = 0
t = Process.clock_gettime(Process::CLOCK_MONOTONIC)
while (Process.clock_gettime(Process::CLOCK_MONOTONIC) - t) < duration do
Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.max_retries = 0
data = "/#{count}"
http.get(uri + data).body
count += 1
end
require 'rufus-scheduler'
require 'lusnoc/mutex'
mutex = Lusnoc::Mutex.new('/locks/jobs', ttl: 20)
mutex.synchronize(timeout: 10) do |mx|
scheduler = Rufus::Scheduler.new.tap do |s|
mx.on_mutex_lost do
exit!(1)
require 'rufus-scheduler'
scheduler = Rufus::Scheduler.new.tap do |s|
s.in '1h' do
RefreshCurrencyJob.perform_later
end
end
scheduler.join
@kinnalru
kinnalru / lusnoc_3.rb
Created January 13, 2021 10:51
Lusnoc usage example
Lusnoc::Mutex.new('/some/key').synchronize do |mx|
mx.time_to_expiration # seconds to session expiration in consul.
mx.ttl # session ttl.
mx.need_renew? # false
sleep (mx.ttl / 2) + 1
mx.need_renew? # true when time_to_expiration less than half of ttl
mx.on_mutex_lost do |mutex|
# this callback will be called from other(guard) thread when mutex is lost(session invalidated)
@kinnalru
kinnalru / lusnoc_2.rb
Created January 13, 2021 10:48
Don't forget to manually renew session
Lusnoc::Mutex.new('/some/key').synchronize do |mx|
# do some work
mx.renew if mx.need_renew? # manualy renew session if needed
# do other work
mx.renew if mx.need_renew? # manualy renew session if needed
# ...
rescue Lusnoc::ExpiredError => e
# Session was invalidated and mutex was lost!
end
require 'lusnoc/mutex'
mutex = Lusnoc::Mutex.new('/locks/mx1', ttl: 20)
mutex.synchronize(timeout: 10) do |mx|
puts "We are exclusively owns resource"
end
Delayed::Worker.plugins.tap do |plugins|
plugins << DelayedJobPlugin.new(limiter_klass: RequestLimiter, min: 100, max:200)
plugins << DelayedJobPlugin.new(limiter_klass: MemoryLimiter, min: 300 * (1024**2), max: 400 * (1024**2))
end