upfluence-utils 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/upfluence/error_logger/sentry.rb +1 -1
- data/lib/upfluence/pool.rb +79 -0
- data/lib/upfluence/utils/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c148d5d8756d1d0eefd36e85cba488b5df5e846d
|
4
|
+
data.tar.gz: f6b03fef67c6c7735670556db8a3f09e6759f54f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18b5549fd9a5069213fe0c1276f75a17e46d5a8b6e416d2abf25b1779d3b6cca79c8088f8d8b4f841e662797431071f6d771dd620d0220b1c323ddba121b4ca3
|
7
|
+
data.tar.gz: 1af342090c2066769a63e774518b66cdd498196012c404e3bb8077ea38df407a8a4d0fc6ea71d0059678081015aec0d90ef0a5ab968c23a4978117d24a4b38ba
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'timeout'
|
3
|
+
|
4
|
+
module Upfluence
|
5
|
+
class Pool
|
6
|
+
class UnknownResource < RuntimeError; end
|
7
|
+
class NoInstanciationBlock < RuntimeError; end
|
8
|
+
|
9
|
+
attr_reader :max
|
10
|
+
|
11
|
+
def initialize(size = 0, options = {}, &block)
|
12
|
+
raise NoInstanciationBlock unless block
|
13
|
+
|
14
|
+
@create_block = block
|
15
|
+
@redeemed = []
|
16
|
+
@que = []
|
17
|
+
@max = size
|
18
|
+
@mutex = Mutex.new
|
19
|
+
@resource = ConditionVariable.new
|
20
|
+
@shutdown_block = nil
|
21
|
+
@timeout = options.fetch :timeout, 5
|
22
|
+
end
|
23
|
+
|
24
|
+
def discard(obj)
|
25
|
+
@mutex.synchronize do
|
26
|
+
raise UnknownResource unless @redeemed.include? obj
|
27
|
+
|
28
|
+
@redeemed.reject! { |e| e == obj }
|
29
|
+
@resource.broadcast
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def push(obj)
|
34
|
+
@mutex.synchronize do
|
35
|
+
raise UnknownResource unless @redeemed.include? obj
|
36
|
+
|
37
|
+
@redeemed.reject! { |e| e == obj }
|
38
|
+
@que.push obj
|
39
|
+
@resource.broadcast
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def pop(options = {})
|
44
|
+
timeout = options.fetch :timeout, @timeout
|
45
|
+
deadline = Time.now + timeout
|
46
|
+
|
47
|
+
@mutex.synchronize do
|
48
|
+
loop do
|
49
|
+
return @que.pop unless @que.empty?
|
50
|
+
|
51
|
+
connection = try_create(options)
|
52
|
+
return connection if connection
|
53
|
+
|
54
|
+
to_wait = deadline - Time.now
|
55
|
+
raise Timeout::Error, "Waited #{timeout} sec" if to_wait <= 0
|
56
|
+
@resource.wait(@mutex, to_wait)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def empty?
|
62
|
+
(@redeemed.length - @que.length) >= @max
|
63
|
+
end
|
64
|
+
|
65
|
+
def length
|
66
|
+
@max - @redeemed.length + @que.length
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def try_create(_options = nil)
|
72
|
+
unless @redeemed.length >= @max
|
73
|
+
object = @create_block.call
|
74
|
+
@redeemed << object
|
75
|
+
object
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: upfluence-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Upfluence
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -245,6 +245,7 @@ files:
|
|
245
245
|
- lib/upfluence/http/server.rb
|
246
246
|
- lib/upfluence/logger.rb
|
247
247
|
- lib/upfluence/mixin/strong_parameters.rb
|
248
|
+
- lib/upfluence/pool.rb
|
248
249
|
- lib/upfluence/utils.rb
|
249
250
|
- lib/upfluence/utils/http/middleware/null.rb
|
250
251
|
- lib/upfluence/utils/thrift.rb
|