xp-forge / lambda-ws

AWS Lambda Webservices for the XP Framework
1 stars 0 forks source link

Implement HTTP response streaming #10

Closed thekid closed 11 months ago

thekid commented 1 year ago

This PR implements HTTP response streaming on top of https://github.com/xp-forge/lambda/issues/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

thekid commented 11 months ago

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 commented 11 months ago

Released in https://github.com/xp-forge/lambda-ws/releases/tag/v2.0.0