-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature for sleeping threads that do nothing
- Loading branch information
1 parent
a99562c
commit babe8ca
Showing
13 changed files
with
310 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
.../no/nav/pensjon/opptjening/omsorgsopptjening/bestem/pensjonsopptjening/config/TimeLock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.config | ||
|
||
import org.apache.commons.lang3.ObjectUtils.min | ||
import java.time.Clock | ||
import java.time.Duration | ||
import java.time.Instant | ||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
data class TimeLock( | ||
private val initialDelay: Duration, | ||
private val maxDelay: Duration, | ||
private val clock: Clock | ||
) { | ||
private var delayUntil: Instant = Instant.now(clock).plusSeconds(initialDelay.toSeconds()) | ||
private var count = AtomicLong(0) | ||
|
||
fun isOpen(): Boolean { | ||
return delayUntil <= Instant.now(clock) | ||
} | ||
|
||
fun lock(): Duration { | ||
val now = Instant.now(clock) | ||
val toAdd = maxDelay.dividedBy(10).multipliedBy(count.incrementAndGet()).toSeconds() | ||
val newUntil = now.plusSeconds(toAdd) | ||
val maxUntil = now.plusSeconds(maxDelay.toSeconds()) | ||
delayUntil = min(newUntil, maxUntil) | ||
return Duration.between(now, delayUntil) | ||
} | ||
|
||
fun open() { | ||
delayUntil = Instant.now(clock) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.