Skip to content

Commit dd5dc9b

Browse files
committed
implement suggestions as far as possible
1 parent af18d04 commit dd5dc9b

4 files changed

Lines changed: 18 additions & 24 deletions

File tree

context.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func fromContext(ctx context.Context) (fctx *frankenPHPContext, ok bool) {
4040
return
4141
}
4242

43+
// NewFrankenPHPContext parses out the correct filename and context to pass to NewRequestWithExistingContext
4344
func NewFrankenPHPContext(r *http.Request, opts ...RequestOption) (string, *frankenPHPContext, error) {
4445
fc := &frankenPHPContext{
4546
done: make(chan interface{}),
@@ -94,22 +95,21 @@ func NewFrankenPHPContext(r *http.Request, opts ...RequestOption) (string, *fran
9495
return fc.scriptFilename, fc, nil
9596
}
9697

98+
// NewRequestWithExistingContext wraps an http request with an existing FrankenPHP request context.
99+
func NewRequestWithExistingContext(r *http.Request, fc *frankenPHPContext) *http.Request {
100+
c := context.WithValue(r.Context(), contextKey, fc)
101+
102+
return r.WithContext(c)
103+
}
104+
97105
// NewRequestWithContext creates a new FrankenPHP request context.
98106
func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Request, error) {
99107
_, fc, err2 := NewFrankenPHPContext(r, opts...)
100108
if err2 != nil {
101109
return nil, err2
102110
}
103-
c := context.WithValue(r.Context(), contextKey, fc)
104-
105-
return r.WithContext(c), nil
106-
}
107-
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)
111111

112-
return r.WithContext(c), nil
112+
return NewRequestWithExistingContext(r, fc), nil
113113
}
114114

115115
func newDummyContext(requestPath string, opts ...RequestOption) (*frankenPHPContext, error) {

frankenphp.go

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

407-
workerName := fc.workerName
408-
if workerName == "" {
409-
workerName = fc.scriptFilename
410-
}
411407
// Detect if a worker is available to handle this request
412-
if worker := getWorkerForName(workerName); worker != nil {
408+
if worker, ok := workers[getWorkerKey(fc.workerName, fc.scriptFilename)]; ok {
413409
worker.handleRequest(fc)
414410
return nil
415411
}

scaling.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func startUpscalingThreads(maxScaledThreads int, scale chan *frankenPHPContext,
160160
}
161161

162162
// if the request has been stalled long enough, scale
163-
if worker := getWorkerForName(fc.scriptFilename); worker != nil {
163+
if worker, ok := workers[getWorkerKey(fc.workerName, fc.scriptFilename)]; ok {
164164
scaleWorkerThread(worker)
165165
} else {
166166
scaleRegularThread()

worker.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ func initWorkers(opt []workerOpt) error {
6767
return nil
6868
}
6969

70-
func getWorkerForName(name string) *worker {
71-
if wrk, ok := workers[name]; ok {
72-
return wrk
70+
func getWorkerKey(name string, filename string) string {
71+
key := filename
72+
if strings.HasPrefix(name, "m#") {
73+
key = name
7374
}
74-
return nil
75+
return key
7576
}
7677

7778
func newWorker(o workerOpt) (*worker, error) {
@@ -80,11 +81,8 @@ func newWorker(o workerOpt) (*worker, error) {
8081
return nil, fmt.Errorf("worker filename is invalid %q: %w", o.fileName, err)
8182
}
8283

83-
key := absFileName
84-
if strings.HasPrefix(o.name, "m#") {
85-
key = o.name
86-
}
87-
if wrk := getWorkerForName(key); wrk != nil {
84+
key := getWorkerKey(o.name, absFileName)
85+
if wrk, ok := workers[key]; ok {
8886
return wrk, nil
8987
}
9088

0 commit comments

Comments
 (0)