Class: Legion::Extensions::Mesh::Helpers::PeerTable
- Inherits:
-
Object
- Object
- Legion::Extensions::Mesh::Helpers::PeerTable
- Defined in:
- lib/legion/extensions/mesh/helpers/peer_table.rb
Constant Summary collapse
- DEFAULT_TTL =
seconds
60
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
- #expire ⇒ Object
- #get(agent_id) ⇒ Object
-
#initialize(ttl: DEFAULT_TTL) ⇒ PeerTable
constructor
A new instance of PeerTable.
- #remove(agent_id) ⇒ Object
- #upsert(agent_id, data = {}) ⇒ Object
Constructor Details
#initialize(ttl: DEFAULT_TTL) ⇒ PeerTable
Returns a new instance of PeerTable.
10 11 12 13 14 |
# File 'lib/legion/extensions/mesh/helpers/peer_table.rb', line 10 def initialize(ttl: DEFAULT_TTL) @ttl = ttl @peers = {} @mutex = Mutex.new end |
Instance Method Details
#all ⇒ Object
26 27 28 |
# File 'lib/legion/extensions/mesh/helpers/peer_table.rb', line 26 def all @mutex.synchronize { @peers.dup } end |
#count ⇒ Object
44 45 46 |
# File 'lib/legion/extensions/mesh/helpers/peer_table.rb', line 44 def count @mutex.synchronize { @peers.size } end |
#expire ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/legion/extensions/mesh/helpers/peer_table.rb', line 30 def expire cutoff = Time.now.utc - @ttl expired = [] @mutex.synchronize do @peers.each do |id, entry| next unless entry[:last_seen_at] < cutoff expired << id end expired.each { |id| @peers.delete(id) } end expired end |
#get(agent_id) ⇒ Object
22 23 24 |
# File 'lib/legion/extensions/mesh/helpers/peer_table.rb', line 22 def get(agent_id) @mutex.synchronize { @peers[agent_id] } end |
#remove(agent_id) ⇒ Object
48 49 50 |
# File 'lib/legion/extensions/mesh/helpers/peer_table.rb', line 48 def remove(agent_id) @mutex.synchronize { @peers.delete(agent_id) } end |
#upsert(agent_id, data = {}) ⇒ Object
16 17 18 19 20 |
# File 'lib/legion/extensions/mesh/helpers/peer_table.rb', line 16 def upsert(agent_id, data = {}) @mutex.synchronize do @peers[agent_id] = data.merge(last_seen_at: Time.now.utc) end end |