Skip to content

Instantly share code, notes, and snippets.

@merqlove
Last active August 29, 2015 14:22
Show Gist options
  • Save merqlove/759c71615575a57108cb to your computer and use it in GitHub Desktop.
Save merqlove/759c71615575a57108cb to your computer and use it in GitHub Desktop.
Ruby objects per request

multithread_state/shared/set_subdomain.rb:

  • If i use Tenant.subdomain ||= env['SERVER_NAME'].split('.')[0], Tenant.subdomain will be the same till server restart.
  • So as i see i need to cleanup it or make singleton, which will be unique with every request.

Finally

multithread_state/shared/set_subdomain_singleton.rb:

  • Seems that i don't need to cleanup Tenant.subdomain after all. This data unique every request if i set it up at start.

Siege:

$  siege -d1 -r10 -c 100 -i -b -f ./urls.txt
Transactions:                   2000 hits
Availability:                 100.00 %
Elapsed time:                   4.67 secs
Data transferred:               0.04 MB
Response time:                  0.23 secs
Transaction rate:             428.27 trans/sec
Throughput:                     0.01 MB/sec
Concurrency:                   97.21
Successful transactions:        2000
Failed transactions:               0
Longest transaction:            0.36
Shortest transaction:           0.02
require_relative 'multithread_state/tenant'
require_relative 'multithread_state/shared/set_subdomain'
require 'rack'
class HelloWorldApp
def self.call(env)
response = Rack::Response.new
data = "#{env['SERVER_NAME']}: #{Tenant.subdomain}"
response.write data
STDOUT.puts data
response.finish
end
end
app = Rack::Builder.new do
use Rack::CommonLogger
use Rack::ShowExceptions
map "/test" do
use Shared::SetSubdomain
run HelloWorldApp
end
end
Rack::Server.start :app => app
require_relative 'multithread_state/tenant_singleton'
require_relative 'multithread_state/shared/set_subdomain_singleton'
require 'rack'
class HelloWorldApp
def self.call(env)
response = Rack::Response.new
tenant = TenantSingleton.instance
data = "#{env['SERVER_NAME']}: #{tenant.subdomain}"
response.write data
STDOUT.puts data
response.finish
end
end
app = Rack::Builder.new do
use Rack::CommonLogger
use Rack::ShowExceptions
map "/test" do
use Shared::SetSubdomainSingleton
run HelloWorldApp
end
end
Rack::Server.start :app => app
# multithread_state/shared/set_subdomain.rb
module Shared
class SetSubdomain
def initialize(app)
@app = app
end
def call(env)
STDOUT.puts 'Setting Subdomain'
Tenant.subdomain = env['SERVER_NAME'].split('.')[0]
@app.call env
end
end
end
# multithread_state/shared/set_subdomain_singleton.rb.rb
module Shared
class SetSubdomainSingleton
def initialize(app)
@app = app
end
def call(env)
STDOUT.puts 'Setting Subdomain'
tenant = TenantSingleton.instance
tenant.subdomain = env['SERVER_NAME'].split('.')[0]
@app.call env
end
end
end
# multithread_state/tenant.rb
class Tenant
class << self
attr_reader :subdomain
def subdomain=(subdomain=nil)
@subdomain = subdomain
end
end
end
# multithread_state/tenant_singleton.rb
class TenantSingleton
@@instance = nil
def self.instance
@@instance ||= TenantSingleton.send(:new)
end
attr_reader :subdomain
def subdomain=(subdomain=nil)
@subdomain = subdomain
end
private_class_method :new
end
PROT=http://
$(PROT)test1.lvh.me:8080/test
$(PROT)test2.lvh.me:8080/test
$(PROT)test3.lvh.me:8080/test
$(PROT)test4.lvh.me:8080/test
$(PROT)test5.lvh.me:8080/test
$(PROT)test6.lvh.me:8080/test
$(PROT)test7.lvh.me:8080/test
$(PROT)test8.lvh.me:8080/test
$(PROT)test9.lvh.me:8080/test
$(PROT)test10.lvh.me:8080/test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment