socket_duplex 1.1.6 → 1.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/socket_duplex.rb +10 -16
- data/socket_duplex.gemspec +1 -1
- metadata +1 -2
- data/lib/utils.rb +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 396d97092eb58854d336f76c7058737967201b95
|
4
|
+
data.tar.gz: b28000e38753d974e7442f0209b28dc6efe4a3e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a53ca6192b230c303f6f0e2bc9069fcd30cf27f1436af1fcab07f62f0b044cc23598f707903a73cccca9afc53bd70b2250d7b7290c030a686b650593312ef4f
|
7
|
+
data.tar.gz: e9728db7f2ba14f3d78101753b6c12f094ff6bca5c50983780e36b2fd735b5672fe08c95bf4409818056cffa4d0f2988ea47e5cf28b5a865bbea62acf98eee89
|
data/lib/socket_duplex.rb
CHANGED
@@ -3,13 +3,11 @@ require 'socket'
|
|
3
3
|
require 'securerandom'
|
4
4
|
|
5
5
|
require_relative 'websocket-client-simple'
|
6
|
-
require 'utils'
|
7
6
|
|
8
7
|
module Rack
|
9
8
|
class SocketDuplex
|
10
9
|
MAX_QUEUE_SIZE = 50
|
11
10
|
NUM_OF_THREADS = 5
|
12
|
-
@@queue = Utils::QueueWithTimeout.new
|
13
11
|
|
14
12
|
def initialize(app, socket_path, secful_key, verify_mode=OpenSSL::SSL::VERIFY_PEER)
|
15
13
|
@app, @socket_path, @verify_mode = app, socket_path, verify_mode
|
@@ -21,8 +19,8 @@ module Rack
|
|
21
19
|
puts 'secful: agent_identifier'
|
22
20
|
@machine_ip = Socket.ip_address_list.detect(&:ipv4_private?).try(:ip_address)
|
23
21
|
puts 'secful: machine_ip'
|
24
|
-
|
25
|
-
|
22
|
+
@queue = SizedQueue.new(MAX_QUEUE_SIZE)
|
23
|
+
puts 'secful: queue init ' + @queue.__id__.to_s
|
26
24
|
@threads_to_sockets = {}
|
27
25
|
puts 'secful: threads_to_sockets'
|
28
26
|
Thread.new { activate_workers() }
|
@@ -50,10 +48,10 @@ module Rack
|
|
50
48
|
puts 'secful: _call'
|
51
49
|
status, headers, body = @app.call(env)
|
52
50
|
puts 'secful: app.call'
|
53
|
-
puts 'secful: queue.len = ' +
|
54
|
-
if
|
51
|
+
puts 'secful: queue.len = ' + @queue.length.to_s + ' id: ' + @queue.__id__.to_s
|
52
|
+
if @queue.length < MAX_QUEUE_SIZE
|
55
53
|
puts 'secful: about to put in queue'
|
56
|
-
|
54
|
+
@queue << env
|
57
55
|
puts 'secful: put in queue'
|
58
56
|
end
|
59
57
|
return [status, headers, body]
|
@@ -63,21 +61,17 @@ module Rack
|
|
63
61
|
puts 'secful: activate_workers'
|
64
62
|
NUM_OF_THREADS.times do
|
65
63
|
puts 'secful: new thread'
|
66
|
-
Thread.new {worker()}
|
67
|
-
puts 'secful: thread started'
|
64
|
+
thr = Thread.new {worker()}
|
65
|
+
puts 'secful: thread started ' + thr.__id__.to_s
|
68
66
|
end
|
69
67
|
end
|
70
68
|
|
71
69
|
def worker
|
72
70
|
loop do
|
73
71
|
begin
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
rescue => e
|
78
|
-
#puts 'secful: queue-pop-exception: ' + e.message
|
79
|
-
end
|
80
|
-
#puts 'secful: worker poped'
|
72
|
+
puts 'secful: worker start: ' + Thread.current.__id__.to_s + 'queue: ' + @queue.__id__.to_s
|
73
|
+
env = @queue.pop
|
74
|
+
puts 'secful: worker poped'
|
81
75
|
if env
|
82
76
|
puts 'secful: env'
|
83
77
|
connect_to_ws(Thread.current)
|
data/socket_duplex.gemspec
CHANGED
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |spec|
|
5
5
|
spec.name = "socket_duplex"
|
6
|
-
spec.version = '1.1.
|
6
|
+
spec.version = '1.1.7'
|
7
7
|
spec.authors = ["Secful"]
|
8
8
|
spec.description = %q{Rack middleware that duplexes HTTP traffic}
|
9
9
|
spec.summary = spec.description
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: socket_duplex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Secful
|
@@ -117,7 +117,6 @@ files:
|
|
117
117
|
- Gemfile
|
118
118
|
- README.md
|
119
119
|
- lib/socket_duplex.rb
|
120
|
-
- lib/utils.rb
|
121
120
|
- lib/websocket-client-simple.rb
|
122
121
|
- lib/websocket-client-simple/client.rb
|
123
122
|
- lib/websocket-client-simple/version.rb
|
data/lib/utils.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
module Rack::Utils
|
2
|
-
#https://spin.atomicobject.com/2014/07/07/ruby-queue-pop-timeout/
|
3
|
-
class QueueWithTimeout
|
4
|
-
def initialize
|
5
|
-
@mutex = Mutex.new
|
6
|
-
@queue = []
|
7
|
-
@recieved = ConditionVariable.new
|
8
|
-
end
|
9
|
-
|
10
|
-
def <<(x)
|
11
|
-
@mutex.synchronize do
|
12
|
-
@queue << x
|
13
|
-
@recieved.signal
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def length
|
18
|
-
@queue.length
|
19
|
-
end
|
20
|
-
|
21
|
-
def pop(non_block = false)
|
22
|
-
pop_with_timeout(non_block ? 0 : nil)
|
23
|
-
end
|
24
|
-
|
25
|
-
def pop_with_timeout(timeout = nil)
|
26
|
-
@mutex.synchronize do
|
27
|
-
if @queue.empty?
|
28
|
-
@recieved.wait(@mutex, timeout) if timeout != 0
|
29
|
-
#if we're still empty after the timeout, raise exception
|
30
|
-
raise ThreadError, "queue empty" if @queue.empty?
|
31
|
-
end
|
32
|
-
@queue.shift
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|