-
-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: removes context on the C side #1404
Open
AlliBalliBaba
wants to merge
18
commits into
main
Choose a base branch
from
feat/auto-scale-clock-time-fc-better-timeouts-refactor-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+362
−391
Open
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ff325e6
Replaces context.
Alliballibaba2 ffcb6f4
Fixes merge conflicts.
Alliballibaba2 b38dc37
Fixes merge conflicts.
Alliballibaba2 42ec18d
Resets scaling update.
Alliballibaba2 755c905
Unbreaks the public api.
Alliballibaba2 dfb943d
Unbreaks the public api.
Alliballibaba2 a8eb03f
Fixes naming and config bleeding in tests.
Alliballibaba2 71259c1
Adjusts comments.
Alliballibaba2 c4c77fc
Inverts logic.
Alliballibaba2 e75e64a
cleanup.
Alliballibaba2 0f63105
Removes unnecessary checks.
Alliballibaba2 7cc551c
removes benchmark
Alliballibaba2 59fb649
clang-format.
Alliballibaba2 36e37ba
Restores startup logic.
Alliballibaba2 e8b8906
Removes busy timeout.
Alliballibaba2 dad4026
Removes unused func.
Alliballibaba2 dbe2f9b
Removes unused func.
Alliballibaba2 1e3341a
Removes docs.
Alliballibaba2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package frankenphp | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
var busyTimeout time.Duration | ||
|
||
// FrankenPHPContext provides contextual information about the Request to handle. | ||
type FrankenPHPContext struct { | ||
documentRoot string | ||
splitPath []string | ||
env PreparedEnv | ||
logger *zap.Logger | ||
request *http.Request | ||
originalRequest *http.Request | ||
|
||
docURI string | ||
pathInfo string | ||
scriptName string | ||
scriptFilename string | ||
|
||
// Whether the request is already closed by us | ||
isDone bool | ||
|
||
responseWriter http.ResponseWriter | ||
exitStatus int | ||
|
||
done chan interface{} | ||
startedAt time.Time | ||
} | ||
|
||
// FromContext extracts the FrankenPHPContext from a context. | ||
func FromContext(ctx context.Context) (fctx *FrankenPHPContext, ok bool) { | ||
fctx, ok = ctx.Value(contextKey).(*FrankenPHPContext) | ||
return | ||
} | ||
|
||
// NewRequestWithContext creates a new FrankenPHP request context. | ||
func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Request, error) { | ||
fc := &FrankenPHPContext{ | ||
done: make(chan interface{}), | ||
startedAt: time.Now(), | ||
request: r, | ||
} | ||
for _, o := range opts { | ||
if err := o(fc); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if fc.logger == nil { | ||
fc.logger = logger | ||
} | ||
|
||
if fc.documentRoot == "" { | ||
if EmbeddedAppPath != "" { | ||
fc.documentRoot = EmbeddedAppPath | ||
} else { | ||
var err error | ||
if fc.documentRoot, err = os.Getwd(); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
if fc.splitPath == nil { | ||
fc.splitPath = []string{".php"} | ||
} | ||
|
||
if fc.env == nil { | ||
fc.env = make(map[string]string) | ||
} | ||
|
||
if splitPos := splitPos(fc, r.URL.Path); splitPos > -1 { | ||
fc.docURI = r.URL.Path[:splitPos] | ||
fc.pathInfo = r.URL.Path[splitPos:] | ||
|
||
// Strip PATH_INFO from SCRIPT_NAME | ||
fc.scriptName = strings.TrimSuffix(r.URL.Path, fc.pathInfo) | ||
|
||
// Ensure the SCRIPT_NAME has a leading slash for compliance with RFC3875 | ||
// Info: https://tools.ietf.org/html/rfc3875#section-4.1.13 | ||
if fc.scriptName != "" && !strings.HasPrefix(fc.scriptName, "/") { | ||
fc.scriptName = "/" + fc.scriptName | ||
} | ||
} | ||
|
||
// SCRIPT_FILENAME is the absolute path of SCRIPT_NAME | ||
fc.scriptFilename = sanitizedPathJoin(fc.documentRoot, fc.scriptName) | ||
|
||
c := context.WithValue(r.Context(), contextKey, fc) | ||
|
||
return r.WithContext(c), nil | ||
} | ||
|
||
func newFakeContext(requestPath string, opts ...RequestOption) (*FrankenPHPContext, error) { | ||
r, err := http.NewRequest(http.MethodGet, requestPath, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fr, err := NewRequestWithContext(r, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fc, _ := FromContext(fr.Context()) | ||
|
||
return fc, nil | ||
} | ||
|
||
// closeContext sends the response to the client | ||
func (fc *FrankenPHPContext) closeContext() { | ||
if !fc.isDone { | ||
close(fc.done) | ||
fc.isDone = true | ||
} | ||
} | ||
|
||
// validate checks if the request should be outright rejected | ||
func (fc *FrankenPHPContext) validate() bool { | ||
if !strings.Contains(fc.request.URL.Path, "\x00") { | ||
return true | ||
} | ||
fc.rejectBadRequest("Invalid request path") | ||
return false | ||
} | ||
|
||
// busyTimeout returns a channel that will be closed after busy_timeout | ||
func (fc *FrankenPHPContext) busyTimeout() <-chan time.Time { | ||
if busyTimeout <= 0 { | ||
return nil | ||
} | ||
return time.After(busyTimeout) | ||
} | ||
|
||
func (fc *FrankenPHPContext) clientHasClosed() bool { | ||
select { | ||
case <-fc.request.Context().Done(): | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
// reject sends a response with the given status code and message | ||
func (fc *FrankenPHPContext) reject(statusCode int, message string) { | ||
if fc.isDone { | ||
return | ||
} | ||
|
||
rw := fc.responseWriter | ||
if rw != nil { | ||
rw.WriteHeader(statusCode) | ||
_, _ = rw.Write([]byte(message)) | ||
rw.(http.Flusher).Flush() | ||
} | ||
|
||
fc.closeContext() | ||
} | ||
|
||
func (fc *FrankenPHPContext) rejectBadRequest(message string) { | ||
fc.reject(http.StatusBadRequest, message) | ||
} | ||
|
||
func (fc *FrankenPHPContext) rejectGatewayTimeout() { | ||
logger.Debug("Request timed out") | ||
fc.reject(http.StatusGatewayTimeout, "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the 'busy timeout' and will put it into a separate PR. This PR now only refactors the context (smaller PRs are easier to review)