r_proxy 0.2.2 → 0.2.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d037d1602ec7e506e092afc420294d16226a968733bb966dd3def1c84b539e36
4
- data.tar.gz: feb0fe63e0d1126e601f100aa00c93d3a8ead3b8fd8dce870d103f12e445f5f1
3
+ metadata.gz: 9f0065ab65442af3554629362b7ba10f12e247a4b81c53ededc545c381b4d5e8
4
+ data.tar.gz: 3882a0ec3c594581970a7db9cca5a79056563bc2d64dfa7bed91495724c903eb
5
5
  SHA512:
6
- metadata.gz: 1756dc3c88ae2fa70031654f102bfbd1519e453e375950ac718d141ec7ae7ce0a11922030d38b0a9be380144e0bb29efbd5d8f49778d359abeb8dcf1305e5984
7
- data.tar.gz: 54c20c979bc70a713c92d1ca66398c0589624e3f87df59afa9eb2fe6ff58c398051c441fe17bdfb315c44abfe8abeb76ea9560d260f300550c3367d1f6b71a53
6
+ metadata.gz: 0a53bd67b6d97a735a0cfc4d1a3b836f54ec6aa41301b5d450905f09612b0374dd52ea29e173873048fa1242439fa7025c1a60c89e25eab6d7231d7e6ad1846a
7
+ data.tar.gz: 867cd29058afb86b28aa7d45d9e30c1f6d2b5f5c16c806464de9e3388f30048bc6008943331f7c0cd87ac4b89d855a7f5a0b9014e7e56497cd34e360943e0d5d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- r_proxy (0.2.0)
4
+ r_proxy (0.2.3)
5
5
  eventmachine (~> 1.2, >= 1.2.7)
6
6
  redis (~> 4.1, >= 4.1.4)
7
7
 
data/lib/r_proxy.rb CHANGED
@@ -11,7 +11,6 @@ require 'r_proxy/http_proxy_parser'
11
11
  require 'r_proxy/redis_service'
12
12
 
13
13
  require 'r_proxy/master_process'
14
- require 'r_proxy/process_handler'
15
14
 
16
15
  require 'r_proxy/target_connection'
17
16
  require 'r_proxy/connection_handler'
@@ -8,6 +8,9 @@ module RProxy
8
8
  def initialize
9
9
  @config = RProxy::Config.new
10
10
  @pids = []
11
+ @watchers = []
12
+ @watcher_status = true
13
+ @mutex = Mutex.new
11
14
  end
12
15
 
13
16
  def set(name, value)
@@ -15,64 +18,79 @@ module RProxy
15
18
  end
16
19
 
17
20
  def run!
21
+ Signal.trap("TERM") { exit }
18
22
  at_exit { stop_all_process }
19
23
  @logger = @config.logger
20
24
  begin
21
25
  start_r_proxy
22
26
  rescue Interrupt
23
- @logger.info('existing all process....') if @logger
24
- EventMachine.stop_event_loop if EventMachine.reactor_running?
27
+ exit
25
28
  rescue => e
26
- @logger.info("master process exit with #{e.message}, #{e.backtrace}") if @logger
27
- EventMachine.stop_event_loop if EventMachine.reactor_running?
29
+ @logger.info("master process error: #{e.message}, #{e.backtrace}") if @logger
30
+ exit
28
31
  end
29
32
  end
30
33
 
31
34
  private
32
35
 
33
36
  def stop_all_process
37
+ @watcher_status = false
34
38
  @pids.each do |pid|
35
39
  next unless pid
36
40
  Process.kill("TERM", pid)
37
41
  end
42
+ @watchers.each { |t| t.join }
43
+ @logger.info('all process exited....') if @logger
38
44
  end
39
45
 
40
- def start_r_proxy
41
46
 
42
- instance_amount = @config.instances
43
- server = TCPServer.new(@config.host, @config.port)
44
- instance_amount.times do
45
- pid = Process.fork do
46
- timestamp = (Time.now.to_f * 1000).round
47
- begin
48
- @logger.info("r_proxy @#{timestamp} process start....") if @logger
49
- RProxy::ProxyServer.new(server, @config, timestamp).run!
50
- rescue Interrupt
51
- @logger.info("r_proxy TPC server instance @#{timestamp} closed now....") if @logger
52
- rescue => e
53
- @logger.error("instance @#{timestamp}, error: #{e.message}, #{e.backtrace}") if @logger
54
- exit!(false)
55
- end
47
+ def spawn_sub_process(server)
48
+ pid = Process.fork do
49
+ timestamp = (Time.now.to_f * 1000).round
50
+ begin
51
+ @logger.info("r_proxy @#{timestamp} process start....") if @logger
52
+ RProxy::ProxyServer.new(server, @config, timestamp).run!
53
+ rescue Interrupt, SystemExit
54
+ @logger.info("r_proxy TPC server instance @#{timestamp} closed now....") if @logger
55
+ rescue => e
56
+ @logger.error("instance @#{timestamp}, error: #{e.message}, #{e.backtrace}") if @logger
57
+ exit(false)
58
+ end
59
+ end
60
+ pid
61
+ end
62
+
63
+ def exec_with_watcher(server)
64
+ @watchers << Thread.fork do
65
+ while @watcher_status
66
+ pid = spawn_sub_process(server)
67
+ push_pids(pid)
68
+ Process.waitpid(pid, 0)
69
+ remove_pid(pid)
56
70
  end
71
+ end
72
+ end
57
73
 
58
- Process.detach(pid)
74
+ def push_pids(pid)
75
+ @mutex.synchronize do
59
76
  @pids << pid
60
- sleep(0.1)
61
77
  end
78
+ end
62
79
 
63
- if EventMachine.kqueue?
64
- EventMachine.kqueue=(true)
80
+ def remove_pid(pid)
81
+ @mutex.synchronize do
82
+ @pids.delete(pid)
65
83
  end
66
- EventMachine.run do
67
- @pids.each do |pid|
68
- EventMachine.watch_process(pid,
69
- RProxy::ProcessHandler,
70
- @pids,
71
- @config,
72
- server,
73
- pid)
74
- end
84
+ end
85
+
86
+ def start_r_proxy
87
+ instance_amount = @config.instances
88
+ server = TCPServer.new(@config.host, @config.port)
89
+ instance_amount.times do
90
+ exec_with_watcher(server)
91
+ sleep(0.1)
75
92
  end
93
+ sleep
76
94
  end
77
95
  end
78
96
  end
@@ -1,3 +1,3 @@
1
1
  module RProxy
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: r_proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick An
@@ -78,7 +78,6 @@ files:
78
78
  - lib/r_proxy/http_post_template.rb
79
79
  - lib/r_proxy/http_proxy_parser.rb
80
80
  - lib/r_proxy/master_process.rb
81
- - lib/r_proxy/process_handler.rb
82
81
  - lib/r_proxy/proxy_server.rb
83
82
  - lib/r_proxy/redis_service.rb
84
83
  - lib/r_proxy/target_connection.rb
@@ -1,39 +0,0 @@
1
- module RProxy
2
- class ProcessHandler < EventMachine::ProcessWatch
3
-
4
- def initialize(pids, config, socket, pid)
5
- @pids = pids
6
- @id = pid
7
- @config = config
8
- @socket = socket
9
- @logger = config.logger
10
- end
11
-
12
- def process_exited
13
-
14
- @pids.delete(@id)
15
- pid = Process.fork do
16
- timestamp = (Time.now.to_f * 1000).round
17
- begin
18
- @logger.info("r_proxy rebuild new instance replace @#{timestamp}....") if @logger
19
- RProxy::ProxyServer.new(@socket, @config, timestamp).run!
20
- rescue Interrupt
21
- @logger.info("r_proxy TPC server instance @#{timestamp} closed now....") if @logger
22
- rescue => e
23
- @logger.error("instance @#{timestamp}, error: #{e.message}, #{e.backtrace}") if @logger
24
- exit(false)
25
- end
26
- end
27
-
28
- Process.detach(pid)
29
- @pids << pid
30
-
31
- EventMachine.watch_process(pid, RProxy::ProcessHandler,
32
- @pids,
33
- @config,
34
- @socket,
35
- pid)
36
- close_connection
37
- end
38
- end
39
- end