From d53f909d202bc76ba3984d28c795d04641566192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 23 Oct 2024 18:46:38 +0200 Subject: [PATCH] chore: various cleanups --- caddy/caddy.go | 8 ++++---- caddy/php-cli.go | 2 +- caddy/php-server.go | 2 +- embed.go | 4 ++-- frankenphp.go | 8 +++----- internal/watcher/watcher.go | 4 ++-- metrics.go | 19 ++++++++++--------- request_options.go | 6 ++++-- worker.go | 4 ++-- 9 files changed, 29 insertions(+), 28 deletions(-) diff --git a/caddy/caddy.go b/caddy/caddy.go index 13ada1746..0ab9bce29 100644 --- a/caddy/caddy.go +++ b/caddy/caddy.go @@ -74,10 +74,10 @@ type FrankenPHPApp struct { } // CaddyModule returns the Caddy module information. -func (a FrankenPHPApp) CaddyModule() caddy.ModuleInfo { +func (f FrankenPHPApp) CaddyModule() caddy.ModuleInfo { return caddy.ModuleInfo{ ID: "frankenphp", - New: func() caddy.Module { return &a }, + New: func() caddy.Module { return &f }, } } @@ -189,7 +189,7 @@ func (f *FrankenPHPApp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { } if wc.FileName == "" { - return errors.New(`The "file" argument must be specified`) + return errors.New(`the "file" argument must be specified`) } if frankenphp.EmbeddedAppPath != "" && filepath.IsLocal(wc.FileName) { @@ -592,7 +592,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) // route to actually pass requests to PHP files; // match only requests that are for PHP files - pathList := []string{} + var pathList []string for _, ext := range extensions { pathList = append(pathList, "*"+ext) } diff --git a/caddy/php-cli.go b/caddy/php-cli.go index 3ff7b9481..3e75e20fd 100644 --- a/caddy/php-cli.go +++ b/caddy/php-cli.go @@ -25,7 +25,7 @@ Executes a PHP script similarly to the CLI SAPI.`, }) } -func cmdPHPCLI(fs caddycmd.Flags) (int, error) { +func cmdPHPCLI(caddycmd.Flags) (int, error) { args := os.Args[2:] if len(args) < 1 { return 1, errors.New("the path to the PHP script is required") diff --git a/caddy/php-server.go b/caddy/php-server.go index d4ad872df..9f77c8d64 100644 --- a/caddy/php-server.go +++ b/caddy/php-server.go @@ -179,7 +179,7 @@ func cmdPHPServer(fs caddycmd.Flags) (int, error) { // route to actually pass requests to PHP files; // match only requests that are for PHP files - pathList := []string{} + var pathList []string for _, ext := range extensions { pathList = append(pathList, "*"+ext) } diff --git a/embed.go b/embed.go index 07bd77102..b99ae6a7b 100644 --- a/embed.go +++ b/embed.go @@ -17,7 +17,7 @@ import ( "time" ) -// The path of the embedded PHP application (empty if none) +// EmbeddedAppPath contains the path of the embedded PHP application (empty if none) var EmbeddedAppPath string //go:embed app.tar @@ -35,7 +35,7 @@ func init() { appPath := filepath.Join(os.TempDir(), "frankenphp_"+string(embeddedAppChecksum)) if err := untar(appPath); err != nil { - os.RemoveAll(appPath) + _ = os.RemoveAll(appPath) panic(err) } diff --git a/frankenphp.go b/frankenphp.go index 8b22ac5b5..ec8081342 100644 --- a/frankenphp.go +++ b/frankenphp.go @@ -56,13 +56,11 @@ var contextKey = contextKeyStruct{} var ( InvalidRequestError = errors.New("not a FrankenPHP request") - AlreaydStartedError = errors.New("FrankenPHP is already started") + AlreadyStartedError = errors.New("FrankenPHP is already started") InvalidPHPVersionError = errors.New("FrankenPHP is only compatible with PHP 8.2+") - ZendSignalsError = errors.New("Zend Signals are enabled, recompile PHP with --disable-zend-signals") NotEnoughThreads = errors.New("the number of threads must be superior to the number of workers") MainThreadCreationError = errors.New("error creating the main thread") RequestContextCreationError = errors.New("error during request context creation") - RequestStartupError = errors.New("error during PHP request startup") ScriptExecutionError = errors.New("error during PHP script execution") requestChan chan *http.Request @@ -280,7 +278,7 @@ func calculateMaxThreads(opt *opt) error { // Init starts the PHP runtime and the configured workers. func Init(options ...Option) error { if requestChan != nil { - return AlreaydStartedError + return AlreadyStartedError } // Ignore all SIGPIPE signals to prevent weird issues with systemd: https://github.com/dunglas/frankenphp/issues/1020 @@ -371,7 +369,7 @@ func Shutdown() { // Remove the installed app if EmbeddedAppPath != "" { - os.RemoveAll(EmbeddedAppPath) + _ = os.RemoveAll(EmbeddedAppPath) } logger.Debug("FrankenPHP shut down") diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index e5aea4755..7cfb4bf2a 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -35,8 +35,8 @@ var ( reloadWaitGroup sync.WaitGroup // we are passing the logger from the main package to the watcher logger *zap.Logger - AlreadyStartedError = errors.New("The watcher is already running") - UnableToStartWatching = errors.New("Unable to start the watcher") + AlreadyStartedError = errors.New("the watcher is already running") + UnableToStartWatching = errors.New("unable to start the watcher") ) func InitWatcher(filePatterns []string, callback func(), zapLogger *zap.Logger) error { diff --git a/metrics.go b/metrics.go index 25f671fcc..29e4514d6 100644 --- a/metrics.go +++ b/metrics.go @@ -1,11 +1,12 @@ package frankenphp import ( - "github.com/prometheus/client_golang/prometheus" "path/filepath" "regexp" "sync" "time" + + "github.com/prometheus/client_golang/prometheus" ) var metricsNameRegex = regexp.MustCompile(`\W+`) @@ -43,19 +44,19 @@ type Metrics interface { type nullMetrics struct{} -func (n nullMetrics) StartWorker(name string) { +func (n nullMetrics) StartWorker(string) { } -func (n nullMetrics) ReadyWorker(name string) { +func (n nullMetrics) ReadyWorker(string) { } -func (n nullMetrics) StopWorker(name string, reason StopReason) { +func (n nullMetrics) StopWorker(string, StopReason) { } -func (n nullMetrics) TotalWorkers(name string, num int) { +func (n nullMetrics) TotalWorkers(string, int) { } -func (n nullMetrics) TotalThreads(num int) { +func (n nullMetrics) TotalThreads(int) { } func (n nullMetrics) StartRequest() { @@ -64,10 +65,10 @@ func (n nullMetrics) StartRequest() { func (n nullMetrics) StopRequest() { } -func (n nullMetrics) StopWorkerRequest(name string, duration time.Duration) { +func (n nullMetrics) StopWorkerRequest(string, time.Duration) { } -func (n nullMetrics) StartWorkerRequest(name string) { +func (n nullMetrics) StartWorkerRequest(string) { } func (n nullMetrics) Shutdown() { @@ -133,7 +134,7 @@ func (m *PrometheusMetrics) getIdentity(name string) (string, error) { return actualName, nil } -func (m *PrometheusMetrics) TotalWorkers(name string, num int) { +func (m *PrometheusMetrics) TotalWorkers(name string, _ int) { m.mu.Lock() defer m.mu.Unlock() diff --git a/request_options.go b/request_options.go index a369fd9cd..9784f0472 100644 --- a/request_options.go +++ b/request_options.go @@ -46,14 +46,16 @@ func WithRequestResolvedDocumentRoot(documentRoot string) RequestOption { } } +// WithRequestSplitPath contains a list of split path strings. +// // The path in the URL will be split into two, with the first piece ending -// with the value of SplitPath. The first piece will be assumed as the +// with the value of splitPath. The first piece will be assumed as the // actual resource (CGI script) name, and the second piece will be set to // PATH_INFO for the CGI script to use. // // Future enhancements should be careful to avoid CVE-2019-11043, // which can be mitigated with use of a try_files-like behavior -// that 404s if the fastcgi path info is not found. +// that 404s if the FastCGI path info is not found. func WithRequestSplitPath(splitPath []string) RequestOption { return func(o *FrankenPHPContext) error { o.splitPath = splitPath diff --git a/worker.go b/worker.go index 0a7fa0fb3..38e4b60a4 100644 --- a/worker.go +++ b/worker.go @@ -34,7 +34,7 @@ var ( workersAreReady atomic.Bool workersAreDone atomic.Bool workersDone chan interface{} - workers map[string]*worker = make(map[string]*worker) + workers = make(map[string]*worker) ) func initWorkers(opt []workerOpt) error { @@ -203,7 +203,7 @@ func drainWorkers() { } func restartWorkersOnFileChanges(workerOpts []workerOpt) error { - directoriesToWatch := []string{} + var directoriesToWatch []string for _, w := range workerOpts { directoriesToWatch = append(directoriesToWatch, w.watch...) }