<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -27,6 +27,15 @@ module ProxyServer
   include LineBufferedConnection, Deferrable
   extend Synchronizable
 
+  @@servers = (1..$options[:count]).inject([]) do |servers, i|
+    servers &lt;&lt; Server.new($options[:host], $options[:port] + i)
+  end
+  @@balancer = $options[:balancer].new(@@servers)
+
+  def self.forward(data)
+    @@balancer.forward(data)
+  end
+  
   def receive_line(line)
     defer do
       $stats.transaction do
@@ -37,27 +46,6 @@ module ProxyServer
       end
     end
   end
-
-  def self.forward(data)
-    balancer.forward(data)
-  end
-
-  private  
-  def self.servers
-    synchronize(:servers) do
-      @servers ||= begin
-        (1..$options[:count]).inject([]) do |servers, i|
-          servers &lt;&lt; Server.new($options[:host], $options[:port] + i)
-        end
-      end
-    end
-  end
-
-  def self.balancer
-    synchronize(:balancer) do
-      @balancer ||= $options[:balancer].new(servers)
-    end
-  end
 end
 
 EM.run do</diff>
      <filename>proxy.rb</filename>
    </modified>
    <modified>
      <diff>@@ -13,9 +13,7 @@ class LeastConnections &lt; Balancer
   def next_server
     server = nil
     synchronize(:next_server) do
-      server = servers.min do |s1, s2|
-        s1.connections &lt;=&gt; s2.connections
-      end
+      server = servers.min
       server.reserve
     end
 </diff>
      <filename>proxy/balancers/least_connections.rb</filename>
    </modified>
    <modified>
      <diff>@@ -5,18 +5,18 @@ class Server
 
   def initialize(host, port)
     @host, @port = host, port
-    synchronize(:connections) { @connections = 0 }
+    @connections = 0
   end
 
   def reserve
     synchronize(:connections) do
-      self.connections += 1
+      @connections += 1
     end
   end
 
   def release
     synchronize(:connections) do
-      self.connections -= 1
+      @connections -= 1
     end
   end
 
@@ -26,17 +26,13 @@ class Server
       socket.readline
     end
   end
+  
+  def &lt;=&gt;(other)
+    connections &lt;=&gt; other.connections
+  end
 
-  private
-  # Whilst I'm not normally a fan of closing apis or gratuitously spreading
-  # private around, this code is subject to thread safety concerns, so
-  # assignment should be ensure local, or the value should not be accessible,
-  # instead it should have a thread safe incr! and decr!.
+  protected
   def connections
     @connections
   end
-
-  def connections=(connections)
-    @connections = connections
-  end
 end
\ No newline at end of file</diff>
      <filename>proxy/server.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,19 +1,16 @@
 module Synchronizable
   @@mutex = Mutex.new
-
-  def mutex
-    # Yup, this is nasty, but it's the only way without patching
-    # initialize/new and having included and extended callbacks.
+  @@mutexes = Hash.new do |h,k|
+    h[k] = Mutex.new
+  end
+  
+  def mutex(mutex)
     @@mutex.synchronize do
-      @mutex ||= Hash.new do |h,k|
-        @@mutex.synchronize do
-          h[k] = Mutex.new
-        end
-      end
+      @@mutexes[mutex]
     end
   end
 
   def synchronize(name, &amp;block)
-    mutex[name].synchronize(&amp;block)
+    mutex(name).synchronize(&amp;block)
   end
 end
\ No newline at end of file</diff>
      <filename>util/synchronizable.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>e946183a140ba251d30ab734362910231e0bf32e</id>
    </parent>
  </parents>
  <author>
    <name>Nick Kallen</name>
    <email>nkallen@yahoo.com</email>
  </author>
  <url>http://github.com/nkallen/gogaruco/commit/6abbb13f2b6491d1993909e012ed6e05fd3845e7</url>
  <id>6abbb13f2b6491d1993909e012ed6e05fd3845e7</id>
  <committed-date>2009-04-20T20:02:53-07:00</committed-date>
  <authored-date>2009-04-20T20:02:53-07:00</authored-date>
  <message>fixes some race conditions, keeps the code terse</message>
  <tree>a59f445403d0f99a6fcf7d1da608acc93e1d266a</tree>
  <committer>
    <name>Nick Kallen</name>
    <email>nkallen@yahoo.com</email>
  </committer>
</commit>
