This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rufus-scheduler' | |
scheduler = Rufus::Scheduler.new.tap do |s| | |
s.in '1h' do | |
RefreshCurrencyJob.perform_later | |
end | |
end | |
scheduler.join |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'lusnoc/mutex' | |
mutex = Lusnoc::Mutex.new('/locks/mx1', ttl: 20) | |
mutex.synchronize(timeout: 10) do |mx| | |
puts "We are exclusively owns resource" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |