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

Allow the use of alternative net.Listener implementations by downstreams #25855

Merged
merged 7 commits into from
Jul 24, 2023
8 changes: 5 additions & 3 deletions modules/graceful/net_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@ func CloseProvidedListeners() error {
return returnableError
}

// GetListener obtains a listener for the local network address. The network must be
// DefaultGetListener obtains a listener for the local network address. The network must be
// a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket". It
// returns an provided net.Listener for the matching network and address, or
// creates a new one using net.Listen.
func GetListener(network, address string) (net.Listener, error) {
// creates a new one using net.Listen. This function can be replaced by changing the
// GetListener variable at the top of this file, for example to listen on an onion service using
// github.com/cretz/bine
func DefaultGetListener(network, address string) (net.Listener, error) {
// Add a deferral to say that we've tried to grab a listener
defer GetManager().InformCleanup()
switch network {
Expand Down
8 changes: 5 additions & 3 deletions modules/graceful/net_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ package graceful

import "net"

// GetListener obtains a listener for the local network address.
// On windows this is basically just a shim around net.Listen.
func GetListener(network, address string) (net.Listener, error) {
// DefaultGetListener obtains a listener for the local network address.
// On windows this is basically just a shim around net.Listen. This function
// can be replaced by changing the GetListener variable at the top of this file,
// for example to listen on an onion service using github.com/cretz/bine
func DefaultGetListener(network, address string) (net.Listener, error) {
// Add a deferral to say that we've tried to grab a listener
defer GetManager().InformCleanup()

Expand Down
7 changes: 7 additions & 0 deletions modules/graceful/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ var (
PerWriteWriteTimeoutKbTime = 10 * time.Second
)

// GetListener returns a listener from a GetListener function, which must have the
// signature: `func FunctioName(network, address string) (net.Listener, error)`.
// This determines the implementation of net.Listener which the server will use.`
// It is implemented in this way so that downstreams may specify the type of listener
// they want to provide Gitea on by default, such as with a hidden service or a p2p network
var GetListener = DefaultGetListener
lunny marked this conversation as resolved.
Show resolved Hide resolved

func init() {
DefaultMaxHeaderBytes = 0 // use http.DefaultMaxHeaderBytes - which currently is 1 << 20 (1MB)
}
Expand Down