mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-27 23:06:24 +02:00
Adds an extra layer on the url normalization cache to avoid recomputing failed attempts before.
This commit is contained in:
@@ -24,9 +24,18 @@ import android.util.Log
|
|||||||
import androidx.collection.LruCache
|
import androidx.collection.LruCache
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import org.czeal.rfc3986.URIReference
|
import org.czeal.rfc3986.URIReference
|
||||||
|
import java.lang.IllegalArgumentException
|
||||||
import kotlin.contracts.ExperimentalContracts
|
import kotlin.contracts.ExperimentalContracts
|
||||||
|
|
||||||
val normalizedUrls = LruCache<String, NormalizedRelayUrl>(5000)
|
sealed interface NormalizationResult {
|
||||||
|
class Sucess(
|
||||||
|
val url: NormalizedRelayUrl,
|
||||||
|
) : NormalizationResult
|
||||||
|
|
||||||
|
object Error : NormalizationResult
|
||||||
|
}
|
||||||
|
|
||||||
|
val normalizedUrls = LruCache<String, NormalizationResult>(5000)
|
||||||
|
|
||||||
class RelayUrlNormalizer {
|
class RelayUrlNormalizer {
|
||||||
companion object {
|
companion object {
|
||||||
@@ -141,12 +150,17 @@ class RelayUrlNormalizer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun normalize(url: String): NormalizedRelayUrl {
|
fun normalize(url: String): NormalizedRelayUrl {
|
||||||
normalizedUrls[url]?.let { return it }
|
normalizedUrls[url]?.let {
|
||||||
|
return when (it) {
|
||||||
|
is NormalizationResult.Sucess -> it.url
|
||||||
|
else -> throw IllegalArgumentException("Invalid Url: $url")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
val fixed = fix(url) ?: return NormalizedRelayUrl(url)
|
val fixed = fix(url) ?: return NormalizedRelayUrl(url)
|
||||||
val normalized = norm(fixed)
|
val normalized = norm(fixed)
|
||||||
normalizedUrls.put(url, normalized)
|
normalizedUrls.put(url, NormalizationResult.Sucess(normalized))
|
||||||
normalized
|
normalized
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
NormalizedRelayUrl(url)
|
NormalizedRelayUrl(url)
|
||||||
@@ -155,16 +169,22 @@ class RelayUrlNormalizer {
|
|||||||
|
|
||||||
fun normalizeOrNull(url: String): NormalizedRelayUrl? {
|
fun normalizeOrNull(url: String): NormalizedRelayUrl? {
|
||||||
if (url.isEmpty()) return null
|
if (url.isEmpty()) return null
|
||||||
normalizedUrls[url]?.let { return it }
|
normalizedUrls[url]?.let {
|
||||||
|
return when (it) {
|
||||||
|
is NormalizationResult.Sucess -> it.url
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
val fixed = fix(url)
|
val fixed = fix(url)
|
||||||
if (fixed != null) {
|
if (fixed != null) {
|
||||||
val normalized = norm(fixed)
|
val normalized = norm(fixed)
|
||||||
normalizedUrls.put(url, normalized)
|
normalizedUrls.put(url, NormalizationResult.Sucess(normalized))
|
||||||
return normalized
|
return normalized
|
||||||
} else {
|
} else {
|
||||||
Log.w("NormalizedRelayUrl", "Rejected Error $url")
|
Log.w("NormalizedRelayUrl", "Rejected Error $url")
|
||||||
|
normalizedUrls.put(url, NormalizationResult.Error)
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
Reference in New Issue
Block a user