Class: RubyLlmMesh::Mesh::PeerRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm_mesh/mesh/peer_registry.rb

Overview

Registry of local LLM peer base URLs (Ollama, LM Studio, etc.).

Defined Under Namespace

Classes: Peer

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: RubyLlmMesh.configuration) ⇒ PeerRegistry

Returns a new instance of PeerRegistry.



19
20
21
22
23
24
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 19

def initialize(config: RubyLlmMesh.configuration)
  @config = config
  @mutex = Mutex.new
  @peers = {}
  seed_from_config!
end

Class Method Details

.instance(config: RubyLlmMesh.configuration) ⇒ Object



10
11
12
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 10

def instance(config: RubyLlmMesh.configuration)
  @instance ||= new(config: config)
end

.reset!Object



14
15
16
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 14

def reset!
  @instance = nil
end

Instance Method Details

#all_urlsObject



69
70
71
72
73
74
75
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 69

def all_urls
  seed_from_config!
  @mutex.synchronize do
    keys = @peers.keys.dup
    keys.empty? ? peer_urls : keys
  end
end

#clear!Object



88
89
90
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 88

def clear!
  @mutex.synchronize { @peers.clear }
end

#each_peerObject



83
84
85
86
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 83

def each_peer
  seed_from_config!
  @mutex.synchronize { @peers.values.map(&:dup) }.each { |peer| yield peer }
end

#enabled?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 26

def enabled?
  !!@config.peer_discovery_enabled || !peer_urls.empty?
end

#healthy_urlsObject



59
60
61
62
63
64
65
66
67
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 59

def healthy_urls
  seed_from_config!
  @mutex.synchronize do
    # Only return peers currently marked healthy. When every peer is
    # unhealthy, return [] so callers (e.g. LocalNode) can apply their
    # own last-resort fallback — never reintroduce unhealthy URLs.
    @peers.values.select(&:healthy).map(&:url)
  end
end

#mark_healthy(url) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 43

def mark_healthy(url)
  update_peer(url) do |peer|
    peer.healthy = true
    peer.last_error = nil
    peer.last_checked_at = Time.now
  end
end

#mark_unhealthy(url, error: nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 51

def mark_unhealthy(url, error: nil)
  update_peer(url) do |peer|
    peer.healthy = false
    peer.last_error = error&.to_s
    peer.last_checked_at = Time.now
  end
end

#peer_urlsObject



77
78
79
80
81
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 77

def peer_urls
  urls = Array(@config.peer_urls).map { |u| normalize_url(u) }.reject(&:empty?)
  primary = normalize_url(@config.local_node_base_url)
  ([primary] + urls).uniq
end

#register(url) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 30

def register(url)
  normalized = normalize_url(url)
  return if normalized.empty?

  @mutex.synchronize do
    @peers[normalized] ||= Peer.new(url: normalized, healthy: true, last_checked_at: nil, last_error: nil)
  end
end

#unregister(url) ⇒ Object



39
40
41
# File 'lib/ruby_llm_mesh/mesh/peer_registry.rb', line 39

def unregister(url)
  @mutex.synchronize { @peers.delete(normalize_url(url)) }
end