Skip to content
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

Implement HTTP response streaming #10

Merged
merged 19 commits into from
Nov 20, 2023
Merged

Implement HTTP response streaming #10

merged 19 commits into from
Nov 20, 2023

Conversation

thekid
Copy link
Member

@thekid thekid commented Jun 18, 2023

This PR implements HTTP response streaming on top of xp-forge/lambda#23 (Implement streaming lambda responses), improving TTFB and memory consumption of web applications. Response streaming is available for lambda function URLs which have their invoke mode set to RESPONSE_STREAM.

Setup

To integrate with lambda function URLs, extend from the new HttpStreaming base class.

use com\amazon\aws\lambda\HttpStreaming;

class Web extends HttpStreaming {

  public function routes($env) {
    return ['/' => function($req, $res) { /* Shortened for brevity */ }];
  }
}

👉 Verify RESPONSE_STREAM is set:

$ aws lambda list-function-url-configs --function-name test-func --output yaml
FunctionUrlConfigs:
- AuthType: NONE
  CreationTime: '2023-06-18T09:46:26.390725Z'
  FunctionArn: arn:aws:lambda:eu-central-1:123456789012:function:test-func
  FunctionUrl: https://XXXXXXXXXX.lambda-url.eu-central-1.on.aws/
  InvokeMode: RESPONSE_STREAM
  LastModifiedTime: '2023-06-18T11:58:50.560707Z'

The AWS CLI provides an option to change the invoke mode:

$ aws lambda update-function-url-config --function-name test-func --invoke-mode RESPONSE_STREAM

If not set, the following will be displayed:

Must use RESPONSE_STREAM

💡This is implemented by setting body to this error message in the meta information. The BUFFERED response handler will use this for the body, while RESPONSE_STREAM will recognize the special application/vnd.awslambda.http-integration-response mime type and will stream everything after the delimiter. This adds an overhead of 34 bytes to each payload, which seems acceptable for the sake of mitigating a big WTF factor.

API Gateway

Streamed responses are not supported by API Gateway’s LAMBDA_PROXY integration. To integrate with it, extend the HttpApi base class instead, as before.

use com\amazon\aws\lambda\HttpApi;

class Web extends HttpApi {

  public function routes($env) {
    return ['/' => function($req, $res) { /* Shortened for brevity */ }];
  }
}

💡Also use this for integrating with function URLs when their invoke method needs to be set to BUFFERED.

BC Break

This PR breaks BC due to the dropped compatibility with older xp-forge/lambda versions. Older versions will not have the streaming functionality backported. Composer will show an error for mismatches.

⚠️ This PR will therefore be released in the next major version, 2.0.0 at the time of writing.

See also

composer.json Outdated Show resolved Hide resolved
@thekid thekid added the enhancement New feature or request label Jun 18, 2023
@thekid
Copy link
Member Author

thekid commented Nov 20, 2023

the requirement to either extend HttpIntegration or ApiGateway to distinguish between the usecases. Extending from HttpApi will raise an error

This incorporates the following class hierarchy:

package com.amazon.aws.lambda:
+ HttpApi
  |- HttpIntegration
  `- ApiGateway

Maybe, we can work around this as follows:

package com.amazon.aws.lambda:
+ HttpIntegration  // New base class
  |- HttpApi       // Uses buffered response mode, as before
  `- HttpStreaming // Name it as it is 🙂

This way, we can avoid the BC break!

@thekid thekid merged commit 3b8bc0d into main Nov 20, 2023
20 checks passed
@thekid thekid deleted the feature/streaming branch November 20, 2023 20:00
@thekid
Copy link
Member Author

thekid commented Nov 20, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant