-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sam Batschelet <sbatsche@redhat.com>
- Loading branch information
Showing
12 changed files
with
232 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package transport | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
type Controls []func(network, addr string, conn syscall.RawConn) error | ||
|
||
func (ctls Controls) Control(network, addr string, conn syscall.RawConn) error { | ||
for _, s := range ctls { | ||
if err := s(network, addr, conn); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type SocketOpts struct { | ||
// ReusePort enables socket option SO_REUSEPORT [1] which allows rebind of | ||
// a port already in use. User should keep in mind that flock can fail | ||
// in which case lock on data file could result in unexpected | ||
// condition. User should take caution to protect against lock race. | ||
// [1] https://man7.org/linux/man-pages/man7/socket.7.html | ||
ReusePort bool | ||
// ReuseAddress enables a socket option SO_REUSEADDR which allows | ||
// binding to an address in `TIME_WAIT` state. Useful to improve MTTR | ||
// in cases where etcd slow to restart due to excessive `TIME_WAIT`. | ||
// [1] https://man7.org/linux/man-pages/man7/socket.7.html | ||
ReuseAddress bool | ||
} | ||
|
||
func getControls(sopts *SocketOpts) Controls { | ||
ctls := Controls{} | ||
if sopts.ReuseAddress { | ||
ctls = append(ctls, setReuseAddress) | ||
} | ||
if sopts.ReusePort { | ||
ctls = append(ctls, setReusePort) | ||
} | ||
return ctls | ||
} | ||
|
||
func (sopts *SocketOpts) Empty() bool { | ||
return sopts.ReuseAddress == false && sopts.ReusePort == false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// +build !windows | ||
|
||
package transport | ||
|
||
import ( | ||
"golang.org/x/sys/unix" | ||
"syscall" | ||
) | ||
|
||
func setReusePort(network, address string, conn syscall.RawConn) error { | ||
return conn.Control(func(fd uintptr) { | ||
syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1) | ||
}) | ||
} | ||
|
||
func setReuseAddress(network, address string, conn syscall.RawConn) error { | ||
return conn.Control(func(fd uintptr) { | ||
syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEADDR, 1) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// +build windows | ||
|
||
package transport | ||
|
||
import ( | ||
"fmt" | ||
"syscall" | ||
) | ||
|
||
func setReusePort(network, address string, c syscall.RawConn) error { | ||
return fmt.Errorf("port reuse is not supported on Windows") | ||
} | ||
|
||
// Windows supports SO_REUSEADDR, but it may cause undefined behavior, as | ||
// there is no protection against port hijacking. | ||
func setReuseAddress(network, addr string, conn syscall.RawConn) error { | ||
return fmt.Errorf("address reuse is not supported on Windows") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters