-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Connection Pool. Introduce PostgresDriverUnit. Only restart…
… connection on fatal error. Use K2 compiler and gradle 8.4. Tests now using shared pool. (#30)
- Loading branch information
1 parent
a3631bd
commit 6bea499
Showing
10 changed files
with
322 additions
and
149 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
|
||
[![Kotlin Experimental](https://kotl.in/badges/experimental.svg)](https://kotlinlang.org/docs/components-stability.html) | ||
[![CI](https://github.com/miguel-moreira/pgkn/actions/workflows/blank.yml/badge.svg?branch=main)](https://github.com/miguel-moreira/pgkn/actions/workflows/blank.yml) | ||
[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0) | ||
[![Maven Central](https://img.shields.io/maven-central/v/io.github.moreirasantos/pgkn)](https://central.sonatype.com/artifact/io.github.moreirasantos/pgkn/) | ||
[![Kotlin](https://img.shields.io/badge/kotlin-1.9.0-blue.svg?logo=kotlin)](http://kotlinlang.org) | ||
|
||
# pgkn | ||
|
||
PostgreSQL Kotlin/Native Driver | ||
|
||
## Usage | ||
|
||
``` | ||
implementation("io.github.moreirasantos:pgkn:1.0.0") | ||
``` | ||
|
||
```kotlin | ||
fun main() { | ||
val driver = PostgresDriver( | ||
|
@@ -42,14 +44,27 @@ fun main() { | |
} | ||
} | ||
``` | ||
|
||
## Features | ||
## Named Parameters | ||
|
||
### Connection Pool | ||
|
||
PGKN has a connection pool, its size being configurable in `PostgresDriver()` - 20 by default. | ||
It will refresh connection units if the query fails fatally, but it still needs more fine-grained status checks. | ||
|
||
|
||
You can also use a single connection with `PostgresDriverUnit`, which currently is not `suspend` | ||
but probably will be in the future. | ||
|
||
### Named Parameters | ||
|
||
```kotlin | ||
driver.execute( | ||
"select name from my_table where name = :one OR email = :other", | ||
mapOf("one" to "your_name", "other" to "[email protected]") | ||
) { it.getString(0) } | ||
``` | ||
|
||
Named Parameters provides an alternative to the traditional syntax using `?` to specify parameters. | ||
Under the hood, it substitutes the named parameters to a query placeholder. | ||
|
||
|
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
kotlin.code.style=official | ||
kotlin.native.binary.sourceInfoType=libbacktrace | ||
kotlin.experimental.tryK2=true | ||
kapt.use.k2=true |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip | ||
networkTimeout=10000 | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
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/commonMain/kotlin/io/github/moreirasantos/pgkn/pool/ConnectionPool.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,29 @@ | ||
package io.github.moreirasantos.pgkn.pool | ||
|
||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.Semaphore | ||
import kotlinx.coroutines.sync.withLock | ||
import kotlinx.coroutines.sync.withPermit | ||
|
||
/** | ||
* Pool with Semaphore/Mutex pattern. | ||
* Using semaphore with max permits being the size of connection pool to throttle. | ||
* Mutex to make sure one connection is not used at the same time- | ||
*/ | ||
internal class ConnectionPool<T>(connections: List<T>) { | ||
|
||
private val mutex = Mutex() | ||
private val semaphore = Semaphore(permits = connections.size) | ||
private val connections = connections.toMutableList() | ||
|
||
suspend operator fun <U> invoke(handler: suspend (T) -> U): U { | ||
semaphore.withPermit { | ||
val borrowed = mutex.withLock { connections.removeLast() } | ||
try { | ||
return handler(borrowed) | ||
} finally { | ||
mutex.withLock { connections.add(borrowed) } | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/commonTest/kotlin/io/github/moreirasantos/pgkn/DriverUnitTest.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,20 @@ | ||
package io.github.moreirasantos.pgkn | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class DriverUnitTest { | ||
@Test | ||
fun `single driver should work`() { | ||
val driver = PostgresDriverUnit( | ||
host = "localhost", | ||
port = 5678, | ||
database = "postgres", | ||
user = "postgres", | ||
password = "postgres" | ||
) | ||
assertEquals("echo", driver | ||
.execute("select 'echo'") { it.getString(0) } | ||
.first()) | ||
} | ||
} |
Oops, something went wrong.