Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.netty.buffer.ByteBufAllocator
import io.netty.buffer.PooledByteBufAllocator
import io.netty.channel.EventLoopGroup
import io.netty.util.CharsetUtil
import java.net.SocketAddress
import java.nio.charset.Charset
import java.time.Duration
import java.util.concurrent.Executor
Expand Down Expand Up @@ -54,5 +55,6 @@ data class Configuration @JvmOverloads constructor(
val applicationName: String? = null,
val interceptors: List<Supplier<QueryInterceptor>> = emptyList(),
val eventLoopGroup: EventLoopGroup = NettyUtils.DefaultEventLoopGroup,
val executionContext: Executor = ExecutorServiceUtils.CommonPool
val executionContext: Executor = ExecutorServiceUtils.CommonPool,
val unixSocket: SocketAddress? = null
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.github.jasync.sql.db.util

import io.netty.channel.Channel
import io.netty.channel.EventLoopGroup
import io.netty.channel.epoll.Epoll
import io.netty.channel.epoll.EpollDomainSocketChannel
import io.netty.channel.epoll.EpollEventLoopGroup
import io.netty.channel.epoll.EpollSocketChannel
import io.netty.channel.kqueue.KQueue
import io.netty.channel.kqueue.KQueueDomainSocketChannel
import io.netty.channel.kqueue.KQueueEventLoopGroup
import io.netty.channel.kqueue.KQueueSocketChannel
import io.netty.channel.nio.NioEventLoopGroup
Expand All @@ -16,6 +19,8 @@ import mu.KotlinLogging

private val logger = KotlinLogging.logger {}

fun EventLoopGroup.domainSocketCompatible() = this is KQueueEventLoopGroup || this is EpollEventLoopGroup

object NettyUtils {

init {
Expand All @@ -35,10 +40,13 @@ object NettyUtils {
}
}

fun getSocketChannelClass(eventLoopGroup: EventLoopGroup): Class<out SocketChannel> = when {
tryOrFalse { eventLoopGroup is EpollEventLoopGroup } -> EpollSocketChannel::class.java
tryOrFalse { eventLoopGroup is KQueueEventLoopGroup } -> KQueueSocketChannel::class.java
else -> NioSocketChannel::class.java
fun getSocketChannelClass(eventLoopGroup: EventLoopGroup, useDomainSocket: Boolean = false): Class<out Channel> = when {
tryOrFalse { eventLoopGroup is EpollEventLoopGroup } -> if (useDomainSocket) EpollDomainSocketChannel::class.java else EpollSocketChannel::class.java
tryOrFalse { eventLoopGroup is KQueueEventLoopGroup } -> if (useDomainSocket) KQueueDomainSocketChannel::class.java else KQueueSocketChannel::class.java
else -> {
logger.info { "domain socket is not supported by NioEventLoopGroup, useDomainSocket flag is skipped" }
NioSocketChannel::class.java
}
}

private fun tryOrFalse(fn: () -> Boolean): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,7 @@ import com.github.jasync.sql.db.mysql.message.server.PreparedStatementPrepareRes
import com.github.jasync.sql.db.mysql.message.server.ResultSetRowMessage
import com.github.jasync.sql.db.mysql.message.server.ServerMessage
import com.github.jasync.sql.db.mysql.util.CharsetMapper
import com.github.jasync.sql.db.util.ExecutorServiceUtils
import com.github.jasync.sql.db.util.FP
import com.github.jasync.sql.db.util.NettyUtils
import com.github.jasync.sql.db.util.XXX
import com.github.jasync.sql.db.util.failed
import com.github.jasync.sql.db.util.flatMapAsync
import com.github.jasync.sql.db.util.head
import com.github.jasync.sql.db.util.installOnFuture
import com.github.jasync.sql.db.util.length
import com.github.jasync.sql.db.util.onFailure
import com.github.jasync.sql.db.util.tail
import com.github.jasync.sql.db.util.toCompletableFuture
import com.github.jasync.sql.db.util.*
import io.netty.bootstrap.Bootstrap
import io.netty.buffer.ByteBuf
import io.netty.buffer.ByteBufAllocator
Expand Down Expand Up @@ -81,7 +70,8 @@ class MySQLConnectionHandler(
private var isPreparedStatement: Boolean? = null

fun connect(): CompletableFuture<MySQLConnectionHandler> {
this.bootstrap.channel(NettyUtils.getSocketChannelClass(this.group))
val useDomainSocket = this.configuration.unixSocket != null && this.group.domainSocketCompatible()
this.bootstrap.channel(NettyUtils.getSocketChannelClass(this.group, useDomainSocket))
this.bootstrap.handler(object : ChannelInitializer<Channel>() {

override fun initChannel(channel: Channel) {
Expand All @@ -100,7 +90,7 @@ class MySQLConnectionHandler(
this.bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.configuration.connectionTimeout)

val channelFuture: ChannelFuture =
this.bootstrap.connect(InetSocketAddress(configuration.host, configuration.port))
this.bootstrap.connect(if (useDomainSocket) configuration.unixSocket else InetSocketAddress(configuration.host, configuration.port))
channelFuture.onFailure(executionContext) { exception ->
this.connectionPromise.completeExceptionally(exception)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ import com.github.jasync.sql.db.postgresql.messages.frontend.ClientMessage
import com.github.jasync.sql.db.postgresql.messages.frontend.CloseMessage
import com.github.jasync.sql.db.postgresql.messages.frontend.SSLRequestMessage
import com.github.jasync.sql.db.postgresql.messages.frontend.StartupMessage
import com.github.jasync.sql.db.util.ExecutorServiceUtils
import com.github.jasync.sql.db.util.Failure
import com.github.jasync.sql.db.util.NettyUtils
import com.github.jasync.sql.db.util.Success
import com.github.jasync.sql.db.util.failed
import com.github.jasync.sql.db.util.onCompleteAsync
import com.github.jasync.sql.db.util.onFailure
import com.github.jasync.sql.db.util.success
import com.github.jasync.sql.db.util.toCompletableFuture
import com.github.jasync.sql.db.util.*
import io.netty.bootstrap.Bootstrap
import io.netty.channel.Channel
import io.netty.channel.ChannelHandlerContext
Expand Down Expand Up @@ -73,8 +65,9 @@ class PostgreSQLConnectionHandler(
private var currentContext: ChannelHandlerContext? = null

fun connect(): CompletableFuture<PostgreSQLConnectionHandler> {
val useDomainSocket = this.configuration.unixSocket != null && this.group.domainSocketCompatible()
this.bootstrap.group(this.group)
this.bootstrap.channel(NettyUtils.getSocketChannelClass(this.group))
this.bootstrap.channel(NettyUtils.getSocketChannelClass(this.group, useDomainSocket))
this.bootstrap.handler(object : ChannelInitializer<Channel>() {
override fun initChannel(ch: Channel) {
ch.pipeline().addLast(
Expand All @@ -91,7 +84,7 @@ class PostgreSQLConnectionHandler(
this.bootstrap.option<Boolean>(ChannelOption.SO_KEEPALIVE, true)
this.bootstrap.option(ChannelOption.ALLOCATOR, configuration.allocator)
this.bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.configuration.connectionTimeout)
this.bootstrap.connect(InetSocketAddress(configuration.host, configuration.port))
this.bootstrap.connect(if (useDomainSocket) configuration.unixSocket else InetSocketAddress(configuration.host, configuration.port))
.onFailure(executionContext) { e ->
connectionFuture.failed(e)
}
Expand Down