fix(zaps): single-flight the LNURL endpoint resolver

A zap-receipt burst hands OkHttpLnurlEndpointResolver one resolve() call
per receipt, each on its own coroutine from LocalCache.consume(LnZapEvent).
The resolver's read-through cache only helps once a fetch has landed, so
the whole burst missed together: N receipts for one lightning address made
N requests to that provider's /.well-known/lnurlp/ endpoint. A
lightning-address server observed ~20 per user action, with no zap sent.

Hold one CompletableDeferred per in-flight URL and let the rest await it.
The entry is keyed through LnurlForm.normalizeUrl, matching how
LnurlEndpointCache keys itself, so host case and a trailing slash share a
flight rather than starting two. The winner releases the slot in a finally
after the cache is populated, so a failed fetch is retried by the next
caller instead of being remembered as null, and awaiters are unblocked
even if the winner is cancelled.

The cache itself is unchanged.

Tested with a burst whose callers are released through a shared gate. The
gate is load-bearing: asserting "one fetch" while relying on every coroutine
reaching putIfAbsent before the winner's fetch returns makes a slow machine
fail the test rather than a regression. The burst test failed 20/1 before
this change.
This commit is contained in:
davotoula
2026-08-28 10:37:19 +02:00
parent 97919fd460
commit 319348de9a
2 changed files with 204 additions and 2 deletions
@@ -24,11 +24,14 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.vitorpamplona.quartz.nip57Zaps.validate.LnurlEndpointCache
import com.vitorpamplona.quartz.nip57Zaps.validate.LnurlEndpointInfo
import com.vitorpamplona.quartz.nip57Zaps.validate.LnurlEndpointResolver
import com.vitorpamplona.quartz.nip57Zaps.validate.LnurlForm
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.coroutines.executeAsync
import java.util.concurrent.ConcurrentHashMap
import kotlin.coroutines.cancellation.CancellationException
/**
@@ -40,17 +43,44 @@ import kotlin.coroutines.cancellation.CancellationException
* `/.well-known/lnurlp/<user>` endpoint, parses `nostrPubkey` + `allowsNostr`,
* caches the result, and returns it. Returns null on HTTP / parse failure;
* callers should treat that as "validation unavailable" rather than "invalid".
*
* Fetches are single-flighted per URL. A feed carrying twenty receipts for one
* lightning address hands us twenty concurrent [resolve] calls before the first
* fetch can populate the cache; without this, that is twenty requests to a
* stranger's unthrottled `/.well-known/` endpoint for one user action.
*/
class OkHttpLnurlEndpointResolver(
private val okHttpClient: (String) -> OkHttpClient,
) : LnurlEndpointResolver {
private val mapper = jacksonObjectMapper()
/**
* Fetches in progress, keyed the same way [LnurlEndpointCache] keys itself so
* two spellings of one address share a flight. An entry lives only for the
* duration of its fetch: the winner removes it in a `finally`, so a failed
* fetch is retried by the next caller rather than being remembered as null.
*/
private val inFlight = ConcurrentHashMap<String, CompletableDeferred<LnurlEndpointInfo?>>()
override suspend fun resolve(lnurlpUrl: String): LnurlEndpointInfo? {
LnurlEndpointCache.get(lnurlpUrl)?.let { return it }
val info = fetch(lnurlpUrl) ?: return null
LnurlEndpointCache.put(lnurlpUrl, info)
val key = LnurlForm.normalizeUrl(lnurlpUrl)
val ours = CompletableDeferred<LnurlEndpointInfo?>()
// Whoever wins putIfAbsent owns the fetch; everyone else awaits its result.
inFlight.putIfAbsent(key, ours)?.let { return it.await() }
var info: LnurlEndpointInfo? = null
try {
info = fetch(lnurlpUrl)
if (info != null) LnurlEndpointCache.put(lnurlpUrl, info)
} finally {
// Cache first, then release, so a caller arriving in between reads the
// cache rather than starting a second flight. Non-suspending, so it
// still runs — and still unblocks the awaiters — if we are cancelled.
inFlight.remove(key, ours)
ours.complete(info)
}
return info
}
@@ -0,0 +1,172 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.commons.service.lnurl
import com.vitorpamplona.quartz.nip57Zaps.validate.LnurlEndpointCache
import com.vitorpamplona.quartz.nip57Zaps.validate.LnurlEndpointInfo
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Protocol
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody
import java.util.concurrent.atomic.AtomicInteger
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
/**
* A zap-receipt burst hands the resolver N concurrent calls for the same
* lightning address. Each one is meant to await a single fetch, not start its
* own — the endpoint the fetch hits belongs to somebody else's server.
*/
class OkHttpLnurlEndpointResolverTest {
private val url = "https://example.com/.well-known/lnurlp/vitor"
/** Counts fetches and holds each one open long enough for the burst to pile up behind it. */
private class CountingInterceptor(
val calls: AtomicInteger = AtomicInteger(0),
private val body: String = """{"nostrPubkey":"aabbcc","allowsNostr":true}""",
private val delayMs: Long = 200,
private val failFirst: Int = 0,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val n = calls.incrementAndGet()
Thread.sleep(delayMs)
if (n <= failFirst) {
return Response
.Builder()
.request(chain.request())
.protocol(Protocol.HTTP_1_1)
.code(503)
.message("Service Unavailable")
.body("".toResponseBody("text/plain".toMediaType()))
.build()
}
return Response
.Builder()
.request(chain.request())
.protocol(Protocol.HTTP_1_1)
.code(200)
.message("OK")
.body(body.toResponseBody("application/json".toMediaType()))
.build()
}
}
private fun resolverWith(interceptor: Interceptor): OkHttpLnurlEndpointResolver {
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()
return OkHttpLnurlEndpointResolver { client }
}
/**
* Releases [n] callers at once and collects what they got.
*
* The gate matters: the assertion under test is "these callers were all in
* flight together", and without it a straggler that arrives after the
* winner's fetch has already returned legitimately starts a second one. That
* would be a slow test machine failing the test, not a regression.
*/
private suspend fun burst(
n: Int,
call: suspend (Int) -> LnurlEndpointInfo?,
): List<LnurlEndpointInfo?> =
coroutineScope {
val gate = CompletableDeferred<Unit>()
val callers =
(0 until n).map { i ->
async(Dispatchers.IO) {
gate.await()
call(i)
}
}
gate.complete(Unit)
callers.awaitAll()
}
@BeforeTest
fun clearCache() {
LnurlEndpointCache.clear()
}
@Test
fun concurrentCallersForTheSameUrlShareOneFetch() =
runBlocking {
val interceptor = CountingInterceptor()
val resolver = resolverWith(interceptor)
val results = burst(20) { resolver.resolve(url) }
assertEquals(1, interceptor.calls.get(), "20 concurrent callers must share one fetch")
assertEquals(20, results.size)
assertTrue(results.all { it != null && it == results.first() }, "every caller gets the same result")
}
@Test
fun aFailedFetchIsNotRememberedAndTheNextCallerRetries() =
runBlocking {
// The server is down for the first burst. Nothing may be cached from
// that — otherwise one bad minute poisons the address until eviction.
val interceptor = CountingInterceptor(failFirst = 1)
val resolver = resolverWith(interceptor)
val firstBurst = burst(5) { resolver.resolve(url) }
assertEquals(1, interceptor.calls.get(), "the failing burst is one fetch, not five")
assertTrue(firstBurst.all { it == null }, "a 503 resolves to null, not a cached verdict")
val retry = resolver.resolve(url)
assertEquals(2, interceptor.calls.get(), "the next caller retries a failed address")
assertNotNull(retry, "the retry sees the recovered server")
resolver.resolve(url)
assertEquals(2, interceptor.calls.get(), "and the success is cached from then on")
}
@Test
fun twoSpellingsOfOneAddressShareOneFetch() =
runBlocking {
// LnurlEndpointCache keys case-insensitively on host and ignores a
// trailing slash. The in-flight map has to agree, or a burst that
// spells the host two ways is two stampedes instead of one.
val interceptor = CountingInterceptor(delayMs = 200)
val resolver = resolverWith(interceptor)
val spellings =
listOf(
"https://example.com/.well-known/lnurlp/vitor",
"https://EXAMPLE.com/.well-known/lnurlp/vitor",
"https://example.com/.well-known/lnurlp/vitor/",
)
val results = burst(12) { i -> resolver.resolve(spellings[i % 3]) }
assertEquals(1, interceptor.calls.get(), "host case and a trailing slash are the same address")
assertTrue(results.all { it != null && it == results.first() })
}
}