Better logs for the basic relay
This commit is contained in:
+28
-19
@@ -42,6 +42,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.commands.toRelay.EventCmd
|
|||||||
import com.vitorpamplona.quartz.nip01Core.relay.commands.toRelay.ReqCmd
|
import com.vitorpamplona.quartz.nip01Core.relay.commands.toRelay.ReqCmd
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
|
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.displayUrl
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.sockets.WebSocket
|
import com.vitorpamplona.quartz.nip01Core.relay.sockets.WebSocket
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.sockets.WebSocketListener
|
import com.vitorpamplona.quartz.nip01Core.relay.sockets.WebSocketListener
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.sockets.WebsocketBuilder
|
import com.vitorpamplona.quartz.nip01Core.relay.sockets.WebsocketBuilder
|
||||||
@@ -63,6 +64,8 @@ open class BasicRelayClient(
|
|||||||
const val DELAY_TO_RECONNECT_IN_MSECS = 500L
|
const val DELAY_TO_RECONNECT_IN_MSECS = 500L
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val logTag = "Relay ${url.displayUrl()}"
|
||||||
|
|
||||||
private var socket: WebSocket? = null
|
private var socket: WebSocket? = null
|
||||||
private var isReady: Boolean = false
|
private var isReady: Boolean = false
|
||||||
private var usingCompression: Boolean = false
|
private var usingCompression: Boolean = false
|
||||||
@@ -109,7 +112,7 @@ open class BasicRelayClient(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d("Relay", "${url.url} connecting...")
|
Log.d(logTag, "Connecting...")
|
||||||
|
|
||||||
lastConnectTentative = TimeUtils.now()
|
lastConnectTentative = TimeUtils.now()
|
||||||
|
|
||||||
@@ -118,7 +121,7 @@ open class BasicRelayClient(
|
|||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
if (e is CancellationException) throw e
|
if (e is CancellationException) throw e
|
||||||
|
|
||||||
Log.w("Relay", "${url.url} Crash before connecting $url", e)
|
Log.w(logTag, "Crash before connecting", e)
|
||||||
stats.newError(e.message ?: "Error trying to connect: ${e.javaClass.simpleName}")
|
stats.newError(e.message ?: "Error trying to connect: ${e.javaClass.simpleName}")
|
||||||
|
|
||||||
markConnectionAsClosed()
|
markConnectionAsClosed()
|
||||||
@@ -135,7 +138,7 @@ open class BasicRelayClient(
|
|||||||
pingMillis: Long,
|
pingMillis: Long,
|
||||||
compression: Boolean,
|
compression: Boolean,
|
||||||
) {
|
) {
|
||||||
Log.d("Relay", "${url.url} onConnect $socket")
|
Log.d(logTag, "OnOpen (ping: ${pingMillis}ms${if (compression) ", using compression" else ""})")
|
||||||
|
|
||||||
markConnectionAsReady(pingMillis, compression)
|
markConnectionAsReady(pingMillis, compression)
|
||||||
|
|
||||||
@@ -145,6 +148,8 @@ open class BasicRelayClient(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onMessage(text: String) {
|
override fun onMessage(text: String) {
|
||||||
|
// Log.d(logTag, "Receiving: $text")
|
||||||
|
|
||||||
stats.addBytesReceived(text.bytesUsedInMemory())
|
stats.addBytesReceived(text.bytesUsedInMemory())
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -161,7 +166,7 @@ open class BasicRelayClient(
|
|||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
if (e is CancellationException) throw e
|
if (e is CancellationException) throw e
|
||||||
stats.newError("Error processing: $text")
|
stats.newError("Error processing: $text")
|
||||||
Log.e("Relay", "${url.url} Error processing: $text")
|
Log.e(logTag, "Error processing: $text")
|
||||||
listener.onError(this@BasicRelayClient, "", Error("Error processing $text"))
|
listener.onError(this@BasicRelayClient, "", Error("Error processing $text"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -170,7 +175,7 @@ open class BasicRelayClient(
|
|||||||
code: Int,
|
code: Int,
|
||||||
reason: String,
|
reason: String,
|
||||||
) {
|
) {
|
||||||
Log.w("Relay", "${url.url} onClosing: $code $reason")
|
Log.w(logTag, "OnClosing $code $reason")
|
||||||
|
|
||||||
listener.onRelayStateChange(this@BasicRelayClient, RelayState.DISCONNECTING)
|
listener.onRelayStateChange(this@BasicRelayClient, RelayState.DISCONNECTING)
|
||||||
}
|
}
|
||||||
@@ -179,10 +184,9 @@ open class BasicRelayClient(
|
|||||||
code: Int,
|
code: Int,
|
||||||
reason: String,
|
reason: String,
|
||||||
) {
|
) {
|
||||||
|
Log.w(logTag, "OnClosed $reason")
|
||||||
|
|
||||||
markConnectionAsClosed()
|
markConnectionAsClosed()
|
||||||
|
|
||||||
Log.w("Relay", "${url.url} onClosed $reason")
|
|
||||||
|
|
||||||
listener.onRelayStateChange(this@BasicRelayClient, RelayState.DISCONNECTED)
|
listener.onRelayStateChange(this@BasicRelayClient, RelayState.DISCONNECTED)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,7 +205,7 @@ open class BasicRelayClient(
|
|||||||
// Failures disconnect the relay.
|
// Failures disconnect the relay.
|
||||||
markConnectionAsClosed()
|
markConnectionAsClosed()
|
||||||
|
|
||||||
Log.w("Relay", "${url.url} onFailure $code $response ${t.message} $socket")
|
Log.w(logTag, "OnFailure $code $response ${t.message} $socket")
|
||||||
t.printStackTrace()
|
t.printStackTrace()
|
||||||
listener.onError(
|
listener.onError(
|
||||||
this@BasicRelayClient,
|
this@BasicRelayClient,
|
||||||
@@ -233,6 +237,7 @@ open class BasicRelayClient(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun processEvent(msg: EventMessage) {
|
private fun processEvent(msg: EventMessage) {
|
||||||
|
// Log.w(logTag, "Event ${msg.subId} ${msg.event.toJson()}")
|
||||||
listener.onEvent(
|
listener.onEvent(
|
||||||
relay = this,
|
relay = this,
|
||||||
subId = msg.subId,
|
subId = msg.subId,
|
||||||
@@ -243,13 +248,13 @@ open class BasicRelayClient(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun processEose(msg: EoseMessage) {
|
private fun processEose(msg: EoseMessage) {
|
||||||
// Log.w("Relay", "Relay onEOSE $url $newMessage")
|
// Log.w(logTag, "EOSE ${msg.subId}")
|
||||||
afterEOSEPerSubscription[msg.subId] = true
|
afterEOSEPerSubscription[msg.subId] = true
|
||||||
listener.onEOSE(this, msg.subId, TimeUtils.now())
|
listener.onEOSE(this, msg.subId, TimeUtils.now())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun processNotice(msg: NoticeMessage) {
|
private fun processNotice(msg: NoticeMessage) {
|
||||||
// Log.w("Relay", "Relay onNotice $url, $newMessage")
|
Log.w(logTag, "Notice ${msg.message}")
|
||||||
stats.newNotice(msg.message)
|
stats.newNotice(msg.message)
|
||||||
listener.onError(this@BasicRelayClient, msg.message, Error("Relay sent notice: $msg.message"))
|
listener.onError(this@BasicRelayClient, msg.message, Error("Relay sent notice: $msg.message"))
|
||||||
}
|
}
|
||||||
@@ -258,7 +263,7 @@ open class BasicRelayClient(
|
|||||||
msg: OkMessage,
|
msg: OkMessage,
|
||||||
onConnected: () -> Unit,
|
onConnected: () -> Unit,
|
||||||
) {
|
) {
|
||||||
Log.w("Relay", "${url.url} onOK: ${msg.eventId} ${msg.success} ${msg.message}")
|
// Log.w(logTag, "OK: ${msg.eventId} ${msg.success} ${msg.message}")
|
||||||
|
|
||||||
// if this is the OK of an auth event, renew all subscriptions and resend all outgoing events.
|
// if this is the OK of an auth event, renew all subscriptions and resend all outgoing events.
|
||||||
if (authResponseWatcher.containsKey(msg.eventId)) {
|
if (authResponseWatcher.containsKey(msg.eventId)) {
|
||||||
@@ -278,29 +283,29 @@ open class BasicRelayClient(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun processAuth(msg: AuthMessage) {
|
private fun processAuth(msg: AuthMessage) {
|
||||||
// Log.d("Relay", "Relay onAuth $url, $newMessage")
|
// Log.d(logTag, "Auth $newMessage")
|
||||||
listener.onAuth(this@BasicRelayClient, msg.challenge)
|
listener.onAuth(this@BasicRelayClient, msg.challenge)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun processNotify(msg: NotifyMessage) {
|
private fun processNotify(msg: NotifyMessage) {
|
||||||
// Log.w("Relay", "Relay onNotify $url, $newMessage")
|
// Log.w(logTag, "Notify $newMessage")
|
||||||
listener.onNotify(this@BasicRelayClient, msg.message)
|
listener.onNotify(this@BasicRelayClient, msg.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun processClosed(msg: ClosedMessage) {
|
private fun processClosed(msg: ClosedMessage) {
|
||||||
|
// Log.w(logTag, "Relay Closed Subscription $newMessage")
|
||||||
afterEOSEPerSubscription[msg.subscriptionId] = false
|
afterEOSEPerSubscription[msg.subscriptionId] = false
|
||||||
// Log.w("Relay", "Relay Closed Subscription $url, $newMessage")
|
|
||||||
listener.onClosed(this@BasicRelayClient, msg.subscriptionId, msg.message)
|
listener.onClosed(this@BasicRelayClient, msg.subscriptionId, msg.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun processUnkownMessage(newMessage: String) {
|
private fun processUnkownMessage(newMessage: String) {
|
||||||
stats.newError("Unsupported message: $newMessage")
|
stats.newError("Unsupported message: $newMessage")
|
||||||
Log.w("Relay", "Unsupported message: $newMessage")
|
Log.w(logTag, "Unsupported message: $newMessage")
|
||||||
listener.onError(this, "", Error("Unsupported message: $newMessage"))
|
listener.onError(this, "", Error("Unsupported message: $newMessage"))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun disconnect() {
|
override fun disconnect() {
|
||||||
Log.d("Relay", "${url.url} disconnecting...")
|
Log.d(logTag, "Disconnecting...")
|
||||||
lastConnectTentative = 0L // this is not an error, so prepare to reconnect as soon as requested.
|
lastConnectTentative = 0L // this is not an error, so prepare to reconnect as soon as requested.
|
||||||
delayToConnect = DELAY_TO_RECONNECT_IN_MSECS
|
delayToConnect = DELAY_TO_RECONNECT_IN_MSECS
|
||||||
socket?.disconnect()
|
socket?.disconnect()
|
||||||
@@ -410,6 +415,7 @@ open class BasicRelayClient(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
socket?.let {
|
socket?.let {
|
||||||
|
// Log.d(logTag, "Sending: $str")
|
||||||
val result = it.send(str)
|
val result = it.send(str)
|
||||||
listener.onSend(this@BasicRelayClient, str, result)
|
listener.onSend(this@BasicRelayClient, str, result)
|
||||||
stats.addBytesSent(str.bytesUsedInMemory())
|
stats.addBytesSent(str.bytesUsedInMemory())
|
||||||
@@ -417,7 +423,10 @@ open class BasicRelayClient(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun close(subscriptionId: String) {
|
override fun close(subscriptionId: String) {
|
||||||
writeToSocket(CloseCmd.Companion.toJson(subscriptionId))
|
// avoids sending closes for subscriptions that were never sent to this relay.
|
||||||
afterEOSEPerSubscription[subscriptionId] = false
|
if (afterEOSEPerSubscription.containsKey(subscriptionId)) {
|
||||||
|
writeToSocket(CloseCmd.Companion.toJson(subscriptionId))
|
||||||
|
afterEOSEPerSubscription[subscriptionId] = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user