Prevent queue buildup and expire abandoned NIP-46 listeners
This commit is contained in:
7 files changed
+139
-38
No files matched your search
@@ -93,7 +93,6 @@ module Users
|
||||
existing = NostrSignerSession.find_by_token(session[:nostr_connect_token])
|
||||
|
||||
if existing && existing.flow == "login" && (existing.pending? || existing.verified?)
|
||||
NostrConnectListenerJob.perform_later(existing.id) if existing.pending?
|
||||
return existing
|
||||
end
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ module NostrManager
|
||||
Result = Struct.new(:url, :message, keyword_init: true)
|
||||
|
||||
class TimeoutError < StandardError; end
|
||||
class InterruptedError < StandardError; end
|
||||
|
||||
DEFAULT_TIMEOUT = 30
|
||||
DEFAULT_CONNECT_TIMEOUT = 5
|
||||
@@ -31,6 +32,8 @@ module NostrManager
|
||||
raise TimeoutError, "relay operation timed out"
|
||||
end
|
||||
|
||||
attr_reader :task
|
||||
|
||||
def initialize(relays:, task:, connect_timeout: DEFAULT_CONNECT_TIMEOUT, logger: Rails.logger)
|
||||
@relays = Array(relays).map(&:to_s).reject(&:blank?).uniq
|
||||
@task = task
|
||||
@@ -41,6 +44,7 @@ module NostrManager
|
||||
@inbox = Async::Queue.new
|
||||
@ready_connections = 0
|
||||
@closed = false
|
||||
@interrupted = false
|
||||
end
|
||||
|
||||
def start
|
||||
@@ -94,16 +98,35 @@ module NostrManager
|
||||
deadline = monotonic_now + timeout
|
||||
|
||||
loop do
|
||||
raise InterruptedError, "relay wait interrupted" if @interrupted
|
||||
|
||||
remaining = deadline - monotonic_now
|
||||
raise TimeoutError, "timed out waiting for a relay message" if remaining <= 0
|
||||
|
||||
result = @task.with_timeout(remaining) { @inbox.dequeue }
|
||||
raise InterruptedError, "relay wait interrupted" if @interrupted
|
||||
|
||||
return result if yield(result)
|
||||
end
|
||||
rescue Async::TimeoutError
|
||||
raise TimeoutError, "timed out waiting for a relay message"
|
||||
end
|
||||
|
||||
def closed?
|
||||
@closed
|
||||
end
|
||||
|
||||
def interrupted?
|
||||
@interrupted
|
||||
end
|
||||
|
||||
def interrupt!
|
||||
return if @interrupted || @closed
|
||||
|
||||
@interrupted = true
|
||||
@inbox.enqueue(:interrupted)
|
||||
end
|
||||
|
||||
def wait_for_connection(timeout: @connect_timeout)
|
||||
deadline = monotonic_now + timeout
|
||||
@task.sleep(0.01) while @ready_connections.zero? && monotonic_now < deadline
|
||||
|
||||
@@ -16,46 +16,54 @@ module NostrManager
|
||||
timeout: timeout,
|
||||
connect_timeout: 10
|
||||
) do |pool|
|
||||
pool.subscribe(
|
||||
Nostr::Filter.new(
|
||||
kinds: [KIND],
|
||||
p: [@session.client_public_key],
|
||||
since: @session.created_at.to_i - 60
|
||||
)
|
||||
)
|
||||
watchdog = start_watchdog(pool)
|
||||
|
||||
client = Client.new(
|
||||
pool: pool,
|
||||
client_private_key: @session.client_private_key,
|
||||
client_public_key: @session.client_public_key,
|
||||
remote_signer_pubkey: @session.remote_signer_pubkey
|
||||
)
|
||||
|
||||
if @session.bunker?
|
||||
client.connect_bunker(
|
||||
remote_signer_pubkey: @session.remote_signer_pubkey,
|
||||
secret: @session.secret,
|
||||
perms: PERMS,
|
||||
metadata: client_metadata,
|
||||
timeout: 30
|
||||
begin
|
||||
pool.subscribe(
|
||||
Nostr::Filter.new(
|
||||
kinds: [KIND],
|
||||
p: [@session.client_public_key],
|
||||
since: @session.created_at.to_i - 60
|
||||
)
|
||||
)
|
||||
else
|
||||
client.await_nostrconnect(secret: @session.secret, timeout: timeout)
|
||||
|
||||
client = Client.new(
|
||||
pool: pool,
|
||||
client_private_key: @session.client_private_key,
|
||||
client_public_key: @session.client_public_key,
|
||||
remote_signer_pubkey: @session.remote_signer_pubkey
|
||||
)
|
||||
|
||||
if @session.bunker?
|
||||
client.connect_bunker(
|
||||
remote_signer_pubkey: @session.remote_signer_pubkey,
|
||||
secret: @session.secret,
|
||||
perms: PERMS,
|
||||
metadata: client_metadata,
|
||||
timeout: 30
|
||||
)
|
||||
else
|
||||
client.await_nostrconnect(secret: @session.secret, timeout: timeout)
|
||||
end
|
||||
|
||||
user_pubkey = client.get_public_key
|
||||
signed_auth = client.sign_event(
|
||||
auth_template,
|
||||
timeout: 90,
|
||||
on_auth_url: ->(url) { @session.update!(auth_url: url) }
|
||||
)
|
||||
|
||||
verify_auth_event!(signed_auth, user_pubkey)
|
||||
fresh_session&.complete_connection!(
|
||||
remote_signer_pubkey: client.remote_signer_pubkey,
|
||||
user_pubkey: user_pubkey
|
||||
)
|
||||
ensure
|
||||
watchdog&.stop
|
||||
end
|
||||
|
||||
user_pubkey = client.get_public_key
|
||||
signed_auth = client.sign_event(
|
||||
auth_template,
|
||||
timeout: 90,
|
||||
on_auth_url: ->(url) { @session.update!(auth_url: url) }
|
||||
)
|
||||
|
||||
verify_auth_event!(signed_auth, user_pubkey)
|
||||
fresh_session&.complete_connection!(
|
||||
remote_signer_pubkey: client.remote_signer_pubkey,
|
||||
user_pubkey: user_pubkey
|
||||
)
|
||||
end
|
||||
rescue NostrManager::Relay::Pool::InterruptedError
|
||||
nil
|
||||
rescue NostrManager::Relay::Pool::TimeoutError
|
||||
fresh_session&.expire!
|
||||
rescue UnsupportedTransportError, NostrManager::Nip44::UnsupportedTransportError => e
|
||||
@@ -69,6 +77,34 @@ module NostrManager
|
||||
|
||||
private
|
||||
|
||||
def start_watchdog(pool)
|
||||
session_id = @session.id
|
||||
|
||||
pool.task.async do
|
||||
loop do
|
||||
pool.task.sleep(10)
|
||||
break if pool.closed? || pool.interrupted?
|
||||
|
||||
current = NostrSignerSession.find_by(id: session_id)
|
||||
|
||||
unless listener_still_wanted?(current)
|
||||
current&.expire! if current&.pending?
|
||||
pool.interrupt!
|
||||
break
|
||||
end
|
||||
end
|
||||
rescue StandardError => e
|
||||
Rails.logger.info("[nostr][signer] listener watchdog stopped: #{e.class}: #{e.message}")
|
||||
end
|
||||
end
|
||||
|
||||
def listener_still_wanted?(current)
|
||||
return false if current.nil? || !current.pending?
|
||||
|
||||
reference = current.last_status_poll_at || current.created_at
|
||||
reference > 60.seconds.ago
|
||||
end
|
||||
|
||||
def fresh_session
|
||||
NostrSignerSession.find_by(id: @session.id)
|
||||
end
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ development:
|
||||
- queues: [default, nostr]
|
||||
threads: 1
|
||||
- queues: [nostr_connect, nostr_rpc]
|
||||
threads: 2
|
||||
threads: 4
|
||||
|
||||
test:
|
||||
<<: *default
|
||||
|
||||
@@ -21,6 +21,15 @@ RSpec.describe "Users::NostrConnects", type: :request do
|
||||
expect(response.body).to include("nostrconnect://")
|
||||
expect(NostrSignerSession.last.flow).to eq("login")
|
||||
end
|
||||
|
||||
it "does not enqueue another listener when the page is reloaded" do
|
||||
get nostr_connect_path
|
||||
clear_enqueued_jobs
|
||||
|
||||
expect do
|
||||
get nostr_connect_path
|
||||
end.not_to have_enqueued_job(NostrConnectListenerJob)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /users/nostr_connect/status" do
|
||||
|
||||
@@ -100,4 +100,27 @@ RSpec.describe NostrManager::RemoteSigner::ListenForConnection, type: :model do
|
||||
expect(session).to be_failed
|
||||
expect(session.error).to include("NIP-04")
|
||||
end
|
||||
|
||||
describe "the listener watchdog" do
|
||||
it "keeps waiting while the browser is polling" do
|
||||
session = build_session
|
||||
session.update!(last_status_poll_at: 5.seconds.ago)
|
||||
|
||||
expect(described_class.new(session: session).send(:listener_still_wanted?, session)).to be(true)
|
||||
end
|
||||
|
||||
it "stops when the browser has not polled for a minute" do
|
||||
session = build_session
|
||||
session.update!(last_status_poll_at: 61.seconds.ago)
|
||||
|
||||
expect(described_class.new(session: session).send(:listener_still_wanted?, session)).to be(false)
|
||||
end
|
||||
|
||||
it "stops when the session is no longer pending" do
|
||||
session = build_session
|
||||
session.update!(status: "verified")
|
||||
|
||||
expect(described_class.new(session: session).send(:listener_still_wanted?, session)).to be(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -32,6 +32,17 @@ class FakeSignerPool
|
||||
)
|
||||
end
|
||||
|
||||
def task
|
||||
@task ||= Object.new.tap do |task|
|
||||
def task.async
|
||||
nil
|
||||
end
|
||||
|
||||
def task.sleep(_seconds)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def signer_pubkey
|
||||
@signer.keypair.public_key.to_s
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user