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

main.wisp.lease.pool.PoolLease.kt Maven / Gradle / Ivy

There is a newer version: 2024.11.14.031606-bddeae6
Show newest version
package wisp.lease.pool

import wisp.lease.Lease

class PoolLease(
  private val delegateLease: Lease,
  private val poolLeaseManager: PoolLeaseManager,
) : Lease by delegateLease {

  /**
   * Can always use the delegate's result
   */
  override fun checkHeld(): Boolean {
    val held = delegateLease.checkHeld()
    if (!held) {
      poolLeaseManager.clearPoolLeaseMapEntry(name)
    }
    return held
  }

  override fun checkHeldElsewhere(): Boolean {
    if (checkHeld()) return false

    return delegateLease.checkHeldElsewhere()
  }

  override fun acquire(): Boolean {
    if (poolLeaseManager.isAcquiredPoolLeaseMapEntry(name)) {
      val acquired = delegateLease.acquire()
      if (!acquired) {
        // we have either lost the lease (or this is a fake deploy or not a pool lease)
        poolLeaseManager.clearPoolLeaseMapEntry(name)
      }
      return acquired
    }

    // if we're not tracking any lease in the pool...
    if (poolLeaseManager.isEmptyPoolLeaseMapEntry(name)) {
      val acquired = delegateLease.acquire()
      if (acquired) {
        // we've acquired the lease, so it's the one to track for the pool
        poolLeaseManager.setPoolLeaseMapEntry(name)
      }
      return acquired
    }
    return false
  }

  override fun release(): Boolean {
    poolLeaseManager.clearPoolLeaseMapEntry(name)
    return delegateLease.release()
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy