upfluence-utils 0.4.1 → 0.5.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
  SHA1:
3
- metadata.gz: 00e955821e96a0584a8a2cb52a66a0c39cb0309b
4
- data.tar.gz: c5556e3c4b059b5c93563e43f7f7186a3903a69e
3
+ metadata.gz: c148d5d8756d1d0eefd36e85cba488b5df5e846d
4
+ data.tar.gz: f6b03fef67c6c7735670556db8a3f09e6759f54f
5
5
  SHA512:
6
- metadata.gz: 46b20f95725621f2624c959e58eb1c03fd4fa94c208c7ef3a1c5c7f26e03299a011858b891e0dbcb7ed16785c3bf5c535d5d12dcb3d1f289e69c3d369b5cd9fe
7
- data.tar.gz: c933ce9a3bf601cd73f2a707ae34e7fd60a6e76d92f8edfb44f8f4148f91a51d453c0b636913c30bd47cf24c8c4086b4f6342df660a4b347092d254b7e4d4424
6
+ metadata.gz: 18b5549fd9a5069213fe0c1276f75a17e46d5a8b6e416d2abf25b1779d3b6cca79c8088f8d8b4f841e662797431071f6d771dd620d0220b1c323ddba121b4ca3
7
+ data.tar.gz: 1af342090c2066769a63e774518b66cdd498196012c404e3bb8077ea38df407a8a4d0fc6ea71d0059678081015aec0d90ef0a5ab968c23a4978117d24a4b38ba
@@ -23,7 +23,7 @@ module Upfluence
23
23
  begin
24
24
  Raven.capture_exception(
25
25
  error,
26
- extra: { method: method, arguments: args },
26
+ extra: { method: method, arguments: args.map(&:inspect) },
27
27
  tags: { method: method }
28
28
  )
29
29
  rescue Raven::Error => e
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Upfluence
2
2
  module Utils
3
- VERSION = '0.4.1'.freeze
3
+ VERSION = '0.5.0'.freeze
4
4
  end
5
5
  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.1
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-13 00:00:00.000000000 Z
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