Skip to content

Commit 7e42ea3

Browse files
authored
fix: Do not set all SocketOptions by default (#765)
Some of the SocketOptions are not supported on other platforms. By default those should not be set. Closes #764
1 parent 94cb4c7 commit 7e42ea3

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

http4s-server-blaze/src/main/scala/com/avast/sst/http4s/server/Http4sBlazeServerConfig.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ object Http4sBlazeServerConfig {
2929

3030
final case class SocketOptions(
3131
tcpNoDelay: Boolean = true,
32-
soKeepAlive: Boolean = true,
33-
soReuseAddr: Boolean = true,
34-
soReusePort: Boolean = true
32+
soKeepAlive: Option[Boolean] = None,
33+
soReuseAddr: Option[Boolean] = None,
34+
soReusePort: Option[Boolean] = None
3535
)
3636

3737
}

http4s-server-blaze/src/main/scala/com/avast/sst/http4s/server/Http4sBlazeServerModule.scala

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import org.http4s.HttpApp
55
import org.http4s.blaze.server.BlazeServerBuilder
66
import org.http4s.server.Server
77

8-
import java.net.{InetSocketAddress, StandardSocketOptions}
8+
import java.net.StandardSocketOptions.{SO_KEEPALIVE, SO_REUSEADDR, SO_REUSEPORT}
9+
import java.net.{InetSocketAddress, SocketOption, StandardSocketOptions}
910
import scala.concurrent.ExecutionContext
1011
import scala.concurrent.duration.Duration
1112

@@ -27,8 +28,8 @@ object Http4sBlazeServerModule {
2728
InetSocketAddress.createUnresolved(config.listenAddress, config.listenPort)
2829
)
2930
)
30-
server <-
31-
BlazeServerBuilder[F](executionContext)
31+
server <- {
32+
val builder = BlazeServerBuilder[F](executionContext)
3233
.bindSocketAddress(inetSocketAddress)
3334
.withHttpApp(httpApp)
3435
.withoutBanner
@@ -43,10 +44,23 @@ object Http4sBlazeServerModule {
4344
.withConnectorPoolSize(config.connectorPoolSize)
4445
.withMaxConnections(config.maxConnections)
4546
.withChannelOption[java.lang.Boolean](StandardSocketOptions.TCP_NODELAY, config.socketOptions.tcpNoDelay)
46-
.withChannelOption[java.lang.Boolean](StandardSocketOptions.SO_KEEPALIVE, config.socketOptions.soKeepAlive)
47-
.withChannelOption[java.lang.Boolean](StandardSocketOptions.SO_REUSEADDR, config.socketOptions.soReuseAddr)
48-
.withChannelOption[java.lang.Boolean](StandardSocketOptions.SO_REUSEPORT, config.socketOptions.soReusePort)
49-
.resource
47+
48+
val optionalOptions = {
49+
import config.socketOptions._
50+
51+
def set(builder: BlazeServerBuilder[F], value: Option[Boolean], option: SocketOption[java.lang.Boolean]): BlazeServerBuilder[F] =
52+
value.map(builder.withChannelOption[java.lang.Boolean](option, _)).getOrElse(builder)
53+
54+
List[BlazeServerBuilder[F] => BlazeServerBuilder[F]](
55+
b => set(b, soKeepAlive, SO_KEEPALIVE),
56+
b => set(b, soReuseAddr, SO_REUSEADDR),
57+
b => set(b, soReusePort, SO_REUSEPORT)
58+
)
59+
}
60+
61+
val updatedBuilder = optionalOptions.foldLeft(builder) { (b, configure) => configure(b) }
62+
updatedBuilder.resource
63+
}
5064
} yield server
5165
}
5266
}

0 commit comments

Comments
 (0)