Browse files

sticky session optimization

  • Loading branch information...
1 parent 160e1be commit af694f5622e338dbd86e39ee3d3fa42bdb26d630 Nick Kallen committed Apr 16, 2009
Showing with 3,231 additions and 10 deletions.
  1. +3 −3 jb
  2. +10 −5 joke_server.rb
  3. +1,691 −0 log/joke_server.log
  4. +1,494 −0 log/proxy.log
  5. +3 −2 proxy.rb
  6. +14 −0 proxy/balancers/sticky.rb
  7. +16 −0 util/in_process_lru_cache.rb
View
6 jb
@@ -26,11 +26,11 @@ count_per_worker = $options[:count] / $options[:concurrency]
benchmark = Benchmark.measure do
threads = []
- $options[:concurrency].times do
+ $options[:concurrency].times do |request|
threads << Thread.new do
socket = TCPSocket.new($options[:host], $options[:port])
- count_per_worker.times do |i|
- socket.print("\n")
+ count_per_worker.times do
+ socket.print("#{request}\n")
socket.readline
end
end
View
15 joke_server.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
['rubygems', 'eventmachine', 'activesupport', 'optparse'].each { |dependency| require dependency }
-['util/statosaurus', 'util/line_buffered_connection'].each { |dependency| require dependency }
+['util/statosaurus', 'util/line_buffered_connection', 'util/in_process_lru_cache'].each { |dependency| require dependency }
begin
$options = {
@@ -19,14 +19,19 @@
module JokeServer
include LineBufferedConnection
+ extend InProcessLRUCache
def receive_line(line)
$stats.transaction do
- $stats.set('source_transaction_id', line)
+ data, source_transaction_id = line.split(';')
+ $stats.set('source_transaction_id', data)
$stats.measure('job') do
- 100000.times { Time.now }
- sleep rand * 3
- send_data("KNOCK KNOCK\n")
+ result = JokeServer.get(data) do
+ 100000.times { Time.now }
+ sleep rand * 3
+ "KNOCK KNOCK: #{data}\n"
+ end
+ send_data(result)
end
end
end
View
1,691 log/joke_server.log
1,691 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
1,494 log/proxy.log
1,494 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
5 proxy.rb
@@ -1,7 +1,8 @@
#!/usr/bin/env ruby
['rubygems', 'activesupport', 'eventmachine', 'socket', 'optparse'].each { |dependency| require dependency }
-['util/statosaurus', 'util/line_buffered_connection', 'util/deferrable', 'proxy/server', 'proxy/balancers/first', 'proxy/balancers/random', 'proxy/balancers/round_robin', 'proxy/balancers/least_connections'].each { |dependency| require dependency }
+['util/statosaurus', 'util/line_buffered_connection', 'util/deferrable'].each { |dependency| require dependency }
+['proxy/server', 'proxy/balancers/first', 'proxy/balancers/random', 'proxy/balancers/round_robin', 'proxy/balancers/least_connections', 'proxy/balancers/sticky'].each { |dependency| require dependency }
begin
$options = {
@@ -29,7 +30,7 @@ def receive_line(line)
defer do
$stats.transaction do
$stats.measure('job') do
- message = $stats.transaction_id + "\n"
+ message = "#{line};#{$stats.transaction_id}\n"
send_data(ProxyServer.forward(message))
end
end
View
14 proxy/balancers/sticky.rb
@@ -0,0 +1,14 @@
+require 'proxy/balancers/balancer'
+
+class Sticky < Balancer
+ def forward(data)
+ server_for(data).call(data)
+ end
+
+ private
+ def server_for(data)
+ p data
+ data = data.split(';').first
+ servers[data.to_i % servers.size]
+ end
+end
View
16 util/in_process_lru_cache.rb
@@ -0,0 +1,16 @@
+module InProcessLRUCache
+ def get(data)
+ @cache ||= []
+ cache_hit = @cache.detect do |key, value|
+ data == key
+ end
+ if !cache_hit
+ result = yield(data)
+ @cache.unshift([data, result])
+ @cache.slice!(2..-1)
+ result
+ else
+ cache_hit[1]
+ end
+ end
+end

0 comments on commit af694f5

Please sign in to comment.