public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
Search Repo:
Prototyping a load balancer
macournoyer (author)
Tue Jan 22 21:47:06 -0800 2008
commit  038740f29eed799ce7c879bedf321fe83be1e496
tree    cd225cd2e9ddfa87360e7862c08b5a6d56e664a4
parent  57eceafee8354b99f06a6bcb29bceb02e863fc9e
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
0
@@ -1 +1,23 @@
0
+# Balancer client
0
+# Start before server:
0
+#
0
+# ruby client.rb PORT
0
+require 'rubygems'
0
+require 'eventmachine'
0
+
0
+class Connection < EventMachine::Connection
0
+ def receive_data(data)
0
+ # puts ">> Received request"
0
+ send_data "HTTP/1.1 200 OK\r\nConnection: close\r\nServer: thin\r\nContent-Type: text/html\r\n\r\n<html><h1>It works</h1></html>"
0
+ # close_connection_after_writing
0
+ end
0
+end
0
+
0
+port = ARGV[0].to_i
0
+
0
+puts "Starting balancer client on #{port}"
0
+EventMachine::run do
0
+ # EventMachine::start_server '0.0.0.0', port, Connection
0
+ EventMachine::start_unix_domain_server "/tmp/thin_client_#{port}", Connection
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
0
@@ -1 +1,71 @@
0
+# Balancer server
0
+require 'rubygems'
0
+require 'eventmachine'
0
+
0
+class ServerConnection < EventMachine::Connection
0
+ @@backends = []
0
+
0
+ def self.backends
0
+ @@backends
0
+ end
0
+
0
+ def receive_data(data)
0
+ @data = data
0
+ send_data_to_backend
0
+ end
0
+
0
+ def send_data_to_backend
0
+ if @backend = select_backend
0
+ @backend.send_data_for @data, self
0
+ else
0
+ # All backend busy ...
0
+ EventMachine.next_tick { send_data_to_backend }
0
+ end
0
+ end
0
+
0
+ def select_backend
0
+ @@backends.find { |b| b.free? }
0
+ end
0
+
0
+ def unbind
0
+ @backend.server_connection = nil if @backend
0
+ end
0
+end
0
+
0
+class BackendConnection < EventMachine::Connection
0
+ attr_accessor :server_connection
0
+
0
+ def post_init
0
+ @opened = true
0
+ end
0
+
0
+ def free?
0
+ @server_connection.nil? && @opened
0
+ end
0
+
0
+ def send_data_for(data, conn)
0
+ @server_connection = conn
0
+ send_data data
0
+ end
0
+
0
+ def receive_data(data)
0
+ @server_connection.send_data data
0
+ @server_connection.close_connection_after_writing
0
+ end
0
+
0
+ def unbind
0
+ @opened = false
0
+ puts "Backend connection has terminated"
0
+ end
0
+end
0
+
0
+EventMachine::run do
0
+ 5000.upto(5002) do |port|
0
+ # EventMachine::connect '0.0.0.0', port, BackendConnection do |backend|
0
+ EventMachine::connect_unix_domain "/tmp/thin_client_#{port}", BackendConnection do |backend|
0
+ ServerConnection.backends << backend
0
+ end
0
+ end
0
+ EventMachine::start_server '0.0.0.0', 3000, ServerConnection
0
+end

Comments

    No one has commented yet.