Skip to content
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

🔌 Detect TCP protcol based on Addr #633

Merged
merged 10 commits into from
Jul 18, 2020
5 changes: 3 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,9 @@ func (app *App) Listen(address interface{}, tlsconfig ...*tls.Config) error {
app.init()
// Start prefork
if app.Settings.Prefork {
if app.Settings.Network == "tcp6" || isIPv6(addr) {
return fmt.Errorf("listen: tcp6 is not supported when prefork is enabled")
// Prefork only supports tcp4 or tcp6, but not both
if isIPv6(addr) {
app.Settings.Network = "tcp6"
}
return app.prefork(addr, tlsconfig...)
}
Expand Down
9 changes: 7 additions & 2 deletions prefork.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) {
// use 1 cpu core per child process
runtime.GOMAXPROCS(1)
var ln net.Listener
// SO_REUSEPORT is not supported on Windows, use SO_REUSEADDR instead
if ln, err = reuseport.Listen("tcp4", addr); err != nil {
network := "tcp4"
if app.Settings.Network == "tcp6" {
network = app.Settings.Network
}
// Linux will use SO_REUSEPORT and Windows falls back to SO_REUSEADDR
// Only tcp4 or tcp6 is supported when preforking, both are not supported
if ln, err = reuseport.Listen(network, addr); err != nil {
if !app.Settings.DisableStartupMessage {
time.Sleep(100 * time.Millisecond) // avoid colliding with startup message
}
Expand Down