Skip to content

Conversation

@dunglas
Copy link
Member

@dunglas dunglas commented Nov 11, 2025

Coupled with caddyserver/caddy#7346, this will give full OpenTelemetry support, and a cleaner API.

@dunglas dunglas marked this pull request as draft November 11, 2025 17:25
Comment on lines 41 to 44
type contextHolder struct {
ctx context.Context
frankenPHPContext *frankenPHPContext
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just put ctx inside of frankenPHPContext ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would create a circular reference. I would prefer not to.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, just calling Value() as I was doing before is likely good enough.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a cyclycal dependency between frankenphpContext and http.request. Getting rid of it was the main idea behind #1857.

Would feel cleaner to just pass around a single object internally.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to remove all circular references, indeed.

@dunglas
Copy link
Member Author

dunglas commented Nov 13, 2025

I created a small benchmark to compare using context.Context.Value() and a direct function call: https://gist.github.com/dunglas/653aedeaafdb2afc340d2a0d33201164
Even if it's a micro-optimization, direct function call is 36% faster than retrieving a context value:

goos: darwin
goarch: arm64
pkg: github.com/dunglas/benchmark-context-value
cpu: Apple M1 Pro
BenchmarkContextValue-10        382303590                3.182 ns/op           0 B/op          0 allocs/op
BenchmarkFunctionCall-10        576038709                2.036 ns/op           0 B/op          0 allocs/op
PASS
ok      github.com/dunglas/benchmark-context-value      2.566s

As the API is entirely internal, and we can call it a lot in C<->Go loops, let's keep this as-is.

@dunglas dunglas marked this pull request as ready for review November 14, 2025 08:54
@dunglas dunglas requested a review from Copilot November 14, 2025 08:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the codebase to rely on context.Context for logging and other context-aware operations, providing OpenTelemetry support and a cleaner API. The changes introduce a contextHolder struct to pass both the Go context and FrankenPHP context together through the system.

Key changes:

  • Introduced contextHolder struct to couple context.Context with frankenPHPContext
  • Replaced global logger with globalLogger and globalCtx for better context management
  • Added context parameter to logging functions and the watcher initialization
  • Wrapped logging calls with Enabled() checks for performance optimization
  • Updated worker and thread interfaces to return both context.Context and frankenPHPContext

Reviewed Changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
context.go Introduced contextHolder struct to couple context with frankenPHPContext
frankenphp.go Replaced global logger with globalLogger and globalCtx, added context-aware logging
frankenphp.c Updated frankenphp_log_message to pass thread index for context retrieval
options.go Added WithContext option to set the main context
phpthread.go Updated thread interface to return context and frankenPHPContext separately
threadregular.go Embedded contextHolder in regularThread, updated to use context
threadworker.go Split context storage into workerContext and workerFrankenPHPContext
threadinactive.go Added context methods returning nil for inactive threads
threadtasks_test.go Added context methods for task threads
worker.go Changed channels to use contextHolder instead of *frankenPHPContext
workerextension.go Updated SendMessage to accept and use context
scaling.go Updated logging to use global context and logger
phpmainthread.go Updated logging to use global context and logger
internal/watcher/watcher.go Added context parameter to InitWatcher and logging functions
internal/watcher/watch_pattern.go Updated logging to use passed context
internal/testserver/main.go Added context creation and usage
frankenphp_test.go Added TestMain to suppress logs in non-verbose mode
phpmainthread_test.go Added setupGlobals helper to properly initialize test contexts
types_test.go Updated to use globalLogger instead of logger
workerextension_test.go Updated test to pass context to SendMessage
cgi.go Updated to use frankenPHPContext() method
caddy/app.go Updated Stop method to use context-aware logging

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dunglas dunglas merged commit 8341cc9 into main Nov 17, 2025
71 of 72 checks passed
@dunglas dunglas deleted the refactor/context branch November 17, 2025 15:32
@dunglas
Copy link
Member Author

dunglas commented Nov 17, 2025

@henderkes, do you have any idea how we can save some space and prevent the "no space left on device" error when building the mostly static binary?

@henderkes
Copy link
Contributor

That's funny, I started getting the same in my CI in the past five days and have tried to fix it. It only happens on old runners (el7, el8). The containers either report "no space left on device" or "connection lost".

I really don't know, unfortunately. GitHub must have changed something.

@henderkes
Copy link
Contributor

The error doesn't even make any sense, it manages to build many more extensions in my CI, including grpc (which alone is literally bigger than all other sources and binaries combined). I can try to shave off a few megabytes off by using shallow clones, but I don't know if that'll do anything.

@henderkes henderkes mentioned this pull request Nov 17, 2025
@dunglas
Copy link
Member Author

dunglas commented Nov 17, 2025

Otherwise, we'll have to disable grpc I guess...

@henderkes
Copy link
Contributor

I wouldn't mind it, gRPC takes up over 1/3 of my CI time and more than half the space of the containers. But it's an absolute pain to compile as a user (the pecl and pie releases are broken, because they don't contain submodules, they'll be missing the tools and it takes north of half an hour on a normal PC).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants