|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using System.Net; |
| 6 | +using System.Text.Json; |
| 7 | +using Amazon.Lambda.APIGatewayEvents; |
| 8 | +using Amazon.Lambda.RuntimeSupport; |
| 9 | +using Amazon.Lambda.RuntimeSupport.Helpers; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | + |
| 12 | +namespace Amazon.Lambda.AspNetCoreServer.Hosting.Internal; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Contains the plumbing to register a user provided <see cref="Func{HttpClient, Task}"/> inside |
| 16 | +/// <see cref="Amazon.Lambda.Core.SnapshotRestore.RegisterBeforeSnapshot"/>. |
| 17 | +/// The function is meant to initialize the asp.net and lambda pipelines during |
| 18 | +/// <see cref="Core.SnapshotRestore.RegisterBeforeSnapshot"/> and improve the |
| 19 | +/// performance gains offered by SnapStart. |
| 20 | +/// <para /> |
| 21 | +/// It works by construction a specialized <see cref="HttpClient" /> that will intercept requests |
| 22 | +/// and saved them inside <see cref="LambdaSnapstartInitializerHttpMessageHandler.CapturedHttpRequests" />. |
| 23 | +/// <para /> |
| 24 | +/// Intercepted requests are then be processed later by <see cref="SnapstartHelperLambdaRequests.ExecuteSnapstartInitRequests"/> |
| 25 | +/// which will route them correctly through a simulated asp.net/lambda pipeline. |
| 26 | +/// </summary> |
| 27 | +internal class LambdaSnapstartExecuteRequestsBeforeSnapshotHelper |
| 28 | +{ |
| 29 | + private readonly LambdaEventSource _lambdaEventSource; |
| 30 | + |
| 31 | + public LambdaSnapstartExecuteRequestsBeforeSnapshotHelper(LambdaEventSource lambdaEventSource) |
| 32 | + { |
| 33 | + _lambdaEventSource = lambdaEventSource; |
| 34 | + } |
| 35 | + |
| 36 | + /// <inheritdoc cref="RegisterInitializerRequests"/> |
| 37 | + [RequiresUnreferencedCode("Serializes object to json")] |
| 38 | + public void RegisterInitializerRequests(HandlerWrapper handlerWrapper) |
| 39 | + { |
| 40 | + #if NET8_0_OR_GREATER |
| 41 | + |
| 42 | + Amazon.Lambda.Core.SnapshotRestore.RegisterBeforeSnapshot(async () => |
| 43 | + { |
| 44 | + // Construct specialized HttpClient that will intercept requests and saved them inside |
| 45 | + // LambdaSnapstartInitializerHttpMessageHandler.CapturedHttpRequests. |
| 46 | + // |
| 47 | + // They will be processed later by SnapstartHelperLambdaRequests.ExecuteSnapstartInitRequests which will |
| 48 | + // route them correctly through a simulated lambda pipeline. |
| 49 | + var messageHandlerThatCollectsRequests = new LambdaSnapstartInitializerHttpMessageHandler(_lambdaEventSource); |
| 50 | + |
| 51 | + var httpClientThatCollectsRequests = new HttpClient(messageHandlerThatCollectsRequests); |
| 52 | + httpClientThatCollectsRequests.BaseAddress = LambdaSnapstartInitializerHttpMessageHandler.BaseUri; |
| 53 | + |
| 54 | + // "Invoke" each registered request function. Requests will be captured inside. |
| 55 | + // LambdaSnapstartInitializerHttpMessageHandler.CapturedHttpRequests. |
| 56 | + await Registrar.Execute(httpClientThatCollectsRequests); |
| 57 | + |
| 58 | + // Request are now in CapturedHttpRequests. Serialize each one into a json object |
| 59 | + // and execute the request through the lambda pipeline (ie handlerWrapper). |
| 60 | + foreach (var req in LambdaSnapstartInitializerHttpMessageHandler.CapturedHttpRequests) |
| 61 | + { |
| 62 | + var json = JsonSerializer.Serialize(req); |
| 63 | + |
| 64 | + // TODO - inline |
| 65 | + await SnapstartHelperLambdaRequests.ExecuteSnapstartInitRequests(json, times: 5, handlerWrapper); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + #endif |
| 70 | + } |
| 71 | + |
| 72 | + /// <inheritdoc cref="ServiceCollectionExtensions.AddAWSLambdaBeforeSnapshotRequest"/> |
| 73 | + internal static BeforeSnapstartRequestRegistrar Registrar = new(); |
| 74 | + |
| 75 | + internal class BeforeSnapstartRequestRegistrar |
| 76 | + { |
| 77 | + private List<Func<HttpClient, Task>> beforeSnapstartFuncs = new(); |
| 78 | + |
| 79 | + public void Register(Func<HttpClient, Task> beforeSnapstartRequest) |
| 80 | + { |
| 81 | + beforeSnapstartFuncs.Add(beforeSnapstartRequest); |
| 82 | + } |
| 83 | + |
| 84 | + internal async Task Execute(HttpClient client) |
| 85 | + { |
| 86 | + foreach (var f in beforeSnapstartFuncs) |
| 87 | + await f(client); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private class LambdaSnapstartInitializerHttpMessageHandler : HttpMessageHandler |
| 92 | + { |
| 93 | + private LambdaEventSource _lambdaEventSource; |
| 94 | + |
| 95 | + public static Uri BaseUri { get; } = new Uri("http://localhost"); |
| 96 | + |
| 97 | + public static List<object> CapturedHttpRequests { get; } = new(); |
| 98 | + |
| 99 | + public LambdaSnapstartInitializerHttpMessageHandler(LambdaEventSource lambdaEventSource) |
| 100 | + { |
| 101 | + _lambdaEventSource = lambdaEventSource; |
| 102 | + } |
| 103 | + |
| 104 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 105 | + { |
| 106 | + // Copy request to correct request, ie APIGatewayProxyRequest |
| 107 | + |
| 108 | + // TODO - IMPLEMENT |
| 109 | + var translatedRequest = new APIGatewayProxyRequest |
| 110 | + { |
| 111 | + Path = request.RequestUri.MakeRelativeUri(BaseUri).ToString(), |
| 112 | + HttpMethod = request.Method.ToString() |
| 113 | + }; |
| 114 | + |
| 115 | + CapturedHttpRequests.Add(translatedRequest); |
| 116 | + |
| 117 | + return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments