Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

less stupid threading

  • Loading branch information...
commit b18fe817a75ab3505aa7ebe8fab8a98d35ca4f36 1 parent ed7b9b5
Nick Kallen authored
View
2  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', 'util/in_process_lru_cache'].each { |dependency| require dependency }
+['util/statosaurus', 'util/synchronizable', 'util/line_buffered_connection', 'util/in_process_lru_cache'].each { |dependency| require dependency }
begin
$options = {
View
9 proxy.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
['rubygems', 'activesupport', 'eventmachine', 'socket', 'optparse'].each { |dependency| require dependency }
-['util/statosaurus', 'util/line_buffered_connection', 'util/deferrable'].each { |dependency| require dependency }
+['util/statosaurus', 'util/synchronizable', '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
@@ -20,11 +20,12 @@
begin
logfile = File.join(File.dirname(__FILE__), 'log', File.basename(__FILE__, '.rb') + '.log')
- $stats = Statosaurus.new(['job_user', 'job_sys', 'job_real'], Logger.new(logfile))
+ $stats = Statosaurus.new(['job_user', 'job_sys', 'job_real', 'server'], Logger.new(logfile))
end
module ProxyServer
include LineBufferedConnection, Deferrable
+ extend Synchronizable
def receive_line(line)
defer do
@@ -43,7 +44,7 @@ def self.forward(data)
private
def self.servers
- @servers ||= Thread.exclusive do
+ @servers ||= synchronize(:servers) do
(1..$options[:count]).inject([]) do |servers, i|
servers << Server.new($options[:host], $options[:port] + i)
end
@@ -51,7 +52,7 @@ def self.servers
end
def self.balancer
- @balancer ||= Thread.exclusive do
+ @balancer ||= synchronize(:balancer) do
$options[:balancer].new(servers)
end
end
View
4 proxy/balancers/least_connections.rb
@@ -1,6 +1,8 @@
require 'proxy/balancers/balancer'
class LeastConnections < Balancer
+ include Synchronizable
+
def forward(data)
next_server do |server|
server.call(data)
@@ -10,7 +12,7 @@ def forward(data)
private
def next_server
server = nil
- Thread.exclusive do
+ synchronize(:next_server) do
server = servers.min do |s1, s2|
s1.connections <=> s2.connections
end
View
4 proxy/balancers/round_robin.rb
@@ -1,13 +1,15 @@
require 'proxy/balancers/balancer'
class RoundRobin < Balancer
+ include Synchronizable
+
def forward(data)
next_server.call(data)
end
private
def next_server
- Thread.exclusive do
+ synchronize(:next_server) do
@current ||= 0
@current = (@current + 1) % servers.size
servers[@current]
View
8 proxy/server.rb
@@ -1,5 +1,7 @@
class Server
- include Deferrable
+ include Synchronizable
+
+ attr_reader :port
def initialize(host, port)
@host, @port = host, port
@@ -14,13 +16,13 @@ def connections=(connections)
end
def reserve
- Thread.exclusive do
+ synchronize(:connections) do
self.connections += 1
end
end
def release
- Thread.exclusive do
+ synchronize(:connections) do
self.connections -= 1
end
end
View
11 util/synchronizable.rb
@@ -0,0 +1,11 @@
+module Synchronizable
+ def mutex
+ @mutex ||= Hash.new do |h,k|
+ h[k] = Mutex.new
+ end
+ end
+
+ def synchronize(name, &block)
+ mutex[name].synchronize(&block)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.