mirror of
https://github.com/zrepl/zrepl.git
synced 2024-12-18 13:10:56 +01:00
28 lines
522 B
Go
28 lines
522 B
Go
|
package tcpsock
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"net"
|
||
|
"syscall"
|
||
|
)
|
||
|
|
||
|
func Listen(address string, tryFreeBind bool) (*net.TCPListener, error) {
|
||
|
control := func(network, address string, c syscall.RawConn) error {
|
||
|
if tryFreeBind {
|
||
|
if err := freeBind(network, address, c); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
var listenConfig = net.ListenConfig{
|
||
|
Control: control,
|
||
|
}
|
||
|
|
||
|
l, err := listenConfig.Listen(context.Background(), "tcp", address)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return l.(*net.TCPListener), nil
|
||
|
}
|