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

orbit.client.execution.ExecutionLeases.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0a19
Show newest version
/*
 Copyright (C) 2015 - 2019 Electronic Arts Inc.  All rights reserved.
 This file is part of the Orbit Project .
 See license in LICENSE.
 */

package orbit.client.execution

import orbit.client.mesh.AddressableLeaser
import orbit.shared.addressable.AddressableLease
import orbit.shared.addressable.AddressableReference
import java.util.concurrent.ConcurrentHashMap

internal class ExecutionLeases(
    private val addressableLeaser: AddressableLeaser
) {
    private val currentLeases = ConcurrentHashMap()

    fun getLease(addressableReference: AddressableReference) = currentLeases[addressableReference]

    suspend fun getOrRenewLease(addressableReference: AddressableReference): AddressableLease {
        var currentLease = currentLeases[addressableReference]

        if (currentLease == null || currentLease.expiresAt.inPast()) {
            currentLease = renewLease(addressableReference)
        }

        return currentLease
    }

    suspend fun renewLease(addressableReference: AddressableReference): AddressableLease {
        val newLease = addressableLeaser.renewLease(addressableReference)
        checkNotNull(newLease)
        currentLeases[addressableReference] = newLease
        return newLease
    }

    suspend fun abandonLease(addressableReference: AddressableReference): Boolean {
        val currentLease = currentLeases[addressableReference]
        if (currentLease != null) {
            return addressableLeaser.abandonLease(addressableReference)
        }
        return false
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy