Skip to content
This repository was archived by the owner on Dec 3, 2019. It is now read-only.

Commit e50ffd0

Browse files
committed
Connection configuration:
- Add ability to set channel class (enables use of, e.g., EpollEventLoop) - Add option to set statement timeout in PostgreSQL
1 parent 7dc83b9 commit e50ffd0

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

db-async-common/src/main/scala/com/github/mauricio/async/db/Configuration.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package com.github.mauricio.async.db
1919
import java.nio.charset.Charset
2020

2121
import io.netty.buffer.{ByteBufAllocator, PooledByteBufAllocator}
22+
import io.netty.channel.Channel
23+
import io.netty.channel.socket.nio.NioSocketChannel
2224
import io.netty.util.CharsetUtil
2325

2426
import scala.concurrent.duration._
@@ -45,8 +47,11 @@ object Configuration {
4547
* to any value you would like but again, make sure you know what you are doing if you do
4648
* change it.
4749
* @param allocator the netty buffer allocator to be used
50+
* @param channelClass the netty channel class to use. Should match the type of the event loop group set
51+
* for connections. Defaults to [[NioSocketChannel]]
4852
* @param connectTimeout the timeout for connecting to servers
4953
* @param testTimeout the timeout for connection tests performed by pools
54+
* @param statementTimeout the optional per-session statement timeout to set in the database
5055
* @param queryTimeout the optional query timeout
5156
*
5257
*/
@@ -60,6 +65,8 @@ case class Configuration(username: String,
6065
charset: Charset = Configuration.DefaultCharset,
6166
maximumMessageSize: Int = 16777216,
6267
allocator: ByteBufAllocator = PooledByteBufAllocator.DEFAULT,
68+
channelClass: Class[_ <: Channel] = classOf[NioSocketChannel],
6369
connectTimeout: Duration = 5.seconds,
6470
testTimeout: Duration = 5.seconds,
71+
statementTimeout: Option[Duration] = None,
6572
queryTimeout: Option[Duration] = None)

mysql-async/src/main/scala/com/github/mauricio/async/db/mysql/codec/MySQLConnectionHandler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class MySQLConnectionHandler(
6868
private var currentContext: ChannelHandlerContext = null
6969

7070
def connect: Future[MySQLConnectionHandler] = {
71-
this.bootstrap.channel(classOf[NioSocketChannel])
71+
this.bootstrap.channel(configuration.channelClass)
7272
this.bootstrap.handler(new ChannelInitializer[io.netty.channel.Channel]() {
7373

7474
override def initChannel(channel: io.netty.channel.Channel): Unit = {

postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/codec/PostgreSQLConnectionHandler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class PostgreSQLConnectionHandler
8181

8282
def connect: Future[PostgreSQLConnectionHandler] = {
8383
this.bootstrap.group(this.group)
84-
this.bootstrap.channel(classOf[NioSocketChannel])
84+
this.bootstrap.channel(configuration.channelClass)
8585
this.bootstrap.handler(new ChannelInitializer[channel.Channel]() {
8686

8787
override def initChannel(ch: channel.Channel): Unit = {

postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/pool/PostgreSQLConnectionFactory.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,16 @@ class PostgreSQLConnectionFactory(
5151

5252
def create: PostgreSQLConnection = {
5353
val connection = new PostgreSQLConnection(configuration, group = group, executionContext = executionContext)
54-
Await.result(connection.connect, configuration.connectTimeout)
55-
54+
val future = configuration.statementTimeout match {
55+
case Some(timeout) => {
56+
connection.connect.flatMap(conn =>
57+
conn.sendQuery(s"SET statement_timeout TO ${timeout.toMillis};"))(executionContext)
58+
}
59+
case None => {
60+
connection.connect
61+
}
62+
}
63+
Await.result(future, configuration.connectTimeout)
5664
connection
5765
}
5866

0 commit comments

Comments
 (0)