Browse Source

chore: various cleanups

Kévin Dunglas 4 months ago
parent
commit
d53f909d20
9 changed files with 29 additions and 28 deletions
  1. 4 4
      caddy/caddy.go
  2. 1 1
      caddy/php-cli.go
  3. 1 1
      caddy/php-server.go
  4. 2 2
      embed.go
  5. 3 5
      frankenphp.go
  6. 2 2
      internal/watcher/watcher.go
  7. 10 9
      metrics.go
  8. 4 2
      request_options.go
  9. 2 2
      worker.go

+ 4 - 4
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)
 	}

+ 1 - 1
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")

+ 1 - 1
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)
 	}

+ 2 - 2
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)
 	}
 

+ 3 - 5
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")

+ 2 - 2
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 {

+ 10 - 9
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()
 

+ 4 - 2
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

+ 2 - 2
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...)
 	}