-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement net.Listener hand-over during config reload
Log library refactored a little to make it easier to enable debug logging.
- Loading branch information
Showing
19 changed files
with
394 additions
and
44 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
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,27 @@ | ||
package netresource | ||
|
||
import "net" | ||
|
||
func dupTCPListener(l *net.TCPListener) (*net.TCPListener, error) { | ||
f, err := l.File() | ||
if err != nil { | ||
return nil, err | ||
} | ||
l2, err := net.FileListener(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return l2.(*net.TCPListener), nil | ||
} | ||
|
||
func dupUnixListener(l *net.UnixListener) (*net.UnixListener, error) { | ||
f, err := l.File() | ||
if err != nil { | ||
return nil, err | ||
} | ||
l2, err := net.FileListener(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return l2.(*net.UnixListener), nil | ||
} |
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,47 @@ | ||
package netresource | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func ListenFD(fd uint) (net.Listener, error) { | ||
file := os.NewFile(uintptr(fd), strconv.FormatUint(uint64(fd), 10)) | ||
defer file.Close() | ||
return net.FileListener(file) | ||
} | ||
|
||
func ListenFDName(name string) (net.Listener, error) { | ||
listenPDStr := os.Getenv("LISTEN_PID") | ||
if listenPDStr == "" { | ||
return nil, errors.New("$LISTEN_PID is not set") | ||
} | ||
listenPid, err := strconv.Atoi(listenPDStr) | ||
if err != nil { | ||
return nil, errors.New("$LISTEN_PID is not integer") | ||
} | ||
if listenPid != os.Getpid() { | ||
return nil, fmt.Errorf("$LISTEN_PID (%d) is not our PID (%d)", listenPid, os.Getpid()) | ||
} | ||
|
||
names := strings.Split(os.Getenv("LISTEN_FDNAMES"), ":") | ||
fd := uintptr(0) | ||
for i, fdName := range names { | ||
if fdName == name { | ||
fd = uintptr(3 + i) | ||
break | ||
} | ||
} | ||
|
||
if fd == 0 { | ||
return nil, fmt.Errorf("name %s not found in $LISTEN_FDNAMES", name) | ||
} | ||
|
||
file := os.NewFile(3+fd, name) | ||
defer file.Close() | ||
return net.FileListener(file) | ||
} |
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,38 @@ | ||
package netresource | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
|
||
"github.com/foxcpp/maddy/framework/log" | ||
) | ||
|
||
var ( | ||
tracker = NewListenerTracker(log.DefaultLogger.Sublogger("netresource")) | ||
) | ||
|
||
func CloseUnusedListeners() error { | ||
return tracker.Close() | ||
} | ||
|
||
func ResetListenersUsage() { | ||
tracker.ResetUsage() | ||
} | ||
|
||
func Listen(network, addr string) (net.Listener, error) { | ||
switch network { | ||
case "fd": | ||
fd, err := strconv.ParseUint(addr, 10, strconv.IntSize) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid FD number: %v", addr) | ||
} | ||
return ListenFD(uint(fd)) | ||
case "fdname": | ||
return ListenFDName(addr) | ||
case "tcp", "tcp4", "tcp6", "unix": | ||
return tracker.Get(network, addr) | ||
default: | ||
return nil, fmt.Errorf("unsupported network: %v", network) | ||
} | ||
} |
Oops, something went wrong.