You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: workerextension.go
+46-33Lines changed: 46 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -8,46 +8,57 @@ import (
8
8
"sync/atomic"
9
9
)
10
10
11
-
// EXPERIMENTAL: Worker allows you to register a worker where instead of calling FrankenPHP handlers on
12
-
// frankenphp_handle_request(), the ProvideRequest method is called. You may provide a standard
13
-
// http.Request that will be conferred to the underlying worker script.
11
+
// EXPERIMENTAL: Worker allows you to register a worker where, instead of calling FrankenPHP handlers on
12
+
// frankenphp_handle_request(), the GetRequest method is called.
13
+
//
14
+
// You may provide an http.Request that will be conferred to the underlying worker script,
15
+
// or custom parameters that will be passed to frankenphp_handle_request().
16
+
//
17
+
// After the execution of frankenphp_handle_request(), the return value WorkerRequest.AfterFunc will be called,
18
+
// with the optional return value of the callback passed as parameter.
14
19
//
15
20
// A worker script with the provided Name and FileName will be registered, along with the provided
16
-
// configuration. You can also provide any environment variables that you want through Env. GetMinThreads allows you to
17
-
// reserve a minimum number of threads from the frankenphp thread pool. This number must be positive.
18
-
// These methods are only called once at startup, so register them in an init() function.
21
+
// configuration. You can also provide any environment variables that you want through Env.
19
22
//
20
-
// When a thread is activated and nearly ready, ThreadActivatedNotification will be called with an opaque threadId;
21
-
// this is a time for setting up any per-thread resources. When a thread is about to be returned to the thread pool,
22
-
// you will receive a call to ThreadDrainNotification that will inform you of the threadId.
23
-
// After the thread is returned to the thread pool, ThreadDeactivatedNotification will be called.
23
+
// Name() and FileName() are only called once at startup, so register them in an init() function.
24
24
//
25
-
// Once you have at least one thread activated, you will receive calls to ProvideRequest where you should respond with
26
-
// a request. FrankenPHP will automatically pipe these requests to the worker script and handle the response.
27
-
// The piping process is designed to run indefinitely and will be gracefully shut down when FrankenPHP shuts down.
25
+
// Workers are designed to run indefinitely and will be gracefully shut down when FrankenPHP shuts down.
28
26
//
29
-
// Note: External workers receive the lowest priority when determining thread allocations. If GetMinThreads cannot be
30
-
// allocated, then frankenphp will panic and provide this information to the user (who will need to allocate more
27
+
// Extension workers receive the lowest priority when determining thread allocations. If MinThreads cannot be
28
+
// allocated, then FrankenPHP will panic and provide this information to the user (who will need to allocate more
31
29
// total threads). Don't be greedy.
32
30
typeWorkerinterface {
31
+
// Name returns the worker name
33
32
Name() string
33
+
// FileName returns the PHP script filename
34
34
FileName() string
35
+
// Env returns the environment variables available in the worker script.
35
36
Env() PreparedEnv
36
-
GetMinThreads() int
37
-
ThreadActivatedNotification(threadIdint)
38
-
ThreadDrainNotification(threadIdint)
39
-
ThreadDeactivatedNotification(threadIdint)
40
-
ProvideRequest() *WorkerRequest
41
-
InjectRequest(r*WorkerRequest)
37
+
// MinThreads returns the minimum number of threads to reserve from the FrankenPHP thread pool.
38
+
// This number must be positive.
39
+
MinThreads() int
40
+
// OnReady is called when the worker is assigned to a thread and receives an opaque thread ID as parameter.
41
+
// This is a time for setting up any per-thread resources.
42
+
OnReady(threadIdint)
43
+
// OnShutdown is called when the worker is shutting down and receives an opaque thread ID as parameter.
44
+
// This is a time for cleaning up any per-thread resources.
45
+
OnShutdown(threadIdint)
46
+
// OnServerShutdown is called when FrankenPHP is shutting down.
47
+
OnServerShutdown(threadIdint)
48
+
// GetRequest is called once at least one thread is ready.
49
+
// The returned request will be passed to the worker script.
50
+
GetRequest() *WorkerRequest
51
+
// SendRequest sends a request to the worker script. The callback function of frankenphp_handle_request() will be called.
52
+
SendRequest(r*WorkerRequest)
42
53
}
43
54
44
-
// EXPERIMENTAL
55
+
// EXPERIMENTAL: WorkerRequest represents a request to pass to a worker script.
45
56
typeWorkerRequeststruct {
46
-
// The request for your worker script to handle
57
+
// Request is an optional HTTP request for your worker script to handle
47
58
Request*http.Request
48
-
// Response is a response writer that provides the output of the provided request, it must not be nil to access the request body
59
+
// Response is an optional response writer that provides the output of the provided request, it must not be nil to access the request body
49
60
Response http.ResponseWriter
50
-
// CallbackParameters is an optional field that will be converted in PHP types and passed as parameter to the PHP callback
61
+
// CallbackParameters is an optional field that will be converted in PHP types or left as-is if it's an unsafe.Pointer and passed as parameter to the PHP callback
51
62
CallbackParametersany
52
63
// AfterFunc is an optional function that will be called after the request is processed with the original value, the return of the PHP callback, converted in Go types, is passed as parameter
53
64
AfterFuncfunc(callbackReturnany)
@@ -56,7 +67,7 @@ type WorkerRequest struct {
56
67
varextensionWorkers=make(map[string]Worker)
57
68
varextensionWorkersMutex sync.Mutex
58
69
59
-
// EXPERIMENTAL
70
+
// EXPERIMENTAL: RegisterWorker registers a custom worker script.
0 commit comments