xlogin 0.14.5 → 0.15.0

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: 8a6aa103f9d9af99c52b9fc954ad2f7df5b21348b6f4c54f6b2b22410949688f
4
- data.tar.gz: 91c82b18fd5dd3c3f1015ead5c52f159be0d8fb7b442e9f01dc2fb674c594f03
3
+ metadata.gz: cda6f2be936220ffb49b9d586d0e8ebcd248194bb0d16b7bc595ab3209da42da
4
+ data.tar.gz: 484a16a9c5f1e3eaa9d65f85de52e634ffb29a6b4e66f5461385c1f29c115ae6
5
5
  SHA512:
6
- metadata.gz: 5fad4a763c96dd13513386c9d63560684012706c598823f66124796b79a0b1f09f3a265cbbfa4ae8fbad849f5b2ad52479fb9c2ecd4e55d083383eb4ed92e5f2
7
- data.tar.gz: 6b6e523c0bbf6a59468c16cf38370cf3b590b53890ba953c1d151c2cb881d512c3c485ae86719541346c9b11824cfec03fccf6b42526cb92b0188a0c6f71eca9
6
+ metadata.gz: 0562b0ad712a00b3b2dd8d36b43c3fc15f1435492f9c025e374c52a1a92495575667476dfe1e679a57a6f396b5eba0bad2341d56c0a36454e70ff7737c656ac8
7
+ data.tar.gz: 716231e8c898ebb0d10b85a58c422f9815361e9c4303c803f2ca40c80fd83e957cd135b3f2db920ec12468e04e42d25f3c5b0213120e149451a62535b218b71b
@@ -40,15 +40,13 @@ module Xlogin
40
40
  return session unless block
41
41
  begin block.call(session) ensure session.close end
42
42
  end
43
- alias_method :create, :get
44
43
 
45
- def get_pool(args, **opts, &block)
44
+ def pool(args, **opts, &block)
46
45
  pool = factory.build_pool(args, **opts)
47
46
 
48
47
  return pool unless block
49
48
  begin block.call(pool) ensure pool.close end
50
49
  end
51
- alias_method :create_pool, :get_pool
52
50
 
53
51
  def configure(&block)
54
52
  instance_eval(&block)
@@ -13,7 +13,7 @@ module Xlogin
13
13
  def initialize
14
14
  @inventory = Hash.new
15
15
  @templates = Hash.new
16
- @gateways = Hash.new
16
+ @tunnels = Hash.new
17
17
  @mutex = Mutex.new
18
18
  end
19
19
 
@@ -54,32 +54,40 @@ module Xlogin
54
54
  @templates.keys
55
55
  end
56
56
 
57
- def open_tunnel(tunnel, host, port)
57
+ def open_tunnel(name, host, port)
58
58
  @mutex.synchronize do
59
- unless @gateways[tunnel]
60
- gateway_uri = Addressable::URI.parse(tunnel)
61
- case gateway_uri.scheme
59
+ unless @tunnels[name]
60
+ uri = Addressable::URI.parse(name)
61
+ case uri.scheme
62
62
  when 'ssh'
63
- username, password = *gateway_uri.userinfo.split(':')
64
- @gateways[tunnel] = Net::SSH::Gateway.new(
65
- gateway_uri.host,
63
+ username, password = *uri.userinfo.split(':')
64
+ gateway = Net::SSH::Gateway.new(
65
+ uri.host,
66
66
  username,
67
67
  password: password,
68
- port: gateway_uri.port || 22
68
+ port: uri.port || 22
69
69
  )
70
+
71
+ @tunnels[name] = Struct.new('Tunnel', :gateway, :ports).new(gateway, [])
70
72
  end
71
73
  end
72
74
 
73
- gateway = @gateways[tunnel]
74
- return host, port unless gateway
75
- return '127.0.0.1', gateway.open(host, port)
75
+ if tunnel = @tunnels[name]
76
+ port = tunnel.gateway.open(host, port)
77
+ host = '127.0.0.1'
78
+ tunnel.ports << port
79
+ end
80
+ return host, port
76
81
  end
77
82
  end
78
83
 
79
- def close_tunnel(tunnel, port)
84
+ def close_tunnel(name, port)
80
85
  @mutex.synchronize do
81
- gateway = @gateways[tunnel]
82
- gateway.close(port) if gateway
86
+ if tunnel = @tunnels[name]
87
+ tunnel.ports.delete(port)
88
+ tunnel.gateway.close(port)
89
+ tunnel.gateway.shutdown! if tunnel.ports.empty?
90
+ end
83
91
  end
84
92
  end
85
93
 
@@ -13,20 +13,12 @@ module Xlogin
13
13
  @args = args
14
14
  @opts = opts
15
15
 
16
- @size = DEFAULT_POOL_SIZE
17
- @idle = DEFAULT_POOL_IDLE
16
+ @size = @opts.delete(:pool_size) || DEFAULT_POOL_SIZE
17
+ @idle = @opts.delete(:pool_idle) || DEFAULT_POOL_IDLE
18
18
 
19
19
  @mutex = Mutex.new
20
20
  @queue = Queue.new
21
- @created = 0
22
- end
23
-
24
- def size=(val)
25
- @mutex.synchronize{ @size = val }
26
- end
27
-
28
- def idle=(val)
29
- @mutex.synchronize{ @idle = val }
21
+ @count = 0
30
22
  end
31
23
 
32
24
  def with
@@ -38,20 +30,22 @@ module Xlogin
38
30
 
39
31
  def close
40
32
  until @queue.empty?
41
- session, _ = @queue.deq
33
+ session, _, _ = @queue.deq
42
34
  destroy(session)
43
35
  end
44
36
  end
45
37
 
46
38
  def deq
47
39
  @mutex.synchronize do
48
- if @queue.empty? && @created < @size
49
- @created += 1
40
+ if @queue.empty? && @count < @size
41
+ @count += 1
50
42
  return Xlogin.get(@args, **@opts)
51
43
  end
52
44
  end
53
45
 
54
- session, last_used = @queue.deq
46
+ session, last_used, watch_dog = @queue.deq
47
+
48
+ watch_dog.kill
55
49
  if Time.now - last_used > @idle
56
50
  destroy(session)
57
51
  return deq
@@ -59,23 +53,24 @@ module Xlogin
59
53
 
60
54
  begin
61
55
  raise IOError if session&.sock&.closed?
56
+ return session
62
57
  rescue IOError, EOFError, Errno::ECONNABORTED, Errno::ECONNREFUSED, Errno::ECONNRESET
63
58
  destroy(session)
64
59
  return deq
65
60
  end
66
-
67
- session
68
61
  end
69
62
 
70
63
  def enq(session)
71
64
  last_used = Time.now
72
- @queue.enq [session, last_used]
65
+ watch_dog = Thread.new(session){ |s| sleep(@idle * 1.5) && s.close rescue nil }
66
+ @queue.enq [session, last_used, watch_dog]
73
67
  end
74
68
 
69
+ private
75
70
  def destroy(session)
76
71
  @mutex.synchronize do
77
72
  session.close rescue nil
78
- @created -= 1
73
+ @count -= 1
79
74
  end
80
75
  end
81
76
 
@@ -1,3 +1,3 @@
1
1
  module Xlogin
2
- VERSION = "0.14.5"
2
+ VERSION = "0.15.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xlogin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.5
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-18 00:00:00.000000000 Z
11
+ date: 2020-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-telnet