diff --git a/app/src/main/java/io/heckel/ntfy/service/Connection.kt b/app/src/main/java/io/heckel/ntfy/service/Connection.kt index 3af4a3f6..f195fade 100644 --- a/app/src/main/java/io/heckel/ntfy/service/Connection.kt +++ b/app/src/main/java/io/heckel/ntfy/service/Connection.kt @@ -1,5 +1,8 @@ package io.heckel.ntfy.service +import okhttp3.internal.http2.StreamResetException +import java.io.EOFException + interface Connection { fun start() fun close() @@ -20,3 +23,10 @@ data class ConnectionId( val clientCertHash: Int, // Hash of client certificate or 0 if none val reconnectVersion: Long // Incremented to force reconnection for this baseUrl ) + +fun isConnectionBrokenException(t: Throwable): Boolean { + return t is EOFException + || t.cause is EOFException + || t is StreamResetException + || t.cause is StreamResetException +} diff --git a/app/src/main/java/io/heckel/ntfy/service/JsonConnection.kt b/app/src/main/java/io/heckel/ntfy/service/JsonConnection.kt index 5e8c0ac8..ab02dc14 100644 --- a/app/src/main/java/io/heckel/ntfy/service/JsonConnection.kt +++ b/app/src/main/java/io/heckel/ntfy/service/JsonConnection.kt @@ -16,6 +16,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import okhttp3.Call +import java.io.EOFException import kotlin.random.Random class JsonConnection( @@ -76,10 +77,11 @@ class JsonConnection( Log.d(TAG, "[$url] Connection cancelled") break } - Log.e(TAG, "[$url] Connection failed: ${e.message}", e) + Log.d(TAG, "[$url] Connection broken, reconnecting ...") retryMillis = nextRetryMillis(retryMillis, startTime) val nextRetryTime = System.currentTimeMillis() + retryMillis - connectionDetailsListener(subscriptionIds, ConnectionState.CONNECTING, e, nextRetryTime) + val error = if (isConnectionBrokenException(e)) null else e + connectionDetailsListener(subscriptionIds, ConnectionState.CONNECTING, error, nextRetryTime) Log.w(TAG, "[$url] Retrying connection in ${retryMillis / 1000}s ...") delay(retryMillis) } diff --git a/app/src/main/java/io/heckel/ntfy/service/WsConnection.kt b/app/src/main/java/io/heckel/ntfy/service/WsConnection.kt index 8b49b660..24b9f633 100644 --- a/app/src/main/java/io/heckel/ntfy/service/WsConnection.kt +++ b/app/src/main/java/io/heckel/ntfy/service/WsConnection.kt @@ -1,7 +1,6 @@ package io.heckel.ntfy.service import android.app.AlarmManager -import android.content.Context import android.os.Build import io.heckel.ntfy.db.ConnectionState import io.heckel.ntfy.db.CustomHeader @@ -18,6 +17,7 @@ import okhttp3.OkHttpClient import okhttp3.Response import okhttp3.WebSocket import okhttp3.WebSocketListener +import java.io.EOFException import java.util.Calendar import java.util.concurrent.atomic.AtomicLong import java.util.concurrent.atomic.AtomicReference @@ -186,7 +186,8 @@ class WsConnection( errorCount++ val retrySeconds = RETRY_SECONDS.getOrNull(errorCount) ?: RETRY_SECONDS.last() val nextRetryTime = System.currentTimeMillis() + (retrySeconds * 1000L) - connectionDetailsListener(subscriptionIds, ConnectionState.CONNECTING, t, nextRetryTime) + val error = if (isConnectionBrokenException(t)) null else t + connectionDetailsListener(subscriptionIds, ConnectionState.CONNECTING, error, nextRetryTime) scheduleReconnect(retrySeconds) } }