All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pubnub.api.subscribe.eventengine.effect.ReconnectionPolicy.kt Maven / Gradle / Ivy

Go to download

PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter second!

There is a newer version: 10.2.0
Show newest version
package com.pubnub.api.subscribe.eventengine.effect

import java.time.Duration
import kotlin.math.pow

internal abstract class RetryPolicy {
    protected abstract val maxRetries: Int
    protected abstract fun computeDelay(count: Int): Duration
    fun nextDelay(attempt: Int): Duration? {
        if (attempt >= maxRetries) {
            return null
        }
        return computeDelay(attempt)
    }
}

internal object NoRetriesPolicy : RetryPolicy() {
    override val maxRetries: Int = 0
    override fun computeDelay(count: Int): Duration = Duration.ZERO
}

internal class LinearPolicy(
    override val maxRetries: Int = 5, // LinearPolicy is created in PNConfiguration default maxRetries is -1 which is unlimited
    private val fixedDelay: Duration = Duration.ofSeconds(3)
) : RetryPolicy() {
    override fun computeDelay(count: Int): Duration = fixedDelay
}

internal class ExponentialPolicy(override val maxRetries: Int = 5) : RetryPolicy() { // LinearPolicy is created in PNConfiguration default maxRetries is -1 which is unlimited
    override fun computeDelay(count: Int): Duration = Duration.ofSeconds((2.0.pow(count - 1)).toLong())
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy