Skip to content

Commit 5fc1edf

Browse files
committed
figure out the worker to use in FrankenPHPModule::ServeHTTP
1 parent 2ff18ba commit 5fc1edf

5 files changed

Lines changed: 58 additions & 36 deletions

File tree

caddy/caddy.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,6 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
427427
}
428428
}
429429

430-
if len(f.Workers) > 0 {
431-
moduleWorkers = append(moduleWorkers, f.Workers...)
432-
}
433-
434430
return nil
435431
}
436432

@@ -460,20 +456,30 @@ func (f *FrankenPHPModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ c
460456
}
461457
}
462458

463-
workerNames := make([]string, len(f.Workers))
464-
for i, w := range f.Workers {
465-
workerNames[i] = w.Name
466-
}
467-
468-
fr, err := frankenphp.NewRequestWithContext(
459+
filename, fc, err := frankenphp.NewFrankenPHPContext(
469460
r,
470461
documentRootOption,
471462
frankenphp.WithRequestSplitPath(f.SplitPath),
472463
frankenphp.WithRequestPreparedEnv(env),
473464
frankenphp.WithOriginalRequest(&origReq),
474-
frankenphp.WithWorkerNames(workerNames),
475465
)
466+
if err != nil {
467+
return caddyhttp.Error(http.StatusInternalServerError, err)
468+
}
469+
470+
workerName := ""
471+
for _, w := range f.Workers {
472+
if p, _ := fastabs.FastAbs(w.FileName); p == filename {
473+
workerName = w.Name
474+
}
475+
}
476476

477+
err = frankenphp.WithModuleWorker(workerName)(fc)
478+
if err != nil {
479+
return caddyhttp.Error(http.StatusInternalServerError, err)
480+
}
481+
482+
fr, err := frankenphp.NewRequestWithExistingContext(r, fc)
477483
if err != nil {
478484
return caddyhttp.Error(http.StatusInternalServerError, err)
479485
}
@@ -577,14 +583,28 @@ func (f *FrankenPHPModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
577583
envString += k + "=" + v + ","
578584
}
579585
envString = strings.TrimSuffix(envString, ",")
580-
wc.Name += "env:" + envString + "_" + wc.FileName
586+
wc.Name += "env:" + envString
581587
}
582588
}
583589
if wc.Name != "" && !strings.HasPrefix(wc.Name, "m#") {
584590
wc.Name = "m#" + wc.Name
585591
}
586592

593+
// Check if a worker with this filename already exists in this module
594+
for _, existingWorker := range f.Workers {
595+
if existingWorker.FileName == wc.FileName {
596+
return fmt.Errorf("workers must not have duplicate filenames: %s", wc.FileName)
597+
}
598+
}
599+
// Check if a worker with this name already exists
600+
for _, existingWorker := range moduleWorkers {
601+
if existingWorker.Name == wc.Name {
602+
return fmt.Errorf("workers must not have duplicate names: %s", wc.Name)
603+
}
604+
}
605+
587606
f.Workers = append(f.Workers, wc)
607+
moduleWorkers = append(moduleWorkers, f.Workers...)
588608
}
589609
}
590610
}

caddy/caddy_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func TestGlobalAndModuleWorker(t *testing.T) {
161161
}
162162
`, "caddyfile")
163163

164-
for i := 0; i < 10; i++ {
164+
for i := 0; i < 2; i++ {
165165
wg.Add(1)
166166

167167
go func(i int) {
@@ -183,13 +183,7 @@ func TestNamedModuleWorkers(t *testing.T) {
183183
skip_install_trust
184184
admin localhost:2999
185185
186-
frankenphp {
187-
worker {
188-
file ../testdata/worker-with-env.php
189-
num 1
190-
env APP_ENV global
191-
}
192-
}
186+
frankenphp
193187
}
194188
195189
http://localhost:`+testPort+` {

context.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type frankenPHPContext struct {
2323
pathInfo string
2424
scriptName string
2525
scriptFilename string
26-
workerNames []string
26+
workerName string
2727

2828
// Whether the request is already closed by us
2929
isDone bool
@@ -40,15 +40,15 @@ func fromContext(ctx context.Context) (fctx *frankenPHPContext, ok bool) {
4040
return
4141
}
4242

43-
func newFrankenPHPContext(r *http.Request, opts ...RequestOption) (*frankenPHPContext, error) {
43+
func NewFrankenPHPContext(r *http.Request, opts ...RequestOption) (string, *frankenPHPContext, error) {
4444
fc := &frankenPHPContext{
4545
done: make(chan interface{}),
4646
startedAt: time.Now(),
4747
request: r,
4848
}
4949
for _, o := range opts {
5050
if err := o(fc); err != nil {
51-
return nil, err
51+
return "", nil, err
5252
}
5353
}
5454

@@ -62,7 +62,7 @@ func newFrankenPHPContext(r *http.Request, opts ...RequestOption) (*frankenPHPCo
6262
} else {
6363
var err error
6464
if fc.documentRoot, err = os.Getwd(); err != nil {
65-
return nil, err
65+
return "", nil, err
6666
}
6767
}
6868
}
@@ -91,12 +91,12 @@ func newFrankenPHPContext(r *http.Request, opts ...RequestOption) (*frankenPHPCo
9191

9292
// SCRIPT_FILENAME is the absolute path of SCRIPT_NAME
9393
fc.scriptFilename = sanitizedPathJoin(fc.documentRoot, fc.scriptName)
94-
return fc, nil
94+
return fc.scriptFilename, fc, nil
9595
}
9696

9797
// NewRequestWithContext creates a new FrankenPHP request context.
9898
func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Request, error) {
99-
fc, err2 := newFrankenPHPContext(r, opts...)
99+
_, fc, err2 := NewFrankenPHPContext(r, opts...)
100100
if err2 != nil {
101101
return nil, err2
102102
}
@@ -105,6 +105,13 @@ func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Reques
105105
return r.WithContext(c), nil
106106
}
107107

108+
// NewRequestWithExistingContext wraps an http request with an existing FrankenPHP request context.
109+
func NewRequestWithExistingContext(r *http.Request, fc *frankenPHPContext) (*http.Request, error) {
110+
c := context.WithValue(r.Context(), contextKey, fc)
111+
112+
return r.WithContext(c), nil
113+
}
114+
108115
func newDummyContext(requestPath string, opts ...RequestOption) (*frankenPHPContext, error) {
109116
r, err := http.NewRequest(http.MethodGet, requestPath, nil)
110117
if err != nil {

frankenphp.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,14 @@ func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error
404404
return nil
405405
}
406406

407-
workerNames := append(fc.workerNames, fc.scriptFilename)
408-
for _, workerName := range workerNames {
409-
// Detect if a worker is available to handle this request
410-
if worker := getWorkerForName(workerName); worker != nil {
411-
worker.handleRequest(fc)
412-
return nil
413-
}
407+
workerName := fc.workerName
408+
if workerName == "" {
409+
workerName = fc.scriptFilename
410+
}
411+
// Detect if a worker is available to handle this request
412+
if worker := getWorkerForName(workerName); worker != nil {
413+
worker.handleRequest(fc)
414+
return nil
414415
}
415416

416417
// If no worker was available, send the request to non-worker threads

request_options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ func WithRequestLogger(logger *zap.Logger) RequestOption {
124124
}
125125
}
126126

127-
// WithRequestLogger sets the logger associated with the current request
128-
func WithWorkerNames(names []string) RequestOption {
127+
// WithModuleWorker sets the worker that should handle the request
128+
func WithModuleWorker(name string) RequestOption {
129129
return func(o *frankenPHPContext) error {
130-
o.workerNames = names
130+
o.workerName = name
131131

132132
return nil
133133
}

0 commit comments

Comments
 (0)