Class: Async::Limiter::Concurrent

Inherits:
Object
  • Object
show all
Defined in:
lib/async/limiter/concurrent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit = 1, parent: nil, queue: []) ⇒ Concurrent

Returns a new instance of Concurrent.



11
12
13
14
15
16
17
18
# File 'lib/async/limiter/concurrent.rb', line 11

def initialize(limit = 1, parent: nil, queue: [])
  @count = 0
  @limit = limit
  @waiting = queue
  @parent = parent

  validate!
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



7
8
9
# File 'lib/async/limiter/concurrent.rb', line 7

def count
  @count
end

#limitObject

Returns the value of attribute limit.



9
10
11
# File 'lib/async/limiter/concurrent.rb', line 9

def limit
  @limit
end

Instance Method Details

#acquire(*queue_args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/async/limiter/concurrent.rb', line 39

def acquire(*queue_args)
  wait(*queue_args)
  @count += 1

  return unless block_given?

  begin
    yield
  ensure
    release
  end
end

#async(*queue_args, parent: (@parent || Task.current), **options) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/async/limiter/concurrent.rb', line 24

def async(*queue_args, parent: (@parent || Task.current), **options)
  acquire(*queue_args)
  parent.async(**options) do |task|
    yield task
  ensure
    release
  end
end

#blocking?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/async/limiter/concurrent.rb', line 20

def blocking?
  limit_blocking?
end

#releaseObject



52
53
54
55
56
# File 'lib/async/limiter/concurrent.rb', line 52

def release
  @count -= 1

  resume_waiting
end

#sync(*queue_args) ⇒ Object



33
34
35
36
37
# File 'lib/async/limiter/concurrent.rb', line 33

def sync(*queue_args)
  acquire(*queue_args) do
    yield(@parent || Task.current)
  end
end