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

pl.allegro.tech.servicemesh.envoycontrol.config.sharing.ContainerPool.kt Maven / Gradle / Ivy

There is a newer version: 0.22.1
Show newest version
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")
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy