Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

basic async stuff

  • Loading branch information...
commit 32f5b536b146fbffab75af3be59ea601ce6ce386 1 parent 0519252
Nick Kallen authored
View
12 joke_server.rb
@@ -1,14 +1,10 @@
#!/usr/bin/env ruby
-require 'rubygems'
-require 'eventmachine'
-require 'activesupport'
-require 'statosaurus'
-require 'optparse'
+['rubygems', 'eventmachine', 'activesupport', 'statosaurus', 'optparse'].each { |dependency| require dependency }
begin
$options = {
- :port => 10000
+ :port => 10001
}
OptionParser.new do |opts|
opts.on('-p', "--port PORT", Integer) { |port| $options[:port] = port }
@@ -24,8 +20,8 @@ module JokeServer
def receive_data(data)
$stats.transaction do
$stats.measure('job') do
- 100.times {}
- sleep 0.1
+ 10000.times {}
+ sleep rand
send_data("knock knock\n")
end
end
View
55 log/joke_server.rb.log
@@ -1175,3 +1175,58 @@
2009-04-16T16:59:09-07:00 13011-1239926349-4890 0.100138 0.000100 0.000100
2009-04-16T16:59:09-07:00 13012-1239926349-6725 0.100202 0.000100 0.000100
2009-04-16T16:59:09-07:00 13013-1239926349-4255 0.100157 0.000100 0.000100
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+2009-04-16T17:16:17-07:00 13362-1239927376-1401 0.671029 0.000100 0.000100
+2009-04-16T17:16:17-07:00 13362-1239927377-854 0.220038 0.000100 0.000100
+2009-04-16T17:16:30-07:00 13362-1239927389-253 0.517145 0.000100 0.000100
+2009-04-16T17:16:31-07:00 13362-1239927390-2582 0.497471 0.000100 0.000100
+2009-04-16T17:16:32-07:00 13362-1239927391-6285 0.931459 0.000100 0.000100
+2009-04-16T17:16:33-07:00 13362-1239927393-4055 0.151051 0.000100 0.000100
+2009-04-16T17:16:33-07:00 13362-1239927393-7459 0.762705 0.000100 0.000100
+2009-04-16T17:16:34-07:00 13362-1239927393-5036 0.487099 0.000100 0.000100
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+2009-04-16T17:21:45-07:00 13409-1239927704-7484 0.558001 0.000100 0.000100
+2009-04-16T17:21:48-07:00 13410-1239927707-5593 0.919000 0.000100 0.000100
+2009-04-16T17:21:49-07:00 13411-1239927708-6732 0.951218 0.000100 0.000100
+2009-04-16T17:22:10-07:00 13409-1239927730-6951 0.168583 0.000100 0.000100
+2009-04-16T17:22:11-07:00 13410-1239927730-8418 0.972878 0.000100 0.000100
+2009-04-16T17:22:38-07:00 13409-1239927757-4043 0.800966 0.000100 0.000100
+2009-04-16T17:23:27-07:00 13409-1239927807-4780 0.565189 0.000100 0.000100
+2009-04-16T17:23:27-07:00 13410-1239927807-6244 0.097401 0.000100 0.000100
+2009-04-16T17:23:28-07:00 13411-1239927808-7056 0.713131 0.000100 0.010000
+2009-04-16T17:23:32-07:00 13412-1239927811-9093 0.491806 0.000100 0.000100
+2009-04-16T17:24:00-07:00 13413-1239927840-993 0.195830 0.000100 0.000100
+2009-04-16T17:24:46-07:00 13409-1239927885-3361 0.642644 0.000100 0.000100
+2009-04-16T17:24:54-07:00 13410-1239927894-2115 0.015258 0.000100 0.000100
+2009-04-16T17:24:58-07:00 13411-1239927898-6544 0.198299 0.000100 0.000100
+2009-04-16T17:26:30-07:00 13409-1239927990-3749 0.554883 0.000100 0.000100
+2009-04-16T17:26:44-07:00 13409-1239928004-1680 0.763778 0.000100 0.000100
+2009-04-16T17:26:45-07:00 13410-1239928005-5625 0.302499 0.000100 0.000100
+2009-04-16T17:26:47-07:00 13411-1239928006-5023 0.919556 0.000100 0.000100
+2009-04-16T17:26:48-07:00 13412-1239928007-80 0.503676 0.000100 0.000100
+2009-04-16T17:26:49-07:00 13413-1239928009-9887 0.264269 0.000100 0.000100
+2009-04-16T17:27:11-07:00 13414-1239928030-4270 0.846945 0.000100 0.000100
+2009-04-16T17:27:12-07:00 13415-1239928031-7120 0.814478 0.000100 0.000100
+2009-04-16T17:27:13-07:00 13416-1239928032-1745 0.489830 0.000100 0.000100
+2009-04-16T17:27:38-07:00 13417-1239928057-7401 0.271229 0.000100 0.000100
+2009-04-16T17:27:39-07:00 13418-1239928058-4713 0.736032 0.000100 0.000100
+2009-04-16T17:28:10-07:00 13409-1239928089-9357 0.729216 0.000100 0.000100
+2009-04-16T17:28:18-07:00 13409-1239928098-7897 0.193687 0.000100 0.000100
+2009-04-16T17:28:21-07:00 13410-1239928100-8469 0.917341 0.000100 0.000100
+2009-04-16T17:28:21-07:00 13411-1239928101-8659 0.494485 0.000100 0.000100
+2009-04-16T17:28:24-07:00 13412-1239928103-4847 0.165890 0.000100 0.000100
+2009-04-16T17:28:24-07:00 13413-1239928104-9537 0.701739 0.000100 0.000100
+2009-04-16T17:28:25-07:00 13414-1239928104-5926 0.701963 0.000100 0.000100
View
40 log/proxy.rb.log
@@ -1154,3 +1154,43 @@
2009-04-16T16:59:09-07:00 13242-1239926349-5241 0.100895 0.000100 0.000100
2009-04-16T16:59:09-07:00 13242-1239926349-8221 0.100735 0.000100 0.000100
2009-04-16T16:59:09-07:00 13242-1239926349-3022 0.100848 0.000100 0.000100
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+2009-04-16T17:21:45-07:00 13429-1239927704-8315 0.560124 0.000100 0.000100
+2009-04-16T17:21:48-07:00 13429-1239927707-3993 0.919765 0.000100 0.000100
+2009-04-16T17:21:49-07:00 13429-1239927708-9132 0.952079 0.000100 0.000100
+# Fields: job_user job_sys job_real
+2009-04-16T17:22:10-07:00 13441-1239927730-4085 0.170136 0.000100 0.000100
+2009-04-16T17:22:11-07:00 13441-1239927730-8676 0.973626 0.010000 0.000100
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+2009-04-16T17:23:27-07:00 13489-1239927807-2840 0.567019 0.000100 0.000100
+2009-04-16T17:23:27-07:00 13489-1239927807-7774 0.098034 0.000100 0.000100
+2009-04-16T17:23:28-07:00 13489-1239927808-969 0.713736 0.000100 0.000100
+2009-04-16T17:23:32-07:00 13489-1239927811-2293 0.492567 0.000100 0.000100
+2009-04-16T17:24:00-07:00 13489-1239927840-7491 0.196699 0.000100 0.000100
+# Fields: job_user job_sys job_real
+2009-04-16T17:24:46-07:00 13509-1239927885-8157 0.644372 0.000100 0.000100
+2009-04-16T17:24:54-07:00 13509-1239927894-9786 0.016059 0.000100 0.000100
+2009-04-16T17:24:58-07:00 13509-1239927898-6008 0.199041 0.000100 0.000100
+# Fields: job_user job_sys job_real
+# Fields: job_user job_sys job_real
+2009-04-16T17:26:44-07:00 13533-1239928004-7774 0.765667 0.010000 0.000100
+2009-04-16T17:26:45-07:00 13533-1239928005-4277 0.303159 0.000100 0.000100
+2009-04-16T17:26:47-07:00 13533-1239928006-7051 0.920220 0.000100 0.000100
+2009-04-16T17:26:48-07:00 13533-1239928007-7576 0.504353 0.000100 0.000100
+2009-04-16T17:26:49-07:00 13533-1239928009-9370 0.264938 0.000100 0.000100
+2009-04-16T17:27:11-07:00 13533-1239928030-9981 0.847717 0.000100 0.000100
+2009-04-16T17:27:12-07:00 13533-1239928031-4947 0.816705 0.000100 0.000100
+2009-04-16T17:27:13-07:00 13533-1239928032-2355 0.490735 0.000100 0.000100
+2009-04-16T17:27:38-07:00 13533-1239928057-8622 0.272053 0.000100 0.000100
+2009-04-16T17:27:39-07:00 13533-1239928058-9397 0.736819 0.000100 0.000100
+2009-04-16T17:28:10-07:00 13533-1239928089-3887 0.729849 0.000100 0.000100
+# Fields: job_user job_sys job_real
+2009-04-16T17:28:18-07:00 13551-1239928098-1007 0.195429 0.000100 0.000100
+2009-04-16T17:28:21-07:00 13551-1239928100-9140 0.918091 0.010000 0.000100
+2009-04-16T17:28:21-07:00 13551-1239928101-3717 0.495213 0.000100 0.000100
+2009-04-16T17:28:24-07:00 13551-1239928103-8019 0.166559 0.000100 0.000100
+2009-04-16T17:28:24-07:00 13551-1239928104-6146 0.702492 0.000100 0.000100
+2009-04-16T17:28:25-07:00 13551-1239928104-1436 0.702699 0.000100 0.000100
View
24 proxy.rb
@@ -23,12 +23,18 @@
end
module ProxyServer
+ include EventMachine::Deferrable
+
def receive_data(data)
- $stats.transaction do # TODO propagate txnid to server.
- $stats.measure('job') do
- send_data(ProxyServer.forward(data))
+ proxy = self
+ EventMachine.spawn do
+ p "spawning"
+ $stats.transaction do # TODO propagate txnid to server.
+ $stats.measure('job') do
+ proxy.send_data(ProxyServer.forward(data))
+ end
end
- end
+ end.run
end
def self.forward(data)
@@ -37,13 +43,17 @@ def self.forward(data)
private
def self.servers
- @servers ||= (1..$options[:count]).inject([]) do |servers, i|
- servers << Server.new($options[:host], $options[:port] + i)
+ Thread.exclusive do
+ @servers ||= (1..$options[:count]).inject([]) do |servers, i|
+ servers << Server.new($options[:host], $options[:port] + i)
+ end
end
end
def self.balancer
- @balancer ||= $options[:balancer].new(servers)
+ Thread.exclusive do
+ @balancer ||= $options[:balancer].new(servers)
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.