Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122076 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 21086 invoked from network); 31 Dec 2023 08:31:30 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 Dec 2023 08:31:30 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1704011518; bh=0uvhyYUfcnOuDjVA+C8NJVOqKcsfw1AkuQYk7/w+J3E=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=LmpFcw8+c7NLg8zVELCTF3BD9BeD9He4uwvDqItNK+7KQsrXZhDiu0DwgUWXL+sBw DSL1NyhO8AWYUUuFvZAgymvddwudockzK7+D2wZ9h9gGRwPIIDJFdU+wn5eyY3L0LJ tosaLya9WJqxU/myCrKfPhkNvWBJzezPYelw+OUUJA30K05qA5qK5dryhitYShAEoT O6aJNICcPBiLghc3Ul8Lt3CRxHXGxMpGduQT2VDj0/GsfXr0kBckGlA8gYOug3nxRJ 8UiUtdqrXjTp2If57S4cv5eW5GScd+Z/ECQZD1jE9FU9xHCwp469S00ncBHHrvYMqc 4tzkM5ulWiRxA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7201D18004D for ; Sun, 31 Dec 2023 00:31:57 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DMARC_MISSING,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 31 Dec 2023 00:31:56 -0800 (PST) Received: by mail-qv1-f54.google.com with SMTP id 6a1803df08f44-67fa018c116so47324096d6.3 for ; Sun, 31 Dec 2023 00:31:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dunglas-fr.20230601.gappssmtp.com; s=20230601; t=1704011487; x=1704616287; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=lX6InaEBfd0qCvdDf/vDi/oWUvHejVpjBR5MH9YLpuw=; b=BCbTrWoO4dYOtxxfzf/8rbYNUqEKZShTXiY/KDrt8rNkv0D/AR+363bRMrWzO5d69i TrshHPGN22ylfPWimLtibv6J1X9bLE7VRs0QJ6eU8cTgCbZDhFGYCJcuLbmeGbwg22ZL CHftA+UYrg+4ztrjTnfC0RfGDUlDr3D19+EMEGUkWynwayi/8dp0OxnLrZHJsPxvlEqJ i0MMxt72JuzX6my7CVGhiE9hCsA3KJFviunHCcxoKvwPTMFSq6ar80jicdYKTYcH6LYQ DXaVSbTGJxI8WK6QxO5Q+nLV7FWpmXLi1WsmdV4W+ghV7+SxaG3brTHn3kPy+o86jscm ALgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704011487; x=1704616287; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lX6InaEBfd0qCvdDf/vDi/oWUvHejVpjBR5MH9YLpuw=; b=nowkGhcS7MJs6r8Doy3Fz9FLHIot0F0/yAVpRtbT3JhUz9cZSdaE8RLF2jGxh0o7NQ GXVMnRPOcNybSCQegfFM8K+GMFjZhzAFO46oJMStaoCaz/QAEiLlzCEjB34+EJs7wQPk LF2MWCTml+1aZjva/Pct7yCC3biqAgj4SkD+EeevjqYISZwESSNyPEWXFeZXYzzhShBY jPLLI+lQk1HYdZv065pIXy8OFGthtzg+50zSdZnmdM8wSTYW6v0jSaG2vT3wN6hFzhva I9kk/cd6DdILActCgrM9iYbHW6ZQGV2MluKoZRX3LL/5JuuL03oZezDdlVoMTtYfddD5 YoUw== X-Gm-Message-State: AOJu0Yxs5mX1I9p74DIBWz44DlTGdlwq6lTrGvhVW/N2ljvin34OUt/o cBFv9fYe3LBjSdjV83y7vXi6ehFH7VAgYxD6j0i/hqR4ncwyyST+NlJzwbSF X-Google-Smtp-Source: AGHT+IEu11+gxGfGmd4jA+VwQYz3ewuXPvzhI9Rv+rnCEOQ5TXXSr/z6Jqe+7ItmdhutCxebl0mQrS6796L+WchLv1k= X-Received: by 2002:a0c:d6c8:0:b0:680:857:1bb4 with SMTP id l8-20020a0cd6c8000000b0068008571bb4mr9773285qvi.120.1704011487651; Sun, 31 Dec 2023 00:31:27 -0800 (PST) MIME-Version: 1.0 References: <5060b986-2e5a-46e4-9c83-763e5b155e83@gmail.com> <6f7815b9-80cc-4e08-819a-49dca090116f@gmail.com> <7F63D301-1A46-49AA-9140-F64543E902C5@gmail.com> <8fb6672c-06e9-4f74-b2f2-cd1a265c75a5@app.fastmail.com> <96C28696-7C58-4018-84EB-69CF4189649B@gmail.com> In-Reply-To: <96C28696-7C58-4018-84EB-69CF4189649B@gmail.com> Date: Sun, 31 Dec 2023 09:31:16 +0100 Message-ID: To: Rowan Tommins Cc: php internals Content-Type: multipart/alternative; boundary="0000000000006a3bca060dca17de" Subject: Re: [PHP-DEV] RFC proposal: worker mode primitives for SAPIs From: kevin@dunglas.fr (=?UTF-8?Q?K=C3=A9vin_Dunglas?=) --0000000000006a3bca060dca17de Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Dec 31, 2023 at 2:20=E2=80=AFAM Rowan Tommins wrote: > On 30 December 2023 19:48:39 GMT, Larry Garfield > wrote: > >The Franken-model is closer to how PHP-FPM works today, which means that > is easier to port existing code to, especially existing code that has lot= s > of globals or hidden globals. (Eg, Laravel.) That may or may not make i= t > the better model overall, I don't know, but it's the more-similar model. > > That's why I said earlier that it provides better backwards compatibility > - existing code which directly uses PHP's current global state can more > easily be run in a worker which populates that global state. > > However, the benefit is marginal, for two reasons. Firstly, because in > practice a lot of applications avoid touching the global state outside of > some request bootstrapping code anyway. The FrankenPHP example code and > Laravel Octane both demonstrate this. > > Secondly, because in an environment that handles a single request at a > time, the reverse is also possible: if the server passes request > information directly to a callback, that callback can populate the > superglobals as appropriate. The only caveat I can think of is input > streams, since userland code can't reset and populate php://input, or > repoint STDOUT. > > On the other hand, as soon as you have any form of concurrency, the two > models are not interchangeable - it would make no sense for an asynchrono= us > callback to read from or write to global state. > > And that's what I meant about FrankenPHP's API having poor forward > compatibility - if you standardise on an API that populates global state, > you close off any possibility of using that API in a concurrent > environment. If you instead standardise on callbacks which hold request a= nd > response information in their own scope, you don't close anything off. > > If anything, calling this "forwards compatibility" is overly generous: th= e > OP gave Swoole as an example of an existing worker environment, but I can= 't > see any way that Swoole could implement an API that communicated request > and response information via global state. > > Regards, > > -- > Rowan Tommins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php This new function is intended for SAPIs. Swoole was given as an example of worker mode, but it isn't a SAPI. AFAIK, it doesn't use the SAPI infrastructure provided by PHP. The scope of my proposal is only to provide a new feature in the SAPI infrastructure to build worker modes to handle HTTP requests, not to deal with non-SAPI engines. That being said, I don't understand what would prevent Swoole from implementing the proposed API, or even to implement a userland implementation of the proposed API using Swoole under the hood. It seems doable to emulate the sequential request handling and to create an adapter from their custom objects to superglobals and streams. For WebSockets and WebTransports, the same considerations apply. The SAPI API will have to be extended to deal with such low-level network layers, worker mode or not. To me, this is very interesting (and needed) but should be discussed in another RFC. As pointed out by Crell, FrankenPHP (and similar theoretical solutions) starts as many workers as needed. This can be a fixed set of workers, as in FrankenPHP, or a dynamic number of workers, similar to traditional FPM workers. FrankenPHP uses threads to parallelize request handling (to start several instances of the worker script in parallel). Other techniques could be used, for instance, in the future, we could use goroutines (which use a mix of system threads and async IO, and goroutines are handled in a single system thread: https://github.com/golang/go/blob/master/src/runtime/HACKING.md#gs-ms-ps) instead of threads, by adding a new backend to TSRM. The global state is never reset in the same worker context, it is preserved across requests, except for superglobals and streams, which are updated with the data of the request being handled. Superglobals are the PHP way to expose CGI-like data. Adding support for other ways to do it such as proposed by WSGI, and/or new objects and the like could be interesting, but again this isn't the scope of this proposal which is narrow, and tries to reuse the existing infrastructure as much as possible. The proposal is simple enough to support new ways if introduced at some point in PHP, and the Symfony Runtime and Laravel Octane libraries prove that it's possible to implement more advanced data structures user-land on top of the existing superglobals infrastructure. Regarding the infinite loop, we could indeed remove it using a few lines of code. I hesitated to do that initially, but the loop gives more flexibility by allowing the implementation of many features in user-land (like restarting the worker after a fixed number of requests, when the memory reaches a certain level, etc). Without this loop, all these features will require new C functions. I thought it was a simple and good way to give more power to user-land, at the price of some boilerplate code (that will in theory always be hidden in low-level libraries or bootstrap code). That being said, I've no strong opinion about that and would be open to removing the need for the while loop if we find a good way to keep the current flexibility. Best regards, --0000000000006a3bca060dca17de--