Skip to content

Commit 6c764ad

Browse files
committed
fix: correctly set the Mercure hub for the main worker request
1 parent e6b3f70 commit 6c764ad

7 files changed

Lines changed: 64 additions & 22 deletions

File tree

caddy/app.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,42 +114,60 @@ retry:
114114
func (f *FrankenPHPApp) addModuleWorkers(workers ...workerConfig) ([]workerConfig, error) {
115115
for i := range workers {
116116
w := &workers[i]
117+
117118
if frankenphp.EmbeddedAppPath != "" && filepath.IsLocal(w.FileName) {
118119
w.FileName = filepath.Join(frankenphp.EmbeddedAppPath, w.FileName)
119120
}
121+
120122
if w.Name == "" {
121123
w.Name = f.generateUniqueModuleWorkerName(w.FileName)
122124
} else if !strings.HasPrefix(w.Name, "m#") {
123125
w.Name = "m#" + w.Name
124126
}
127+
125128
f.Workers = append(f.Workers, *w)
126129
}
130+
127131
return workers, nil
128132
}
129133

130134
func (f *FrankenPHPApp) Start() error {
131135
repl := caddy.NewReplacer()
132136

133-
opts := []frankenphp.Option{
137+
optionsMU.RLock()
138+
opts := make([]frankenphp.Option, 0, len(options)+len(f.Workers)+7)
139+
opts = append(opts, options...)
140+
optionsMU.RUnlock()
141+
142+
opts = append(opts,
134143
frankenphp.WithContext(f.ctx),
135144
frankenphp.WithLogger(f.logger),
136145
frankenphp.WithNumThreads(f.NumThreads),
137146
frankenphp.WithMaxThreads(f.MaxThreads),
138147
frankenphp.WithMetrics(f.metrics),
139148
frankenphp.WithPhpIni(f.PhpIni),
140149
frankenphp.WithMaxWaitTime(f.MaxWaitTime),
141-
}
142-
143-
optionsMU.RLock()
144-
opts = append(opts, options...)
145-
optionsMU.RUnlock()
146-
147-
for _, w := range append(f.Workers) {
148-
workerOpts := []frankenphp.WorkerOption{
149-
frankenphp.WithWorkerEnv(w.Env),
150-
frankenphp.WithWorkerWatchMode(w.Watch),
151-
frankenphp.WithWorkerMaxFailures(w.MaxConsecutiveFailures),
152-
frankenphp.WithWorkerMaxThreads(w.MaxThreads),
150+
)
151+
152+
for _, w := range f.Workers {
153+
workerOpts := make([]frankenphp.WorkerOption, 0, len(w.requestOptions)+4)
154+
155+
if w.requestOptions == nil {
156+
workerOpts = append(workerOpts,
157+
frankenphp.WithWorkerEnv(w.Env),
158+
frankenphp.WithWorkerWatchMode(w.Watch),
159+
frankenphp.WithWorkerMaxFailures(w.MaxConsecutiveFailures),
160+
frankenphp.WithWorkerMaxThreads(w.MaxThreads),
161+
)
162+
} else {
163+
workerOpts = append(
164+
workerOpts,
165+
frankenphp.WithWorkerEnv(w.Env),
166+
frankenphp.WithWorkerWatchMode(w.Watch),
167+
frankenphp.WithWorkerMaxFailures(w.MaxConsecutiveFailures),
168+
frankenphp.WithWorkerMaxThreads(w.MaxThreads),
169+
frankenphp.WithWorkerRequestOptions(w.requestOptions...),
170+
)
153171
}
154172

155173
opts = append(opts, frankenphp.WithWorkers(w.Name, repl.ReplaceKnown(w.FileName, ""), w.Num, workerOpts...))

caddy/module.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
7474
return fmt.Errorf(`expected ctx.App("frankenphp") to return *FrankenPHPApp, got nil`)
7575
}
7676

77+
f.assignMercureHubRequestOption(ctx)
78+
7779
for i, wc := range f.Workers {
7880
// make the file path absolute from the public directory
7981
// this can only be done if the root is defined inside php_server
@@ -85,6 +87,12 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
8587
if f.Env != nil {
8688
wc.inheritEnv(f.Env)
8789
}
90+
91+
wc.requestOptions = []frankenphp.RequestOption{frankenphp.WithRequestLogger(f.logger)}
92+
if f.mercureHubRequestOption != nil {
93+
wc.requestOptions = append(wc.requestOptions, *f.mercureHubRequestOption)
94+
}
95+
8896
f.Workers[i] = wc
8997
}
9098

@@ -146,8 +154,6 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
146154
}
147155
}
148156

149-
f.assignMercureHubRequestOption(ctx)
150-
151157
return nil
152158
}
153159

@@ -592,7 +598,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
592598
// workers can also match a path without being in the public directory
593599
// in this case we need to prepend the worker routes to the existing routes
594600
func prependWorkerRoutes(routes caddyhttp.RouteList, h httpcaddyfile.Helper, f FrankenPHPModule, fsrv caddy.Module, disableFsrv bool) caddyhttp.RouteList {
595-
allWorkerMatches := caddyhttp.MatchPath{}
601+
var allWorkerMatches caddyhttp.MatchPath
596602
for _, w := range f.Workers {
597603
for _, path := range w.MatchPath {
598604
allWorkerMatches = append(allWorkerMatches, path)
@@ -607,7 +613,7 @@ func prependWorkerRoutes(routes caddyhttp.RouteList, h httpcaddyfile.Helper, f F
607613
if !disableFsrv {
608614
routes = append(routes, caddyhttp.Route{
609615
MatcherSetsRaw: []caddy.ModuleMap{
610-
caddy.ModuleMap{
616+
{
611617
"file": h.JSON(fileserver.MatchFile{
612618
TryFiles: []string{"{http.request.uri.path}"},
613619
Root: f.Root,

caddy/workerconfig.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type workerConfig struct {
3838
MatchPath []string `json:"match_path,omitempty"`
3939
// MaxConsecutiveFailures sets the maximum number of consecutive failures before panicking (defaults to 6, set to -1 to never panick)
4040
MaxConsecutiveFailures int `json:"max_consecutive_failures,omitempty"`
41+
42+
requestOptions []frankenphp.RequestOption
4143
}
4244

4345
func parseWorkerConfig(d *caddyfile.Dispenser) (workerConfig, error) {

context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Reques
100100

101101
// newDummyContext creates a fake context from a request path
102102
func newDummyContext(requestPath string, opts ...RequestOption) (*frankenPHPContext, error) {
103-
r, err := http.NewRequest(http.MethodGet, requestPath, nil)
103+
r, err := http.NewRequestWithContext(globalCtx, http.MethodGet, requestPath, nil)
104104
if err != nil {
105105
return nil, err
106106
}

options.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type workerOpt struct {
3636
num int
3737
maxThreads int
3838
env PreparedEnv
39+
requestOptions []RequestOption
3940
watch []string
4041
maxConsecutiveFailures int
4142
extensionWorkers *extensionWorkers
@@ -160,6 +161,15 @@ func WithWorkerEnv(env map[string]string) WorkerOption {
160161
}
161162
}
162163

164+
// WithWorkerRequestOptions sets options for the main dummy request created for the worker
165+
func WithWorkerRequestOptions(options ...RequestOption) WorkerOption {
166+
return func(w *workerOpt) error {
167+
w.requestOptions = append(w.requestOptions, options...)
168+
169+
return nil
170+
}
171+
}
172+
163173
// WithWorkerMaxThreads sets the max number of threads for this specific worker
164174
func WithWorkerMaxThreads(num int) WorkerOption {
165175
return func(w *workerOpt) error {

threadworker.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ func setupWorkerScript(handler *workerThread, worker *worker) {
110110
// Create a dummy request to set up the worker
111111
fc, err := newDummyContext(
112112
filepath.Base(worker.fileName),
113-
WithRequestDocumentRoot(filepath.Dir(worker.fileName), false),
114-
WithRequestPreparedEnv(worker.env),
113+
worker.requestOptions...,
115114
)
116115
if err != nil {
117116
panic(err)

worker.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "C"
55
import (
66
"fmt"
77
"os"
8+
"path/filepath"
89
"strings"
910
"sync"
1011
"time"
@@ -19,7 +20,7 @@ type worker struct {
1920
fileName string
2021
num int
2122
maxThreads int
22-
env PreparedEnv
23+
requestOptions []RequestOption
2324
requestChan chan contextHolder
2425
threads []*phpThread
2526
threadMutex sync.RWMutex
@@ -127,9 +128,9 @@ func newWorker(o workerOpt) (*worker, error) {
127128
w := &worker{
128129
name: o.name,
129130
fileName: absFileName,
131+
requestOptions: o.requestOptions,
130132
num: o.num,
131133
maxThreads: o.maxThreads,
132-
env: o.env,
133134
requestChan: make(chan contextHolder),
134135
threads: make([]*phpThread, 0, o.num),
135136
allowPathMatching: allowPathMatching,
@@ -138,6 +139,12 @@ func newWorker(o workerOpt) (*worker, error) {
138139
onThreadShutdown: o.onThreadShutdown,
139140
}
140141

142+
w.requestOptions = append(
143+
w.requestOptions,
144+
WithRequestDocumentRoot(filepath.Dir(o.fileName), false),
145+
WithRequestPreparedEnv(o.env),
146+
)
147+
141148
if o.extensionWorkers != nil {
142149
o.extensionWorkers.internalWorker = w
143150
}

0 commit comments

Comments
 (0)