go_reuseport icon indicating copy to clipboard operation
go_reuseport copied to clipboard

UDP listens on TCP port

Open gen2brain opened this issue 6 years ago • 6 comments
trafficstars

The following example will reuse ports but will listen on TCP instead of UDP:

package main

import (
	"fmt"
	"os"

	"github.com/kavu/go_reuseport"
)

func main() {
	l1, err := reuseport.Listen("udp", ":514")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	l2, err := reuseport.Listen("udp", ":514")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println(l1, l2)

	select {}
}
# netstat -tulpn | grep 514
tcp        0      0 0.0.0.0:514             0.0.0.0:*               LISTEN      21168/./test2       
tcp        0      0 0.0.0.0:514             0.0.0.0:*               LISTEN      21168/./test2

gen2brain avatar Apr 24 '19 11:04 gen2brain

Everything is ok with ListenPacket when I use UDP.

gen2brain avatar Apr 24 '19 12:04 gen2brain

@gen2brain thank you for your report!

In fact it is an somewhat expected behaviour. But I am not sure why Listen does not return an err on udp, as ListenPacket should panic on tcp. I'll test it again, and probably will return a proper errors.

kavu avatar Apr 24 '19 13:04 kavu

Also there is no "Listen" method for Windows, which breaks multi OS builds.

https://github.com/wjiec/greuseport seems to have better implementation.

yusufozturk avatar Oct 17 '21 10:10 yusufozturk

@yusufozturk Thank you for your feedback, Yusuf! What do you mean by "Also there is no "Listen" method for Windows"? This package doesn't support Windows, yes. But it should still be compatible to build your package on multiple OS's without breaking the build itself — Windows version has a stub for NewReusablePortListener and NewReusablePortPacketConn. Have you faced some difficulties with it? Can you provide an example or elaborate more?

kavu avatar Oct 18 '21 07:10 kavu

Hi @kavu,

I would expect to have one interface for all OS types. If OS does not support the operation, returns back the standard net.Listener. Right now, in this package, I should use .Listen() for Unix devices, .NewReusablePortListener() for Windows devices. So it's breaking multi OS builds. To make it work, I can write a wrapper in front of this library but library should support this for a better implementation.

Here is another library and they just expose .Listen() and .ListenPacket() for all OS types: https://github.com/libp2p/go-reuseport

Please don't get me wrong, I just want to share different examples to show how it is in the other libraries. This is not about if library works or not, it's just about how the implementation should be.

Thanks for this great library. I discovered reuseport with your library.

yusufozturk avatar Oct 18 '21 07:10 yusufozturk

Right now, in this package, I should use .Listen() for Unix devices, .NewReusablePortListener() for Windows devices. So it's breaking multi OS builds. To make it work, I can write a wrapper in front of this library but library should support this for a better implementation.

Yeah, I see it. My bad. I messed up the build tags. I thought that

https://github.com/kavu/go_reuseport/blob/fd621afe35d03da136129a2cabe4cc742e898fa7/reuseport.go#L42-L50 will be available for Windows, but it is not, unfortunately. due to

https://github.com/kavu/go_reuseport/blob/fd621afe35d03da136129a2cabe4cc742e898fa7/reuseport.go#L1

I will revisit build tags and try to make it work for Windows too. @yusufozturk can I ask you to try to build your app with upcoming changes? If not, that's okay, no worries.

Still, Windows won't get the support of actual reuseport, that's out of my scope for now.

kavu avatar Oct 18 '21 08:10 kavu