socket_duplex 1.1.4 → 1.1.5

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
  SHA1:
3
- metadata.gz: bd7858a97d9298289df3a28e442b08de176037fd
4
- data.tar.gz: 9ed7ff8d36e75df4515a12443fe9393c98c71643
3
+ metadata.gz: dd2f17bdd206534047e35566999937cdd332b912
4
+ data.tar.gz: 7502e0228ba1ebed5269c49bb698ba5ac41a0c11
5
5
  SHA512:
6
- metadata.gz: d70a1620423200f85664b8ada1bae89f937d581e076b2e3b17496a8c6bfda5efa9bc78b26c4575bd705a1a62c5599d2cd4e8165e814f4dc63257b717341a11a7
7
- data.tar.gz: 25c070803d311e6831fb6afff2ab56310113a3fe2b6a1414f9d06174579f7a9333d3308437b1684e15f2273e6a6c38e9ba5b0c42feb73767ca980f883cf27815
6
+ metadata.gz: df436ea1c4ae42e96fe9c346739b5bdfa354cfbdf93954b2ab880b6d47d202c40eaedea0478cd40ed20dd997a1d477e52c0a4d1fbd1a0bd5bd1cf2e8215e275f
7
+ data.tar.gz: 82d081d2e7cf6b486440a26ecb210beb7cd3e348079e5aaf7432cd16b71a53c68a36a65dba1008e64df9afc5a65b201ffb4db1fbeaed97f2eb60327d8e7c1586
data/lib/socket_duplex.rb CHANGED
@@ -8,6 +8,7 @@ module Rack
8
8
  class SocketDuplex
9
9
  MAX_QUEUE_SIZE = 50
10
10
  NUM_OF_THREADS = 5
11
+ @@queue = Utils::QUERY_STRING.new
11
12
 
12
13
  def initialize(app, socket_path, secful_key, verify_mode=OpenSSL::SSL::VERIFY_PEER)
13
14
  @app, @socket_path, @verify_mode = app, socket_path, verify_mode
@@ -19,8 +20,8 @@ module Rack
19
20
  puts 'secful: agent_identifier'
20
21
  @machine_ip = Socket.ip_address_list.detect(&:ipv4_private?).try(:ip_address)
21
22
  puts 'secful: machine_ip'
22
- @queue = SizedQueue.new(MAX_QUEUE_SIZE)
23
- puts 'secful: queue'
23
+ #@queue = SizedQueue.new(MAX_QUEUE_SIZE)
24
+ #puts 'secful: queue init ' + @queue.__id__.to_s
24
25
  @threads_to_sockets = {}
25
26
  puts 'secful: threads_to_sockets'
26
27
  Thread.new { activate_workers() }
@@ -48,10 +49,10 @@ module Rack
48
49
  puts 'secful: _call'
49
50
  status, headers, body = @app.call(env)
50
51
  puts 'secful: app.call'
51
- puts 'secful: queue.len = ' + @queue.length.to_s
52
- if @queue.length < @queue.max
52
+ puts 'secful: queue.len = ' + @@queue.length.to_s
53
+ if @@queue.length < MAX_QUEUE_SIZE
53
54
  puts 'secful: about to put in queue'
54
- @queue << env
55
+ @@queue << env
55
56
  puts 'secful: put in queue'
56
57
  end
57
58
  return [status, headers, body]
@@ -69,8 +70,12 @@ module Rack
69
70
  def worker
70
71
  loop do
71
72
  begin
72
- puts 'secful: worker start'
73
- env = @queue.pop
73
+ puts 'secful: worker start: ' + @@queue.__id__.to_s
74
+ begin
75
+ env = @@queue.pop_with_timeout(1)
76
+ rescue => e
77
+ puts 'secful: queue-pop-exception: ' + e.message
78
+ end
74
79
  puts 'secful: worker poped'
75
80
  if env
76
81
  puts 'secful: env'
data/lib/utils.rb ADDED
@@ -0,0 +1,36 @@
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
@@ -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.4'
6
+ spec.version = '1.1.5'
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: socket_duplex
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Secful
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-24 00:00:00.000000000 Z
11
+ date: 2016-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -117,6 +117,7 @@ files:
117
117
  - Gemfile
118
118
  - README.md
119
119
  - lib/socket_duplex.rb
120
+ - lib/utils.rb
120
121
  - lib/websocket-client-simple.rb
121
122
  - lib/websocket-client-simple/client.rb
122
123
  - lib/websocket-client-simple/version.rb