From da0ce85b4b392acb0e99735b38c46acbd9f2a264 Mon Sep 17 00:00:00 2001 From: idk Date: Tue, 30 May 2023 03:04:39 +0000 Subject: [PATCH 1/4] allow the use of an alternate GetListener function, which can return any type of listener --- modules/graceful/net_unix.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/graceful/net_unix.go b/modules/graceful/net_unix.go index e9c1285123080..4abd539c3dbb2 100644 --- a/modules/graceful/net_unix.go +++ b/modules/graceful/net_unix.go @@ -150,11 +150,13 @@ func CloseProvidedListeners() error { return returnableError } +var GetListener = DefaultGetListener + // GetListener 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) { +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 { From 6431ca1a878de95432b6250f027f9a5feb26c171 Mon Sep 17 00:00:00 2001 From: eyedeekay Date: Thu, 13 Jul 2023 10:49:20 -0400 Subject: [PATCH 2/4] Add comments to explain how to use the GetListener variable, add a GetListener/DefaultGetListener pair on Windows with same comments/usage --- modules/graceful/net_unix.go | 13 +++++++++---- modules/graceful/net_windows.go | 13 ++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/modules/graceful/net_unix.go b/modules/graceful/net_unix.go index 4abd539c3dbb2..85d6d827250db 100644 --- a/modules/graceful/net_unix.go +++ b/modules/graceful/net_unix.go @@ -30,6 +30,11 @@ const ( watchdogTimeoutEnv = "WATCHDOG_USEC" ) +// 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.` +var GetListener = DefaultGetListener + // In order to keep the working directory the same as when we started we record // it at startup. var originalWD, _ = os.Getwd() @@ -150,12 +155,12 @@ func CloseProvidedListeners() error { return returnableError } -var GetListener = DefaultGetListener - -// 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. +// 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() diff --git a/modules/graceful/net_windows.go b/modules/graceful/net_windows.go index a2f58e224a944..efa623e064ca5 100644 --- a/modules/graceful/net_windows.go +++ b/modules/graceful/net_windows.go @@ -9,9 +9,16 @@ 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) { +// 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.` +var GetListener = DefaultGetListener + +// 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() From 6dcb29be7957262e95246387774306c283c52962 Mon Sep 17 00:00:00 2001 From: eyedeekay Date: Mon, 17 Jul 2023 12:31:55 -0400 Subject: [PATCH 3/4] Move GetListener to server.go per @wxiaoguang's advice --- modules/graceful/net_unix.go | 5 ----- modules/graceful/net_windows.go | 5 ----- modules/graceful/server.go | 7 +++++++ 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/modules/graceful/net_unix.go b/modules/graceful/net_unix.go index 85d6d827250db..f5af1e39378ab 100644 --- a/modules/graceful/net_unix.go +++ b/modules/graceful/net_unix.go @@ -30,11 +30,6 @@ const ( watchdogTimeoutEnv = "WATCHDOG_USEC" ) -// 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.` -var GetListener = DefaultGetListener - // In order to keep the working directory the same as when we started we record // it at startup. var originalWD, _ = os.Getwd() diff --git a/modules/graceful/net_windows.go b/modules/graceful/net_windows.go index efa623e064ca5..15d228d6b6540 100644 --- a/modules/graceful/net_windows.go +++ b/modules/graceful/net_windows.go @@ -9,11 +9,6 @@ package graceful import "net" -// 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.` -var GetListener = DefaultGetListener - // 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, diff --git a/modules/graceful/server.go b/modules/graceful/server.go index e42d35cd49b80..cf204f09948c9 100644 --- a/modules/graceful/server.go +++ b/modules/graceful/server.go @@ -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 + func init() { DefaultMaxHeaderBytes = 0 // use http.DefaultMaxHeaderBytes - which currently is 1 << 20 (1MB) } From f214b3dff0068a6e4532e4eeae33ca3db15abe52 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 24 Jul 2023 14:47:30 +0800 Subject: [PATCH 4/4] Update modules/graceful/server.go Co-authored-by: wxiaoguang --- modules/graceful/server.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/graceful/server.go b/modules/graceful/server.go index cf204f09948c9..bd917828bc862 100644 --- a/modules/graceful/server.go +++ b/modules/graceful/server.go @@ -38,6 +38,7 @@ var ( // 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 +// No need to worry about "breaking" if there would be a refactoring for the Listeners. No compatibility-guarantee for this mechanism var GetListener = DefaultGetListener func init() {