Skip to content

Commit

Permalink
remove smart pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
withinboredom committed Aug 12, 2024
1 parent cf0c808 commit b4a53d1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 84 deletions.
35 changes: 19 additions & 16 deletions frankenphp.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Reques
fc.scriptFilename = sanitizedPathJoin(fc.documentRoot, fc.scriptName)

c := context.WithValue(r.Context(), contextKey, fc)
c = context.WithValue(c, handleKey, Handles())

return r.WithContext(c), nil
}
Expand Down Expand Up @@ -307,7 +306,7 @@ func Init(options ...Option) error {

shutdownWG.Add(1)
done = make(chan struct{})
requestChan = make(chan *http.Request)
requestChan = make(chan *http.Request, opt.numThreads*2)

if C.frankenphp_init(C.int(opt.numThreads)) != 0 {
return MainThreadCreationError
Expand Down Expand Up @@ -355,10 +354,10 @@ func getLogger() *zap.Logger {
return logger
}

func updateServerContext(request *http.Request, create bool, mrh C.uintptr_t) error {
func updateServerContext(request *http.Request, create bool, mrh C.uintptr_t) (*Handle, error) {
fc, ok := FromContext(request.Context())
if !ok {
return InvalidRequestError
return nil, InvalidRequestError
}

authUser, authPassword, ok := request.BasicAuth()
Expand All @@ -378,7 +377,7 @@ func updateServerContext(request *http.Request, create bool, mrh C.uintptr_t) er
var err error
contentLength, err = strconv.Atoi(contentLengthStr)
if err != nil {
return fmt.Errorf("invalid Content-Length header: %w", err)
return nil, fmt.Errorf("invalid Content-Length header: %w", err)
}
}

Expand All @@ -400,12 +399,9 @@ func updateServerContext(request *http.Request, create bool, mrh C.uintptr_t) er

var rh Handle
if fc.responseWriter == nil {
h := NewHandle(request)
request.Context().Value(handleKey).(*handleList).AddHandle(h)
mrh = C.uintptr_t(h)
mrh = C.uintptr_t(NewHandle(request))
} else {
rh = NewHandle(request)
request.Context().Value(handleKey).(*handleList).AddHandle(rh)
}

ret := C.frankenphp_update_server_context(
Expand All @@ -425,10 +421,18 @@ func updateServerContext(request *http.Request, create bool, mrh C.uintptr_t) er
)

if ret > 0 {
return RequestContextCreationError
rh.Delete()
Handle(mrh).Delete()
return nil, RequestContextCreationError
}

return nil
if rh != 0 {
return &rh, nil
}

rh = Handle(mrh)

return &rh, nil
}

// ServeHTTP executes a PHP script according to the given context.
Expand Down Expand Up @@ -467,19 +471,16 @@ func go_handle_request() bool {
return false

case r := <-requestChan:
h := NewHandle(r)
r.Context().Value(handleKey).(*handleList).AddHandle(h)

fc, ok := FromContext(r.Context())
if !ok {
panic(InvalidRequestError)
}
defer func() {
maybeCloseContext(fc)
r.Context().Value(handleKey).(*handleList).FreeAll()
}()

if err := updateServerContext(r, true, 0); err != nil {
rh, err := updateServerContext(r, true, 0)
if err != nil {
panic(err)
}

Expand All @@ -489,6 +490,8 @@ func go_handle_request() bool {
panic(ScriptExecutionError)
}

rh.Delete()

return true
}
}
Expand Down
63 changes: 0 additions & 63 deletions smartpointer.go

This file was deleted.

11 changes: 6 additions & 5 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,14 @@ func go_frankenphp_worker_handle_request_start(mrh C.uintptr_t) C.uintptr_t {
case r = <-rc:
}

fc.currentWorkerRequest = NewHandle(r)
r.Context().Value(handleKey).(*handleList).AddHandle(fc.currentWorkerRequest)

l.Debug("request handling started", zap.String("worker", fc.scriptFilename), zap.String("url", r.RequestURI))
if err := updateServerContext(r, false, mrh); err != nil {

rh, err := updateServerContext(r, false, mrh)
fc.currentWorkerRequest = *rh
if err != nil {
// Unexpected error
l.Debug("unexpected error", zap.String("worker", fc.scriptFilename), zap.String("url", r.RequestURI), zap.Error(err))
rh.Delete()

return 0
}
Expand All @@ -175,7 +176,7 @@ func go_frankenphp_finish_request(mrh, rh C.uintptr_t, deleteHandle bool) {
fc := r.Context().Value(contextKey).(*FrankenPHPContext)

if deleteHandle {
r.Context().Value(handleKey).(*handleList).FreeAll()
rHandle.Delete()
Handle(mrh).Value().(*http.Request).Context().Value(contextKey).(*FrankenPHPContext).currentWorkerRequest = 0
}

Expand Down

0 comments on commit b4a53d1

Please sign in to comment.