Warn when timeout proxy issue

This commit is contained in:
sim 2023-03-26 14:18:36 +02:00
parent 5bdad9ca4c
commit 6ed937d9b6
4 changed files with 59 additions and 33 deletions

View File

@ -23,6 +23,9 @@ import java.util.Calendar
class SSEListener(val context: Context) : EventSourceListener() {
private var pinged = false
private var started = false
override fun onOpen(eventSource: EventSource, response: Response) {
FailureHandler.newEventSource(context, eventSource)
StartService.wakeLock?.let {
@ -43,7 +46,14 @@ class SSEListener(val context: Context) : EventSourceListener() {
lastEventDate = Calendar.getInstance()
when (type) {
"start" -> context.hasStartedOnce = true
"start" -> {
started = true
context.hasStartedOnce = true
}
"ping" -> {
pinged = true
FailureHandler.newPing()
}
"keepalive" -> {
val message = Gson().fromJson(data, SSEResponse::class.java)
keepalive = message.keepalive
@ -76,7 +86,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
Log.d(TAG, "onClosed: $eventSource")
eventSource.cancel()
if (!shouldRestart()) return
FailureHandler.newFail(context, eventSource)
FailureHandler.newFail(context, eventSource, started, pinged)
RestartWorker.run(context, delay = 0)
}
@ -95,7 +105,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
FailureHandler.once(eventSource)
return
}
FailureHandler.newFail(context, eventSource)
FailureHandler.newFail(context, eventSource, started, pinged)
val delay = when (FailureHandler.nFails) {
1 -> 2 // 2sec
2 -> 5 // 5sec

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.util.Log
import okhttp3.sse.EventSource
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.deleteWarningNotification
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showNoPingNotification
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showWarningNotification
import org.unifiedpush.distributor.nextpush.utils.TAG
@ -12,6 +13,8 @@ object FailureHandler {
var nFails = 0
private set
private var nFailsBeforePing = 0
// This is the last eventSource opened
private var eventSource: EventSource? = null
@ -22,7 +25,11 @@ object FailureHandler {
deleteWarningNotification(context)
}
fun newFail(context: Context, eventSource: EventSource?) {
fun newPing() {
nFailsBeforePing = 0
}
fun newFail(context: Context, eventSource: EventSource?, started: Boolean, pinged: Boolean) {
Log.d(TAG, "newFail/Eventsource: $eventSource")
// ignore fails from a possible old eventSource
// if we are already reconnected
@ -32,6 +39,12 @@ object FailureHandler {
if (nFails == 2) {
showWarningNotification(context)
}
if (started && !pinged) {
nFailsBeforePing++
if (nFailsBeforePing == 3) {
showNoPingNotification(context)
}
}
this.eventSource = null
}
}

View File

@ -33,8 +33,6 @@ private data class NotificationData(
)
object NotificationUtils {
private val Context.WARNING_CHANNEL_ID
get() = "${this.getString(R.string.app_name)}.Warning"
private var warningShown = false
private fun createNotificationChannel(context: Context, channelData: ChannelData) {
@ -136,11 +134,8 @@ object NotificationUtils {
)
}
fun showWarningNotification(context: Context) {
if (warningShown) {
return
}
val notificationChannelId = context.WARNING_CHANNEL_ID
private fun createWarningNotificationChannel(context: Context): String {
val notificationChannelId = "${context.getString(R.string.app_name)}.Warning"
createNotificationChannel(
context,
@ -151,6 +146,14 @@ object NotificationUtils {
context.getString(R.string.warning_notif_description)
)
)
return notificationChannelId
}
fun showWarningNotification(context: Context) {
if (warningShown) {
return
}
val notificationChannelId = createWarningNotificationChannel(context)
val intent = createIntentToMain(context)
@ -178,17 +181,7 @@ object NotificationUtils {
}
fun showStartErrorNotification(context: Context) {
val notificationChannelId = context.WARNING_CHANNEL_ID
createNotificationChannel(
context,
ChannelData(
notificationChannelId,
"Warning",
NotificationManager.IMPORTANCE_HIGH,
context.getString(R.string.warning_notif_description)
)
)
val notificationChannelId = createWarningNotificationChannel(context)
val notification = createNotification(
context,
@ -207,17 +200,7 @@ object NotificationUtils {
}
fun showLowKeepaliveNotification(context: Context, keepalive: Int) {
val notificationChannelId = context.WARNING_CHANNEL_ID
createNotificationChannel(
context,
ChannelData(
notificationChannelId,
"Warning",
NotificationManager.IMPORTANCE_HIGH,
context.getString(R.string.warning_notif_description)
)
)
val notificationChannelId = createWarningNotificationChannel(context)
val notification = createNotification(
context,
@ -234,4 +217,23 @@ object NotificationUtils {
show(context, NOTIFICATION_ID_WARNING, notification)
}
fun showNoPingNotification(context: Context) {
val notificationChannelId = createWarningNotificationChannel(context)
val notification = createNotification(
context,
NotificationData(
context.getString(R.string.no_ping_notif_content),
context.getString(R.string.warning_notif_ticker),
Notification.PRIORITY_HIGH,
false,
notificationChannelId
),
null,
true
)
show(context, NOTIFICATION_ID_WARNING, notification)
}
}

View File

@ -20,6 +20,7 @@
<string name="warning_notif_ticker">Warning</string>
<string name="start_error_notif_content">The service could not be started correctly. Check the configuration of your server.</string>
<string name="low_keepalive_notif_content">The server app is configured with a low keepalive: %ss. It will drain your battery. We recommend using a higher keepalive.</string>
<string name="no_ping_notif_content">NextPush was disconnected 3 times before receiving the ping. You probably have a problem with your server configuration. Your reverse proxy timeout is probably too low.</string>
<string name="foreground_notif_ticker">Foreground</string>
<string name="message_missing_nextcloud_app">Nextcloud Files is not installed on your device.\nPlease install it</string>
<string name="uri_market_nextcloud_app">market://details?id=com.nextcloud.client</string>