Skip to content

Conversation

@dunglas
Copy link
Member

@dunglas dunglas commented Sep 16, 2025

Usage:

import (
	"net/http"
	"net/url"

	"github.com/dunglas/frankenphp"
)

var w = &worker{
	messages: make(chan message),
}

func init() {
	frankenphp.RegisterExternalWorker(w)
}

type worker struct {
	messages chan message
}

func (w *worker) Name() string {
	return "m#MyWorker"
}

func (w *worker) FileName() string {
	return "worker.php"
}

func (w *worker) GetMinThreads() int {
	return 1
}

func (w *worker) ThreadActivatedNotification(int)   {}
func (w *worker) ThreadDrainNotification(int)       {}
func (w *worker) ThreadDeactivatedNotification(int) {}
func (w *worker) Env() frankenphp.PreparedEnv {
	return frankenphp.PreparedEnv{}
}

var u = &url.URL{Host: "my-service.alt", Path: "/my-service"}

func (w *worker) ProvideRequest() *frankenphp.WorkerRequest[map[string]string, map[string]string] {
	m := <-w.messages

	return &frankenphp.WorkerRequest{
		Request: &http.Request{URL: u}},
		CallbackParameters: m.request,
		AfterFunc: func(callbackReturn map[string]string) {
			m.responseChan <- callbackReturn
		},
	}
}
<?php

// Handler outside the loop for better performance (doing less work)
$handler = static function (array $request): array  {
	// Do something with the request

    return ['message' => "Hello, {$request['Name']}"];
};

$maxRequests = (int)($_SERVER['MAX_REQUESTS'] ?? 0);
for ($nbRequests = 0; !$maxRequests || $nbRequests < $maxRequests; ++$nbRequests) {
    $keepRunning = \frankenphp_handle_request($handler);

    // Call the garbage collector to reduce the chances of it being triggered in the middle of the handling of a request
    gc_collect_cycles();

    if (!$keepRunning) {
      break;
    }
}

@dunglas
Copy link
Member Author

dunglas commented Sep 16, 2025

I added generics support, which way cleaner.

@withinboredom
Copy link
Member

TIL that generics are allowed on structs... 😍

@withinboredom
Copy link
Member

Is this one ready to merge @dunglas?

@dunglas
Copy link
Member Author

dunglas commented Sep 17, 2025

Yes!

Copy link
Member

@alexandre-daubois alexandre-daubois left a comment

Choose a reason for hiding this comment

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

LGTM otherwise 🙂

@withinboredom withinboredom merged commit eab9194 into add/modular-threads Sep 17, 2025
@withinboredom withinboredom deleted the feat/modular-threads-improved branch September 17, 2025 09:51
dunglas added a commit that referenced this pull request Sep 18, 2025
* create a simple thread framework

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* add tests

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* fix comment

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* remove mention of an old function that no longer exists

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* simplify providing a request

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* satisfy linter

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* add error handling and handle shutdowns

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* add tests

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* pipes are tied to workers, not threads

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* fix test

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* add a way to detect when a request is completed

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* we never shutdown workers or remove them, so we do not need this

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* add more comments

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* Simplify modular threads (#1874)

* Simplify

* remove unused variable

* log thread index

* feat: allow passing parameters to the PHP callback and accessing its return value (#1881)

* fix formatting

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* fix test compilation

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* fix segfaults

Signed-off-by: Robert Landers <landers.robert@gmail.com>

* Update frankenphp.c

Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>

---------

Signed-off-by: Robert Landers <landers.robert@gmail.com>
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
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