This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michel Zimmer
committed
Jun 15, 2022
1 parent
89d52f5
commit 710750c
Showing
9 changed files
with
157 additions
and
16 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
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 |
---|---|---|
|
@@ -2,5 +2,9 @@ services: | |
main: | ||
build: | ||
context: . | ||
command: | ||
- -Xmx1g | ||
- -jar | ||
- /opt/bandwhichd-server.jar | ||
ports: | ||
- 8080:8080 |
35 changes: 35 additions & 0 deletions
35
src/main/scala/de/neuland/bandwhichd/server/adapter/in/scheduler/MemoryScheduler.scala
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,35 @@ | ||
package de.neuland.bandwhichd.server.adapter.in.scheduler | ||
|
||
import cats.Monad | ||
import de.neuland.bandwhichd.server.adapter.out.measurement.MeasurementInMemoryRepository | ||
import de.neuland.bandwhichd.server.lib.health.jvm.JvmMemoryUtilization | ||
import de.neuland.bandwhichd.server.lib.scheduling.{Schedule, Scheduler, Work} | ||
|
||
import scala.concurrent.duration.{FiniteDuration, SECONDS} | ||
|
||
class MemoryScheduler[F[_]: Monad]( | ||
private val measurementInMemoryRepository: MeasurementInMemoryRepository[F] | ||
) extends Scheduler[F] { | ||
|
||
private val inMemoryStorageCapThreshold: Long = | ||
scala.util.Properties | ||
.envOrElse("IN_MEMORY_STORAGE_CAP_THRESHOLD", "") | ||
.toLongOption | ||
.getOrElse(80) | ||
|
||
override def schedule: F[Schedule[F]] = | ||
Monad[F].pure( | ||
Schedule.Pausing( | ||
FiniteDuration(10, SECONDS), | ||
Work({ | ||
if ( | ||
JvmMemoryUtilization.current.usedMemoryPercentage > inMemoryStorageCapThreshold | ||
) { | ||
measurementInMemoryRepository.cap | ||
} else { | ||
Monad[F].pure(()) | ||
} | ||
}) | ||
) | ||
) | ||
} |
16 changes: 13 additions & 3 deletions
16
src/main/scala/de/neuland/bandwhichd/server/adapter/in/v1/health/HealthController.scala
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
29 changes: 29 additions & 0 deletions
29
src/main/scala/de/neuland/bandwhichd/server/adapter/out/CappedStorage.scala
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,29 @@ | ||
package de.neuland.bandwhichd.server.adapter.out | ||
|
||
case class CappedStorage[A] private ( | ||
maybeCap: Option[Int], | ||
storage: Vector[A] | ||
) { | ||
def cap: CappedStorage[A] = | ||
capAt(storage.length) | ||
|
||
private def capAt(cap: Int): CappedStorage[A] = | ||
capAt(Some(cap)) | ||
|
||
private def capAt(maybeCap: Option[Int]): CappedStorage[A] = | ||
maybeCap.fold(CappedStorage(maybeCap, storage))(cap => | ||
CappedStorage(maybeCap, storage.drop(storage.length - cap)) | ||
) | ||
|
||
def store(value: A): CappedStorage[A] = | ||
CappedStorage( | ||
maybeCap, | ||
maybeCap | ||
.fold(storage)(cap => storage.drop(storage.length + 1 - cap)) | ||
.appended(value) | ||
) | ||
} | ||
|
||
object CappedStorage { | ||
def empty[A]: CappedStorage[A] = CappedStorage(None, Vector.empty) | ||
} |
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
32 changes: 32 additions & 0 deletions
32
src/test/scala/de/neuland/bandwhichd/server/adapter/out/CappedStorageSpec.scala
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,32 @@ | ||
package de.neuland.bandwhichd.server.adapter.out | ||
|
||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
class CappedStorageSpec extends AnyWordSpec with Matchers { | ||
"CappedStorage" should { | ||
"store and cap" in { | ||
val cappedStorage0 = CappedStorage.empty[Int] | ||
cappedStorage0.maybeCap shouldBe None | ||
cappedStorage0.storage shouldBe empty | ||
val cappedStorage1 = cappedStorage0.store(3) | ||
cappedStorage1.maybeCap shouldBe None | ||
cappedStorage1.storage shouldBe Vector(3) | ||
val cappedStorage2 = cappedStorage1.store(2) | ||
cappedStorage2.maybeCap shouldBe None | ||
cappedStorage2.storage shouldBe Vector(3, 2) | ||
val cappedStorage3 = cappedStorage2.store(-5) | ||
cappedStorage3.maybeCap shouldBe None | ||
cappedStorage3.storage shouldBe Vector(3, 2, -5) | ||
val cappedStorage4 = cappedStorage3.cap | ||
cappedStorage4.maybeCap shouldBe Some(3) | ||
cappedStorage4.storage shouldBe Vector(3, 2, -5) | ||
val cappedStorage5 = cappedStorage4.store(9) | ||
cappedStorage5.maybeCap shouldBe Some(3) | ||
cappedStorage5.storage shouldBe Vector(2, -5, 9) | ||
val cappedStorage6 = cappedStorage5.store(0) | ||
cappedStorage6.maybeCap shouldBe Some(3) | ||
cappedStorage6.storage shouldBe Vector(-5, 9, 0) | ||
} | ||
} | ||
} |