Class: Legion::Extensions::Mesh::Helpers::PeerTable

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/mesh/helpers/peer_table.rb

Constant Summary collapse

DEFAULT_TTL =

seconds

60

Instance Method Summary collapse

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

#allObject



26
27
28
# File 'lib/legion/extensions/mesh/helpers/peer_table.rb', line 26

def all
  @mutex.synchronize { @peers.dup }
end

#countObject



44
45
46
# File 'lib/legion/extensions/mesh/helpers/peer_table.rb', line 44

def count
  @mutex.synchronize { @peers.size }
end

#expireObject



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