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.



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

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
  @missed = Zache.new
  @pool.add do
    Endless.new('hungry', log: log).run { pull }
  end
  super(wallets)
end

Instance Method Details

#acq(id, exclusive: false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zold/hungry_wallets.rb', line 56

def acq(id, exclusive: false)
  @wallets.acq(id, exclusive: exclusive) do |wallet|
    unless wallet.exists?
      if @queue.size > 256
        @log.error("Hungry queue is full with #{@queue.size} wallets, can't add #{id}")
      elsif @missed.exists?(id.to_s)
        @log.debug("Hungry queue has seen #{id} just #{Age.new(@missed.mtime(id.to_s))} ago \
(amoung #{@missed.size} others) and it was not found")
      else
        @mutex.synchronize do
          unless @queue.include?(id)
            @missed.put(id.to_s, true, lifetime: 5 * 60)
            @queue << id
            @log.debug("Hungry queue got #{id}, at the pos no.#{@queue.size - 1}")
          end
        end
      end
    end
    yield wallet
  end
end