<commit_before>/*
 * This file is part of Blokada.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * Copyright © 2021 Blocka AB. All rights reserved.
 *
 * @author <NAME> (<EMAIL>)
 */

package engine

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import model.BlokadaException
import repository.Repos
import ui.utils.cause
import ui.utils.now
import utils.LoggerWithThread
import java.net.InetSocketAddress
import java.net.Socket

/**
 * Monitoring performance of a packet loop (any implementation).
 *
 * Will measure DNS query RTT, and count recoverable errors (like network IO errors). If too many
 * errors happen in a short amount of time, it fire onNoConnectivity() event (and cause a restart by
 * PacketLoopService).
 */
object MetricsService {

    internal const val PACKET_BUFFER_SIZE = 1600

    private const val MAX_ONE_WAY_DNS_REQUESTS = 30
    private const val MAX_RECENT_ERRORS = 50

    private val log = LoggerWithThread("Metrics")
    private val scope = GlobalScope

    private val processingRepo by lazy { Repos.processing }

    private lateinit var onNoConnectivity: () -> Unit

    private var hadAtLeastOneSuccessfulQuery = false
    private var oneWayDnsCounter = 0

    private var queryCounter = 0
    private var printEveryXQuery = 1

    private var cleanLoopCounter = 0
    private var errorCounterBeforeLoop = 0
    private var errorCounter = 0

    private var id: Short? = null
    private var lastTimestamp = 0L

    var lastRtt = 0L
        @Synchronized get
        @Synchronized set

    fun onLoopEnter() {
        errorCounterBeforeLoop = errorCounter<commit_msg>android: lower max recent errors and max dns err for packet loop to 15<commit_after>/*
 * This file is part of Blokada.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * Copyright © 2021 Blocka AB. All rights reserved.
 *
 * @author <NAME> (<EMAIL>)
 */

package engine

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import model.BlokadaException
import repository.Repos
import ui.utils.cause
import ui.utils.now
import utils.LoggerWithThread
import java.net.InetSocketAddress
import java.net.Socket

/**
 * Monitoring performance of a packet loop (any implementation).
 *
 * Will measure DNS query RTT, and count recoverable errors (like network IO errors). If too many
 * errors happen in a short amount of time, it fire onNoConnectivity() event (and cause a restart by
 * PacketLoopService).
 */
object MetricsService {

    internal const val PACKET_BUFFER_SIZE = 1600

    private const val MAX_ONE_WAY_DNS_REQUESTS = 15
    private const val MAX_RECENT_ERRORS = 15

    private val log = LoggerWithThread("Metrics")
    private val scope = GlobalScope

    private val processingRepo by lazy { Repos.processing }

    private lateinit var onNoConnectivity: () -> Unit

    private var hadAtLeastOneSuccessfulQuery = false
    private var oneWayDnsCounter = 0

    private var queryCounter = 0
    private var printEveryXQuery = 1

    private var cleanLoopCounter = 0
    private var errorCounterBeforeLoop = 0
    private var errorCounter = 0

    private var id: Short? = null
    private var lastTimestamp = 0L

    var lastRtt = 0L
        @Synchronized get
        @Synchronized set

    fun onLoopEnter() {
        errorCounterBeforeLoop = errorCounter
{'fraction_non_alphanumeric': 0.06314398943196829, 'fraction_numerical': 0.013210039630118891, 'mean_word_length': 3.56144578313253, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7387807', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1061, 'n_words': 416}