Skip to content

Commit 7dbe6c6

Browse files
committed
refactor moduleID to uint64 for faster comparisons
1 parent a22cd19 commit 7dbe6c6

File tree

9 files changed

+33
-25
lines changed

9 files changed

+33
-25
lines changed

caddy/caddy.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package caddy
55

66
import (
77
"crypto/sha256"
8-
"encoding/hex"
8+
"encoding/binary"
99
"encoding/json"
1010
"errors"
1111
"fmt"
@@ -32,8 +32,11 @@ const defaultDocumentRoot = "public"
3232

3333
var iniError = errors.New("'php_ini' must be in the format: php_ini \"<key>\" \"<value>\"")
3434

35-
// moduleWorkers is a package-level variable to store workers that can be accessed by both FrankenPHPModule and FrankenPHPApp
36-
var moduleWorkers []workerConfig
35+
// sharedState is a package-level variable to store information that can be accessed by both FrankenPHPModule and FrankenPHPApp
36+
var sharedState struct {
37+
ModuleIDs []uint64
38+
Workers []workerConfig
39+
}
3740

3841
func init() {
3942
caddy.RegisterModule(FrankenPHPApp{})
@@ -61,7 +64,7 @@ type workerConfig struct {
6164
// Directories to watch for file changes
6265
Watch []string `json:"watch,omitempty"`
6366
// ModuleID identifies which module created this worker
64-
ModuleID string `json:"module_id,omitempty"`
67+
ModuleID uint64 `json:"module_id,omitempty"`
6568
}
6669

6770
type FrankenPHPApp struct {
@@ -121,7 +124,7 @@ func (f *FrankenPHPApp) Start() error {
121124
}
122125

123126
// Add workers from FrankenPHPModule configurations
124-
for _, w := range moduleWorkers {
127+
for _, w := range sharedState.Workers {
125128
opts = append(opts, frankenphp.WithWorkers(w.Name, repl.ReplaceKnown(w.FileName, ""), w.Num, w.Env, w.Watch, w.ModuleID))
126129
}
127130

@@ -143,13 +146,13 @@ func (f *FrankenPHPApp) Stop() error {
143146
frankenphp.DrainWorkers()
144147
}
145148

146-
// reset configuration so it doesn't bleed into later tests
149+
// reset the configuration so it doesn't bleed into later tests
147150
f.Workers = nil
148151
f.NumThreads = 0
149152
f.MaxWaitTime = 0
150153

151154
// reset moduleWorkers
152-
moduleWorkers = nil
155+
sharedState.Workers = nil
153156

154157
return nil
155158
}
@@ -358,7 +361,7 @@ type FrankenPHPModule struct {
358361
// Env sets an extra environment variable to the given value. Can be specified more than once for multiple environment variables.
359362
Env map[string]string `json:"env,omitempty"`
360363
// ModuleID is the module ID that created this request.
361-
ModuleID string `json:"-"`
364+
ModuleID uint64 `json:"-"`
362365
// Workers configures the worker scripts to start.
363366
Workers []workerConfig `json:"workers,omitempty"`
364367

@@ -432,15 +435,20 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
432435
}
433436
}
434437

435-
data := []byte(f.Root + strings.Join(f.SplitPath, ",") + time.Now().String())
436-
hash := sha256.Sum256(data)
437-
f.ModuleID = hex.EncodeToString(hash[:8])
438-
439438
if len(f.Workers) > 0 {
439+
envString := ""
440+
for k, v := range f.Env {
441+
envString += k + "=" + v + ","
442+
}
443+
data := []byte(f.Root + envString)
444+
hash := sha256.Sum256(data)
445+
f.ModuleID = binary.LittleEndian.Uint64(hash[:8])
446+
440447
for _, w := range f.Workers {
441448
w.ModuleID = f.ModuleID
442449
}
443-
moduleWorkers = append(moduleWorkers, f.Workers...)
450+
sharedState.Workers = append(sharedState.Workers, f.Workers...)
451+
f.logger.Warn(fmt.Sprintf("Initialized FrankenPHP module %d with %d workers", f.ModuleID, len(f.Workers)))
444452
}
445453

446454
return 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 uint64
3737
}
3838

3939
// fromContext extracts the frankenPHPContext from a context.

frankenphp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,13 @@ 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
}
414414
}
415415

416-
// If no worker was available send the request to non-worker threads
416+
// If no worker was available, send the request to non-worker threads
417417
handleRequestWithRegularPHPThreads(fc)
418418

419419
return nil

frankenphp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func runTest(t *testing.T, test func(func(http.ResponseWriter, *http.Request), *
6565

6666
initOpts := []frankenphp.Option{frankenphp.WithLogger(opts.logger)}
6767
if opts.workerScript != "" {
68-
initOpts = append(initOpts, frankenphp.WithWorkers("workerName", testDataDir+opts.workerScript, opts.nbWorkers, opts.env, opts.watch, ""))
68+
initOpts = append(initOpts, frankenphp.WithWorkers("workerName", testDataDir+opts.workerScript, opts.nbWorkers, opts.env, opts.watch, 0))
6969
}
7070
initOpts = append(initOpts, opts.initOpts...)
7171
if opts.phpIni != nil {

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 uint64
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 uint64) Option {
6161
return func(o *opt) error {
6262
o.workers = append(o.workers, workerOpt{name, fileName, num, PrepareEnv(env), watch, moduleID})
6363

phpmainthread_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func TestTransitionThreadsWhileDoingRequests(t *testing.T) {
9494

9595
assert.NoError(t, Init(
9696
WithNumThreads(numThreads),
97-
WithWorkers(worker1Name, worker1Path, 1, map[string]string{"ENV1": "foo"}, []string{}, ""),
98-
WithWorkers(worker2Name, worker2Path, 1, map[string]string{"ENV1": "foo"}, []string{}, ""),
97+
WithWorkers(worker1Name, worker1Path, 1, map[string]string{"ENV1": "foo"}, []string{}, 0),
98+
WithWorkers(worker2Name, worker2Path, 1, map[string]string{"ENV1": "foo"}, []string{}, 0),
9999
WithLogger(zap.NewNop()),
100100
))
101101

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 uint64) 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 uint64
2424
}
2525

2626
var (

worker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func TestWorkerGetOpt(t *testing.T) {
119119

120120
func ExampleServeHTTP_workers() {
121121
if err := frankenphp.Init(
122-
frankenphp.WithWorkers("worker1", "worker1.php", 4, map[string]string{"ENV1": "foo"}, []string{}, ""),
123-
frankenphp.WithWorkers("worker1", "worker2.php", 2, map[string]string{"ENV2": "bar"}, []string{}, ""),
122+
frankenphp.WithWorkers("worker1", "worker1.php", 4, map[string]string{"ENV1": "foo"}, []string{}, 0),
123+
frankenphp.WithWorkers("worker1", "worker2.php", 2, map[string]string{"ENV2": "bar"}, []string{}, 0),
124124
); err != nil {
125125
panic(err)
126126
}

0 commit comments

Comments
 (0)