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

Commit ac299f8

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 ac299f8

4 files changed

Lines changed: 8 additions & 4 deletions

File tree

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

Lines changed: 4 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._
@@ -60,6 +62,8 @@ case class Configuration(username: String,
6062
charset: Charset = Configuration.DefaultCharset,
6163
maximumMessageSize: Int = 16777216,
6264
allocator: ByteBufAllocator = PooledByteBufAllocator.DEFAULT,
65+
channelClass: Class[_ <: Channel] = classOf[NioSocketChannel],
66+
statementTimeout: Duration = 0.seconds,
6367
connectTimeout: Duration = 5.seconds,
6468
testTimeout: Duration = 5.seconds,
6569
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ 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 stmt = s"SET statement_timeout TO ${configuration.statementTimeout.toMillis};"
55+
Await.result(connection.connect.map(conn => conn.sendQuery(stmt))(executionContext), configuration.connectTimeout)
5656
connection
5757
}
5858

0 commit comments

Comments
 (0)