Skip to content

Commit

Permalink
fix: Do not set all SocketOptions by default (#765)
Browse files Browse the repository at this point in the history
Some of the SocketOptions are not supported on other platforms.
By default those should not be set.

Closes #764
  • Loading branch information
jakubjanecek authored Dec 16, 2021
1 parent 94cb4c7 commit 7e42ea3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ object Http4sBlazeServerConfig {

final case class SocketOptions(
tcpNoDelay: Boolean = true,
soKeepAlive: Boolean = true,
soReuseAddr: Boolean = true,
soReusePort: Boolean = true
soKeepAlive: Option[Boolean] = None,
soReuseAddr: Option[Boolean] = None,
soReusePort: Option[Boolean] = None
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import org.http4s.HttpApp
import org.http4s.blaze.server.BlazeServerBuilder
import org.http4s.server.Server

import java.net.{InetSocketAddress, StandardSocketOptions}
import java.net.StandardSocketOptions.{SO_KEEPALIVE, SO_REUSEADDR, SO_REUSEPORT}
import java.net.{InetSocketAddress, SocketOption, StandardSocketOptions}
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.Duration

Expand All @@ -27,8 +28,8 @@ object Http4sBlazeServerModule {
InetSocketAddress.createUnresolved(config.listenAddress, config.listenPort)
)
)
server <-
BlazeServerBuilder[F](executionContext)
server <- {
val builder = BlazeServerBuilder[F](executionContext)
.bindSocketAddress(inetSocketAddress)
.withHttpApp(httpApp)
.withoutBanner
Expand All @@ -43,10 +44,23 @@ object Http4sBlazeServerModule {
.withConnectorPoolSize(config.connectorPoolSize)
.withMaxConnections(config.maxConnections)
.withChannelOption[java.lang.Boolean](StandardSocketOptions.TCP_NODELAY, config.socketOptions.tcpNoDelay)
.withChannelOption[java.lang.Boolean](StandardSocketOptions.SO_KEEPALIVE, config.socketOptions.soKeepAlive)
.withChannelOption[java.lang.Boolean](StandardSocketOptions.SO_REUSEADDR, config.socketOptions.soReuseAddr)
.withChannelOption[java.lang.Boolean](StandardSocketOptions.SO_REUSEPORT, config.socketOptions.soReusePort)
.resource

val optionalOptions = {
import config.socketOptions._

def set(builder: BlazeServerBuilder[F], value: Option[Boolean], option: SocketOption[java.lang.Boolean]): BlazeServerBuilder[F] =
value.map(builder.withChannelOption[java.lang.Boolean](option, _)).getOrElse(builder)

List[BlazeServerBuilder[F] => BlazeServerBuilder[F]](
b => set(b, soKeepAlive, SO_KEEPALIVE),
b => set(b, soReuseAddr, SO_REUSEADDR),
b => set(b, soReusePort, SO_REUSEPORT)
)
}

val updatedBuilder = optionalOptions.foldLeft(builder) { (b, configure) => configure(b) }
updatedBuilder.resource
}
} yield server
}
}

0 comments on commit 7e42ea3

Please sign in to comment.