pl.allegro.tech.servicemesh.envoycontrol.config.sharing.ContainerPool.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of envoy-control-tests Show documentation
Show all versions of envoy-control-tests Show documentation
Production-ready Control Plane for Service Mesh based on Envoy Proxy.
package pl.allegro.tech.servicemesh.envoycontrol.config.sharing
import org.testcontainers.containers.GenericContainer
import java.util.LinkedList
import java.util.Queue
class ContainerPool>(private val containerFactory: () -> CONTAINER) {
private val freeContainers: Queue = LinkedList()
private val usedContainers = mutableMapOf()
fun acquire(owner: OWNER): CONTAINER {
val container = freeContainers.poll() ?: containerFactory()
container.start()
usedContainers[owner] = container
return container
}
fun release(owner: OWNER) {
val container = usedContainers.remove(owner) ?: throw ContainerNotFound(owner.toString())
freeContainers.add(container)
}
class ContainerNotFound(owner: String) : RuntimeException("container owned by $owner not found")
}