-
-
Notifications
You must be signed in to change notification settings - Fork 366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Customized PHP Runners #1499
Comments
Hi @deminy, thanks a lot for all these details. I have a few ideas that we can discuss regarding the bootstrap/runner/runtime, but before that I think it's better to discuss about the 4 examples first. Example 4 shows the clear benefit of concurrent IO operations -> all good. For example 3, async operations are not "awaited" before they are finished, and the response is returned to Lambda beforehand. When that happens, Lambda freezes the environment. The coroutines will (unless I am mistaken) never run in the background, unless the same Lambda instance is called by a consecutive request. This is not guaranteed (Lambda instances can be terminated at any time), so if the IO operations are important (e.g. writing to a database, etc.), they might not run successfully at all. I think the Swoole runner should make sure there are no pending coroutines at the end of an invocation. This is what the NodeJS runtime does: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html#nodejs-handler-callback They do have the option though, but it's not enabled by default as I guess this can be very confusing for users: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html |
Hi @mnapoli , thanks for your feedback. I didn't provide enough clarity in example 3, and I can see how some of the concerns you've mentioned are indeed valid. To address these issues, I'll update the README.md file for better clarity and understanding. 1
Yes, example 3 functions similarly to setting the
2
Not exactly. The coroutine will continue running in the background until it either completes, encounters a Lambda timeout, or the Lambda execution environment is terminated due to a lack of invocations. That's why in the Lambda response I had a field count included to count total number of active coroutines within the execution environment. By invoking the Lambda function multiple times in quick succession, you'll notice the counter increases by 5 with each invocation. This indicates that coroutines from previous invocations are still actively running within the same environment. 3
Yes. we need to be careful when using coroutines to run background tasks within a Lambda environment, mainly because of the following two reasons:
Thus, we should use coroutines to run short background tasks only, and we should do it only in the following scenarios:
4
In general, the answer is yes, although not in every case. My reply in the previous section provides a more detailed explanation. Please feel free to let me know if you have any questions. Thanks |
1ChatGPT is correct, but this is not what I am talking about. A "warm" container has its code completely frozen. Let me clarify: As soon as PHP returns a response, the entire Linux environment and all processes running in the Lambda instance are frozen. Code does not run anymore. In your test, pending coroutines only run again because the next invocation thaws the environment/processes, and code runs again.
It does immediately freeze the environment. What ChatGPT is talking about is that the Lambda instance is kept (frozen) in memory so that Lambda can handle further requests faster. To verify this, try the following:
The log will not be written, because the coroutine never runs.
"A Lambda environment can persist for a maximum of 15 minutes." -> this is not related. A single invocation can run for 15 minutes, but in the case of an HTTP API it can only run for 29 seconds. I think this is a misinterpretation of what these 15 minutes are, it's for an "INVOKE" phase. The Lambda environment can be alive for hours if it is invoked by multiple requests. Also:
The important conclusion here is: Lambda environments can be destroyed at any time for many reasons.
Yes. Do you have examples of such tasks? These need to be tasks that have no guarantee of running/finishing, I'm not sure there are many practical examples?
Because of what I mentioned above, there is no guarantee tasks would run at all. It is only viable for unimportant tasks that have no guarantees of running. I don't see any real use case for this to be honest, but I'm interested if you have some in mind? |
Example 3 was to illustrate the potential capabilities within the same execution environment after sending a Lambda response back, regardless of whether the next invocation occurs or not. However, it may not be the best representation of what happens when I acknowledge that |
Understood! To clarify, if I understand correctly, example 5 the same as example 3 with a shorter And to clarify, can you confirm that this is what is happening:
What I want to confirm is that:
Is my understanding correct? |
Yes, For example 5, we can submit one invocation only, and the messages from invocation 1 are written to CloudWatch. The response is sent back immediately (less than 1 second) via the Lambda API So for the following statements/questions:
To help better understand it, I've updated example 5 a little bit to print out more debugging messages, and added a new example (example 6) for comparison. Please note that example 5 and example 6 are exactly the same, except that they always run in different execution environments. If we invoke the Lambda function of example 5 once every couple seconds, we should see that:
There is one more thing I'd like to mention. By default, Bref quits the PHP execution environment after each invocation, unless we explicitly set
Please feel free to let me know if there are any questions. Thanks |
Thanks a lot for digging in! That is very interesting 🤔 Just a heads up: I will be on holidays next week, so I will be able to look back into it later! |
Hello! I have created a draft patch along with a few examples to enhance the support for asynchronous extensions/frameworks such as Swoole and OpenSwoole. You can find the patch at https://github.com/deminy/customized-runner-for-bref .
As the patch involves modifications to the bootstrap script or certain internal classes, I have committed it to a personal repository rather than submitting a pull request directly. If you have any questions or suggestions for improvement, please don't hesitate to reach out. I am more than willing to assist in any way I can.
Thanks
The text was updated successfully, but these errors were encountered: