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
config.middleware.tap do |middleware| | |
killer = PassengerKiller.new | |
# Max requests per worker | |
middleware.insert_before( | |
Rack::Sendfile, | |
Middleware, limiter_klass: RequestLimiter, killer: killer, min: 4096, max: 5120) | |
# Max memory size (RSS) per worker | |
middleware.insert_before( |
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 DelayedJobPlugin | |
attr_reader :limiter, :dj | |
def initialize(limiter_klass:, **opts) | |
@limiter = limiter_klass.new(opts) | |
end | |
# Delayed::Worker вызовет new без агрументов у каждого плагина |
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 |
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
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
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
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
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
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
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 |