fs2 icon indicating copy to clipboard operation
fs2 copied to clipboard

Unify TCP/UDP and UnixSocket APIs

Open mpilquist opened this issue 8 months ago • 2 comments

Fixes #3560

This PR revamps fs2.io.net in a number of significant ways:

  • Unifies support for IP and Unix sockets in to a single set of operations -- connect for clients and bind for servers, both taking the new com.comcast.ip4s.GenSocketAddress type, which is a supertype of SocketAddress[Host] and UnixSocketAddress
  • Resultantly adds support for socket options on unix sockets
  • Deprecates SocketGroup - there has never really been a great reason to manage explicit thread pools / async socket groups & the API was JVM only. With the new polling system, there's even less of a reason to ever use the explicitly managed socket groups.
  • Deprecates UnixSockets as there's no utility provided that's not also provided by Network now
  • Deprecates client, server, serverResource in favor of connect, bindAndAccept, bind
  • Introduces ServerSocket, which provides the bound address of the server socket, access to options, and a stream of client sockets.
  • Introduces SocketInfo as a supertype of Socket and ServerSocket.
  • Introduces address: GenSocketAddress on SocketInfo and peerAddress: GenSocketAddress on Socket, replacing localAddress and remoteAddress (each of type F[SocketAddress[IpAddress]].
  • Deprecating isOpen on Socket which is a vestige from when there was a close method.

Datagram sockets have been similarly revamped:

  • openDatagramSocket has been replaced with bindDatagramSocket, taking a GenSocketAddress
  • As of this PR, datagrams over unix sockets are only supported on the JVM, via jnr-unixsocket (JEP-380 doesn't support datagrams). Node.js doesn't have built-in support for datagrams over unix sockets, though we could add via a dependency. Datagram sockets, in general, are not currently implemented on native.

Updates to downstream projects:

  • https://github.com/http4s/http4s/pull/7675/
  • https://github.com/typelevel/fs2-chat/commit/c4f264324b2c61d876b9d064dd61fd01c5c603ba

TODO:

  • Finish conversion of DatagramSocketOption to SocketOption
  • Finish implementation of supportedOptions / getOption / setOption on all platforms and socket types
  • Implement a cross platform NetworkInterface

mpilquist avatar Apr 08 '25 11:04 mpilquist

Need to lazily load socket options that were added in Java 9+. Here's an example failure with 3.13.0-M2 and http4s tests running on Java 8:

java.lang.NoSuchFieldError: SO_REUSEPORT
	at fs2.io.net.SocketOptionCompanionPlatform.$init$(SocketOptionPlatform.scala:75)
	at fs2.io.net.SocketOption$.<clinit>(SocketOption.scala:36)
	at org.http4s.ember.server.internal.ServerHelpers$.$anonfun$unixSocketServer$1(ServerHelpers.scala:150)

mpilquist avatar May 05 '25 14:05 mpilquist

UnixDatagramSuite on Linux is failing due to https://github.com/jnr/jnr-unixsocket/pull/107. Will exclude it for now.

mpilquist avatar Jun 05 '25 17:06 mpilquist

Going to merge this one this week unless there's any objections.

mpilquist avatar Aug 26 '25 11:08 mpilquist