Class: Zold::HungryWallets

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/zold/hungry_wallets.rb

Overview

Wallets decorator that adds missing wallets to the queue to be pulled later.

Instance Method Summary collapse

Constructor Details

#initialize(wallets, remotes, copies, pool, log: Log::NULL, network: 'test') ⇒ HungryWallets

Returns a new instance of HungryWallets.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zold/hungry_wallets.rb', line 37

def initialize(wallets, remotes, copies, pool,
  log: Log::NULL, network: 'test')
  @wallets = wallets
  @remotes = remotes
  @copies = copies
  @log = log
  @network = network
  @pool = pool
  @queue = []
  @mutex = Mutex.new
  @pool.add do
    Endless.new('hungry', log: log).run { pull }
  end
  super(wallets)
end

Instance Method Details

#acq(id, exclusive: false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/zold/hungry_wallets.rb', line 53

def acq(id, exclusive: false)
  @wallets.acq(id, exclusive: exclusive) do |wallet|
    if @queue.size > 256
      @log.error("Hungry queue is full with #{@queue.size} wallets, can't add #{id}")
    else
      @mutex.synchronize do
        unless @queue.include?(id)
          @queue << id
          @log.debug("Hungry queue got #{id}, at the pos no.#{@queue.size - 1}")
        end
      end
    end
    yield wallet
  end
end