proxymachine 0.2.7 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ = 0.2.8 / 2009-10-14
2
+ * Minor changes
3
+ * Always log proxy connection
4
+ * Add version and total connection count to procline
5
+ * Add max connection count to procline
6
+ * Use Logger for logging
7
+
1
8
  = 0.2.7 / 2009-10-12
2
9
  * Minor changes
3
10
  * Use a 10k buffer to prevent memory growth due to slow clients
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 2
3
- :patch: 7
3
+ :patch: 8
4
4
  :major: 0
data/bin/proxymachine CHANGED
@@ -38,8 +38,8 @@ rescue Exception => e
38
38
  if e.instance_of?(SystemExit)
39
39
  raise
40
40
  else
41
- puts 'Uncaught exception'
42
- puts e.message
43
- puts e.backtrace.join("\n")
41
+ LOGGER.info 'Uncaught exception'
42
+ LOGGER.info e.message
43
+ LOGGER.info e.backtrace.join("\n")
44
44
  end
45
45
  end
data/examples/git.rb CHANGED
@@ -6,7 +6,7 @@ class GitRouter
6
6
  # Look at the routing table and return the correct address for +name+
7
7
  # Returns "<host>:<port>" e.g. "ae8f31c.example.com:9418"
8
8
  def self.lookup(name)
9
- puts "Proxying for user #{name}"
9
+ LOGGER.info "Proxying for user #{name}"
10
10
  "localhost:9418"
11
11
  end
12
12
  end
@@ -3,17 +3,26 @@ module EventMachine
3
3
  class ClientConnection < Connection
4
4
  def self.start(host, port)
5
5
  $server = EM.start_server(host, port, self)
6
- puts "Listening on #{host}:#{port}"
7
- puts "Send QUIT to quit after waiting for all connections to finish."
8
- puts "Send TERM or INT to quit after waiting for up to 10 seconds for connections to finish."
6
+ LOGGER.info "Listening on #{host}:#{port}"
7
+ LOGGER.info "Send QUIT to quit after waiting for all connections to finish."
8
+ LOGGER.info "Send TERM or INT to quit after waiting for up to 10 seconds for connections to finish."
9
9
  end
10
10
 
11
11
  def post_init
12
+ LOGGER.info "Accepted #{peer}"
12
13
  @buffer = []
13
14
  @tries = 0
14
15
  ProxyMachine.incr
15
16
  end
16
17
 
18
+ def peer
19
+ @peer ||=
20
+ begin
21
+ port, ip = Socket.unpack_sockaddr_in(get_peername)
22
+ "#{ip}:#{port}"
23
+ end
24
+ end
25
+
17
26
  def receive_data(data)
18
27
  if !@server_side
19
28
  @buffer << data
@@ -21,13 +30,14 @@ module EventMachine
21
30
  end
22
31
  rescue => e
23
32
  close_connection
24
- puts "#{e.class} - #{e.message}"
33
+ LOGGER.info "#{e.class} - #{e.message}"
25
34
  end
26
35
 
27
36
  def ensure_server_side_connection
28
37
  @timer.cancel if @timer
29
38
  unless @server_side
30
39
  commands = ProxyMachine.router.call(@buffer.join)
40
+ LOGGER.info "#{peer} #{commands.inspect}"
31
41
  close_connection unless commands.instance_of?(Hash)
32
42
  if remote = commands[:remote]
33
43
  m, host, port = *remote.match(/^(.+):(.+)$/)
@@ -55,20 +65,18 @@ module EventMachine
55
65
  def try_server_connect(host, port)
56
66
  @server_side = ServerConnection.request(host, port, self)
57
67
  proxy_incoming_to(@server_side, 10240)
58
- if @tries > 0
59
- puts "Successful connection."
60
- end
68
+ LOGGER.info "Successful connection to #{host}:#{port}."
61
69
  true
62
70
  rescue => e
63
71
  if @tries < 10
64
72
  @tries += 1
65
- puts "Failed on server connect attempt #{@tries}. Trying again..."
73
+ LOGGER.info "Failed on server connect attempt #{@tries}. Trying again..."
66
74
  @timer.cancel if @timer
67
75
  @timer = EventMachine::Timer.new(0.1) do
68
76
  self.ensure_server_side_connection
69
77
  end
70
78
  else
71
- puts "Failed after ten connection attempts."
79
+ LOGGER.info "Failed after ten connection attempts."
72
80
  end
73
81
  false
74
82
  end
data/lib/proxymachine.rb CHANGED
@@ -1,14 +1,21 @@
1
1
  require 'rubygems'
2
2
  require 'eventmachine'
3
+ require 'logger'
3
4
 
4
5
  require 'proxymachine/client_connection'
5
6
  require 'proxymachine/server_connection'
6
7
 
8
+ LOGGER = Logger.new(STDOUT)
9
+
7
10
  class ProxyMachine
8
11
  MAX_FAST_SHUTDOWN_SECONDS = 10
9
12
 
10
13
  def self.update_procline
11
- $0 = "proxymachine - #{@@name} #{@@listen} - #{self.count} connections"
14
+ $0 = "proxymachine #{VERSION} - #{@@name} #{@@listen} - #{self.stats} cur/max/tot conns"
15
+ end
16
+
17
+ def self.stats
18
+ "#{@@counter}/#{@@maxcounter}/#{@@totalcounter}"
12
19
  end
13
20
 
14
21
  def self.count
@@ -16,7 +23,9 @@ class ProxyMachine
16
23
  end
17
24
 
18
25
  def self.incr
26
+ @@totalcounter += 1
19
27
  @@counter += 1
28
+ @@maxcounter = @@counter if @@counter > @@maxcounter
20
29
  self.update_procline
21
30
  @@counter
22
31
  end
@@ -24,7 +33,7 @@ class ProxyMachine
24
33
  def self.decr
25
34
  @@counter -= 1
26
35
  if $server.nil?
27
- puts "Waiting for #{@@counter} connections to finish."
36
+ LOGGER.info "Waiting for #{@@counter} connections to finish."
28
37
  end
29
38
  self.update_procline
30
39
  EM.stop if $server.nil? and @@counter == 0
@@ -41,17 +50,17 @@ class ProxyMachine
41
50
 
42
51
  def self.graceful_shutdown(signal)
43
52
  EM.stop_server($server) if $server
44
- puts "Received #{signal} signal. No longer accepting new connections."
45
- puts "Waiting for #{ProxyMachine.count} connections to finish."
53
+ LOGGER.info "Received #{signal} signal. No longer accepting new connections."
54
+ LOGGER.info "Waiting for #{ProxyMachine.count} connections to finish."
46
55
  $server = nil
47
56
  EM.stop if ProxyMachine.count == 0
48
57
  end
49
58
 
50
59
  def self.fast_shutdown(signal)
51
60
  EM.stop_server($server) if $server
52
- puts "Received #{signal} signal. No longer accepting new connections."
53
- puts "Maximum time to wait for connections is #{MAX_FAST_SHUTDOWN_SECONDS} seconds."
54
- puts "Waiting for #{ProxyMachine.count} connections to finish."
61
+ LOGGER.info "Received #{signal} signal. No longer accepting new connections."
62
+ LOGGER.info "Maximum time to wait for connections is #{MAX_FAST_SHUTDOWN_SECONDS} seconds."
63
+ LOGGER.info "Waiting for #{ProxyMachine.count} connections to finish."
55
64
  $server = nil
56
65
  EM.stop if ProxyMachine.count == 0
57
66
  Thread.new do
@@ -61,6 +70,8 @@ class ProxyMachine
61
70
  end
62
71
 
63
72
  def self.run(name, host, port)
73
+ @@totalcounter = 0
74
+ @@maxcounter = 0
64
75
  @@counter = 0
65
76
  @@name = name
66
77
  @@listen = "#{host}:#{port}"
@@ -80,6 +91,15 @@ class ProxyMachine
80
91
  end
81
92
  end
82
93
  end
94
+
95
+ def self.version
96
+ yml = YAML.load(File.read(File.join(File.dirname(__FILE__), *%w[.. VERSION.yml])))
97
+ "#{yml[:major]}.#{yml[:minor]}.#{yml[:patch]}"
98
+ rescue
99
+ 'unknown'
100
+ end
101
+
102
+ VERSION = self.version
83
103
  end
84
104
 
85
105
  module Kernel
data/proxymachine.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{proxymachine}
8
- s.version = "0.2.7"
8
+ s.version = "0.2.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tom Preston-Werner"]
12
- s.date = %q{2009-10-12}
12
+ s.date = %q{2009-10-14}
13
13
  s.default_executable = %q{proxymachine}
14
14
  s.email = %q{tom@mojombo.com}
15
15
  s.executables = ["proxymachine"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proxymachine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Preston-Werner
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-12 00:00:00 -07:00
12
+ date: 2009-10-14 00:00:00 -07:00
13
13
  default_executable: proxymachine
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency