discourse/lib/message_bus_diags.rb
Sam Saffron 28292d2759
PERF: avoid shelling to get hostname aggressively
Previously we had many places in the app that called `hostname` to get
hostname of a server. This commit replaces the pattern in 2 ways

1. We cache the result in `Discourse.os_hostname` so it is only ever called once

2. We prefer to use Socket.gethostname which avoids making a shell command

This improves performance as we are not spawning hostname processes throughout
the app lifetime
2020-02-18 15:13:19 +11:00

35 lines
680 B
Ruby

# frozen_string_literal: true
class MessageBusDiags
@host_info = {}
def self.my_id
@my_id ||= "#{Discourse.os_hostname}-#{Process.pid}"
end
def self.seen_host(name)
@host_info[name] = DateTime.now
end
def self.establish_peer_names
MessageBus.publish "/server-name", channel: "/server-name-reply/#{my_id}"
end
def self.seen_hosts
@host_info
end
unless @subscribed
MessageBus.subscribe "/server-name-reply/#{my_id}" do |msg|
MessageBusDiags.seen_host(msg.data)
end
MessageBus.subscribe "/server-name" do |msg|
MessageBus.publish msg.data["channel"], MessageBusDiags.my_id
end
@subscribed = true
end
end