Skip to content

fix: Do not set all SocketOptions by default #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2021
Merged
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 @@ -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
}
}