Skip to content

Commit

Permalink
Merge pull request #113 from armanbilge/pr/i112
Browse files Browse the repository at this point in the history
Default `SO_REUSEADDR=true` for server socket
  • Loading branch information
armanbilge authored Feb 19, 2023
2 parents b261380 + 233aeee commit 2cb3104
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ lazy val tests = crossProject(JVMPlatform, NativePlatform)
"org.typelevel" %%% "munit-cats-effect" % munitCEVersion % Test
)
)
.jvmSettings(fork := true)

lazy val example = project
.in(file("example"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ object EpollAsyncServerSocketChannel {
case epoll: EventPollingExecutorScheduler =>
val fd = SocketHelpers.mkNonBlocking()
val ch = new EpollAsyncServerSocketChannel(fd)
ch.setOption(StandardSocketOptions.SO_REUSEADDR, java.lang.Boolean.TRUE)
ch.unmonitor = epoll.monitor(fd, reads = true, writes = false)(ch)
ch
case _ =>
Expand Down
15 changes: 15 additions & 0 deletions tests/shared/src/test/scala/epollcat/TcpSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,19 @@ class TcpSuite extends EpollcatSuite {
IOServerSocketChannel.open.use_
}

test("closing/re-opening a server does not throw BindException: Address already in use") {
IOServerSocketChannel
.open
.evalTap(_.bind(new InetSocketAddress("localhost", 0)))
.use { server =>
server.localAddress.flatTap { address =>
val connect =
IOSocketChannel.open.evalTap(_.connect(address)).surround(IO.sleep(1.second))
val accept = server.accept.surround(IO.sleep(1.second))
connect.both(accept).void
}
}
.flatMap(address => IOServerSocketChannel.open.evalTap(_.bind(address)).use_)
}

}

0 comments on commit 2cb3104

Please sign in to comment.