Skip to content

Commit 49c15f8

Browse files
authored
[142]: Remove semaphore from TransactorZIO (#143)
1 parent 68ee22e commit 49c15f8

1 file changed

Lines changed: 18 additions & 70 deletions

File tree

Lines changed: 18 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.augustnagro.magnum.magzio
22

33
import com.augustnagro.magnum.{DbCon, DbTx, SqlException, SqlLogger}
4-
import zio.{Semaphore, Task, Trace, UIO, URLayer, ZIO, ZLayer}
4+
import zio.{Task, Trace, UIO, URLayer, ZIO, ZLayer}
55

66
import java.sql.Connection
77
import javax.sql.DataSource
@@ -10,16 +10,14 @@ import scala.util.control.NonFatal
1010
class TransactorZIO private (
1111
dataSource: DataSource,
1212
sqlLogger: SqlLogger,
13-
connectionConfig: Connection => Unit,
14-
semaphore: Option[Semaphore]
13+
connectionConfig: Connection => Unit
1514
):
1615

1716
def withSqlLogger(sqlLogger: SqlLogger): TransactorZIO =
1817
new TransactorZIO(
1918
dataSource,
2019
sqlLogger,
21-
connectionConfig,
22-
semaphore
20+
connectionConfig
2321
)
2422

2523
def withConnectionConfig(
@@ -28,23 +26,21 @@ class TransactorZIO private (
2826
new TransactorZIO(
2927
dataSource,
3028
sqlLogger,
31-
connectionConfig,
32-
semaphore
29+
connectionConfig
3330
)
3431

3532
def connect[A](f: DbCon ?=> A)(using Trace): Task[A] =
36-
val zio = ZIO.blocking(
33+
ZIO.blocking(
3734
ZIO.acquireReleaseWith(acquireConnection)(releaseConnection)(cn =>
3835
ZIO.attempt {
3936
connectionConfig(cn)
4037
f(using DbCon(cn, sqlLogger))
4138
}
4239
)
4340
)
44-
semaphore.fold(zio)(_.withPermit(zio))
4541

4642
def transact[A](f: DbTx ?=> A)(using Trace): Task[A] =
47-
val zio = ZIO.blocking(
43+
ZIO.blocking(
4844
ZIO.acquireReleaseWith(acquireConnection)(releaseConnection)(cn =>
4945
ZIO.attempt {
5046
connectionConfig(cn)
@@ -61,7 +57,6 @@ class TransactorZIO private (
6157
}.uninterruptible
6258
)
6359
)
64-
semaphore.fold(zio)(_.withPermit(zio))
6560

6661
private def acquireConnection(using Trace): Task[Connection] =
6762
ZIO
@@ -81,40 +76,6 @@ end TransactorZIO
8176
object TransactorZIO:
8277
private val noOpConnectionConfig: Connection => Unit = _ => ()
8378

84-
/** Construct a TransactorZIO
85-
*
86-
* @param sqlLogger
87-
* Logging configuration
88-
* @param connectionConfig
89-
* Customize the underlying JDBC Connections
90-
* @param maxBlockingThreads
91-
* Number of threads in your connection pool. This helps magzio be more
92-
* memory efficient by limiting the number of blocking pool threads used.
93-
* Not needed if using the ZIO virtual-thread based blocking executor
94-
*/
95-
def layer(
96-
sqlLogger: SqlLogger,
97-
connectionConfig: Connection => Unit,
98-
maxBlockingThreads: Option[Int]
99-
): URLayer[DataSource, TransactorZIO] =
100-
ZLayer.fromZIO {
101-
for {
102-
dataSource <- ZIO.service[DataSource]
103-
transactor <- ZIO
104-
.fromOption(maxBlockingThreads)
105-
.flatMap(threads => Semaphore.make(threads))
106-
.unsome
107-
.map(semaphoreOpt =>
108-
new TransactorZIO(
109-
dataSource = dataSource,
110-
sqlLogger = sqlLogger,
111-
connectionConfig = connectionConfig,
112-
semaphore = semaphoreOpt
113-
)
114-
)
115-
} yield transactor
116-
}
117-
11879
/** Construct a TransactorZIO
11980
*
12081
* @param sqlLogger
@@ -126,32 +87,32 @@ object TransactorZIO:
12687
sqlLogger: SqlLogger,
12788
connectionConfig: Connection => Unit
12889
): URLayer[DataSource, TransactorZIO] =
129-
layer(
130-
sqlLogger = sqlLogger,
131-
connectionConfig = connectionConfig,
132-
maxBlockingThreads = None
133-
)
90+
ZLayer
91+
.service[DataSource]
92+
.project(ds =>
93+
TransactorZIO(
94+
dataSource = ds,
95+
sqlLogger = sqlLogger,
96+
connectionConfig = connectionConfig
97+
)
98+
)
13499

135100
/** Construct a TransactorZIO
136101
*
137-
* @param dataSource
138-
* Datasource to be used
139102
* @param sqlLogger
140103
* Logging configuration
141104
*/
142105
def layer(sqlLogger: SqlLogger): URLayer[DataSource, TransactorZIO] =
143106
layer(
144107
sqlLogger = sqlLogger,
145-
connectionConfig = noOpConnectionConfig,
146-
maxBlockingThreads = None
108+
connectionConfig = noOpConnectionConfig
147109
)
148110

149111
/** Construct a TransactorZIO */
150112
def layer: URLayer[DataSource, TransactorZIO] =
151113
layer(
152114
sqlLogger = SqlLogger.Default,
153-
connectionConfig = noOpConnectionConfig,
154-
maxBlockingThreads = None
115+
connectionConfig = noOpConnectionConfig
155116
)
156117

157118
/** Construct a TransactorZIO
@@ -164,20 +125,7 @@ object TransactorZIO:
164125
): URLayer[DataSource, TransactorZIO] =
165126
layer(
166127
sqlLogger = SqlLogger.Default,
167-
connectionConfig = connectionConfig,
168-
maxBlockingThreads = None
169-
)
170-
171-
/** @param maxBlockingThreads
172-
* Number of threads in your connection pool. This helps magzio be more
173-
* memory efficient by limiting the number of blocking pool threads used.
174-
* Not needed if using the ZIO virtual-thread based blocking executor
175-
*/
176-
def layer(maxBlockingThreads: Int): URLayer[DataSource, TransactorZIO] =
177-
layer(
178-
sqlLogger = SqlLogger.Default,
179-
connectionConfig = noOpConnectionConfig,
180-
maxBlockingThreads = Some(maxBlockingThreads)
128+
connectionConfig = connectionConfig
181129
)
182130

183131
end TransactorZIO

0 commit comments

Comments
 (0)