Skip to content

Commit 61bbef4

Browse files
committed
refactor moduleID to uintptr for faster comparisons
1 parent a165c4e commit 61bbef4

6 files changed

Lines changed: 12 additions & 13 deletions

File tree

caddy/caddy.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strconv"
1313
"strings"
1414
"time"
15+
"unsafe"
1516

1617
"github.com/dunglas/frankenphp/internal/fastabs"
1718

@@ -59,7 +60,7 @@ type workerConfig struct {
5960
// Directories to watch for file changes
6061
Watch []string `json:"watch,omitempty"`
6162
// ModuleID identifies which module created this worker
62-
ModuleID string `json:"module_id,omitempty"`
63+
ModuleID uintptr `json:"module_id,omitempty"`
6364
}
6465

6566
type FrankenPHPApp struct {
@@ -119,7 +120,7 @@ func (f *FrankenPHPApp) Start() error {
119120
opts = append(opts, frankenphp.WithWorkers(w.Name, repl.ReplaceKnown(w.FileName, ""), w.Num, w.Env, w.Watch, w.ModuleID))
120121
}
121122

122-
// Add workers from shared location (added by FrankenPHPModule)
123+
// Add workers from FrankenPHPModule configurations
123124
for _, w := range moduleWorkers {
124125
opts = append(opts, frankenphp.WithWorkers(w.Name, repl.ReplaceKnown(w.FileName, ""), w.Num, w.Env, w.Watch, w.ModuleID))
125126
}
@@ -147,7 +148,7 @@ func (f *FrankenPHPApp) Stop() error {
147148
f.NumThreads = 0
148149
f.MaxWaitTime = 0
149150

150-
// reset shared workers
151+
// reset moduleWorkers
151152
moduleWorkers = nil
152153

153154
return nil
@@ -376,7 +377,6 @@ func (FrankenPHPModule) CaddyModule() caddy.ModuleInfo {
376377
// Provision sets up the module.
377378
func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
378379
f.logger = ctx.Logger()
379-
f.logger.Info("FrankenPHPModule provisioning 🐘")
380380

381381
if f.Root == "" {
382382
if frankenphp.EmbeddedAppPath == "" {
@@ -432,9 +432,8 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
432432

433433
if len(f.Workers) > 0 {
434434
// Tag workers with a unique module ID based on the module's memory address
435-
moduleID := fmt.Sprintf("%p", f)
436435
for i := range f.Workers {
437-
f.Workers[i].ModuleID = moduleID
436+
f.Workers[i].ModuleID = uintptr(unsafe.Pointer(f))
438437
}
439438
moduleWorkers = append(moduleWorkers, f.Workers...)
440439
}
@@ -474,7 +473,7 @@ func (f *FrankenPHPModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ c
474473
frankenphp.WithRequestSplitPath(f.SplitPath),
475474
frankenphp.WithRequestPreparedEnv(env),
476475
frankenphp.WithOriginalRequest(&origReq),
477-
frankenphp.WithModuleID(fmt.Sprintf("%p", f)),
476+
frankenphp.WithModuleID(uintptr(unsafe.Pointer(f))),
478477
)
479478

480479
if err != nil {

context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type frankenPHPContext struct {
3333
startedAt time.Time
3434

3535
// The module ID that created this request
36-
moduleID string
36+
moduleID uintptr
3737
}
3838

3939
// fromContext extracts the frankenPHPContext from a context.

frankenphp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error
407407
// Detect if a worker is available to handle this request
408408
if worker, ok := workers[fc.scriptFilename]; ok {
409409
// can handle with a global worker, or a module worker from the matching module
410-
if worker.moduleID == "" || worker.moduleID == fc.moduleID {
410+
if worker.moduleID == 0 || worker.moduleID == fc.moduleID {
411411
worker.handleRequest(fc)
412412
return nil
413413
}

options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type workerOpt struct {
2828
num int
2929
env PreparedEnv
3030
watch []string
31-
moduleID string
31+
moduleID uintptr
3232
}
3333

3434
// WithNumThreads configures the number of PHP threads to start.
@@ -57,7 +57,7 @@ func WithMetrics(m Metrics) Option {
5757
}
5858

5959
// WithWorkers configures the PHP workers to start, moduleID is used to identify the worker for a specific domain
60-
func WithWorkers(name string, fileName string, num int, env map[string]string, watch []string, moduleID string) Option {
60+
func WithWorkers(name string, fileName string, num int, env map[string]string, watch []string, moduleID uintptr) Option {
6161
return func(o *opt) error {
6262
o.workers = append(o.workers, workerOpt{name, fileName, num, PrepareEnv(env), watch, moduleID})
6363

request_options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func WithRequestLogger(logger *zap.Logger) RequestOption {
125125
}
126126

127127
// WithModuleID sets the module ID associated with the current request
128-
func WithModuleID(moduleID string) RequestOption {
128+
func WithModuleID(moduleID uintptr) RequestOption {
129129
return func(o *frankenPHPContext) error {
130130
o.moduleID = moduleID
131131

worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type worker struct {
2020
requestChan chan *frankenPHPContext
2121
threads []*phpThread
2222
threadMutex sync.RWMutex
23-
moduleID string
23+
moduleID uintptr
2424
}
2525

2626
var (

0 commit comments

Comments
 (0)