CI / Test (pull_request) Successful in 45s
- Track EOSE with a done flag so a healthy relay with no matching events returns immediately instead of waiting out the timeout - Restore the ensure around the client thread cleanup - Raise the metadata fetch client timeout to 60s to cover the worst-case sequential discovery on the server
68 lines
1.6 KiB
Ruby
68 lines
1.6 KiB
Ruby
module NostrManager
|
|
class FetchEvent < NostrManagerService
|
|
TIMEOUT = 5
|
|
|
|
def initialize(filter:, relay_url:)
|
|
@filter = filter
|
|
@relay = new_relay(relay_url)
|
|
@client = Nostr::Client.new
|
|
end
|
|
|
|
def call
|
|
filter, client, relay = @filter, @client, @relay
|
|
event = nil
|
|
done = false
|
|
errored = false
|
|
mutex = Mutex.new
|
|
received_event = ConditionVariable.new
|
|
log_prefix = "[nostr][#{@relay.name}]"
|
|
|
|
thread = Thread.new do
|
|
client.on :connect do
|
|
client.subscribe(filter: filter)
|
|
end
|
|
|
|
client.on :error do |e|
|
|
Rails.logger.info "#{log_prefix} Error: #{e}"
|
|
mutex.synchronize do
|
|
errored = true
|
|
received_event.signal
|
|
end
|
|
end
|
|
|
|
client.on :message do |m|
|
|
msg = JSON.parse(m) rescue nil
|
|
if msg && msg[0] == "EVENT" && msg[2]
|
|
Rails.logger.debug "#{log_prefix} Event received: #{msg[2]["id"]}"
|
|
mutex.synchronize do
|
|
event = msg[2]
|
|
received_event.signal
|
|
end
|
|
elsif msg && msg[0] == "EOSE"
|
|
mutex.synchronize do
|
|
done = true
|
|
received_event.signal
|
|
end
|
|
end
|
|
end
|
|
|
|
client.connect relay
|
|
end
|
|
|
|
begin
|
|
mutex.synchronize do
|
|
received_event.wait(mutex, TIMEOUT) if event.nil? && !done && !errored
|
|
end
|
|
|
|
if event.nil? && !done && !errored
|
|
Rails.logger.debug "#{log_prefix} Timeout: No event received within #{TIMEOUT} seconds"
|
|
end
|
|
ensure
|
|
thread.exit
|
|
end
|
|
|
|
event
|
|
end
|
|
end
|
|
end
|