Skip to content

Commit c7d6057

Browse files
committed
WIP - add Snapstart helper to AbstractAspNetCoreFunction
1 parent bfa3ca9 commit c7d6057

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

Libraries/src/Amazon.Lambda.AspNetCoreServer/AbstractAspNetCoreFunction.cs

+39
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System;
99
using System.Collections.Generic;
1010
using System.IO;
11+
using System.Linq;
12+
using System.Net.Http;
1113
using System.Reflection;
1214
using System.Text;
1315
using System.Threading.Tasks;
@@ -251,6 +253,17 @@ protected virtual IHostBuilder CreateHostBuilder()
251253
return builder;
252254
}
253255

256+
/// <summary>
257+
/// TODO - copy from AddAWSLambdaBeforeSnapshotRequest
258+
/// Override to register a <see cref="HttpRequestMessage"/> that will be executed
259+
/// within <see cref="SnapshotRestore.RegisterBeforeSnapshot"/>.
260+
///
261+
/// Improves performance of SnapStart
262+
/// </summary>
263+
/// <returns></returns>
264+
protected virtual IEnumerable<TREQUEST> RegisterBeforeSnapshotRequest() => Enumerable.Empty<TREQUEST>();
265+
266+
254267
private protected bool IsStarted
255268
{
256269
get
@@ -284,6 +297,32 @@ protected void Start()
284297
"instead of ConfigureWebHostDefaults to make sure the property Lambda services are registered.");
285298
}
286299
_logger = ActivatorUtilities.CreateInstance<Logger<AbstractAspNetCoreFunction<TREQUEST, TRESPONSE>>>(this._hostServices);
300+
301+
#if NET8_0_OR_GREATER
302+
var beforeSnapstartRequests = RegisterBeforeSnapshotRequest();
303+
304+
Amazon.Lambda.Core.SnapshotRestore.RegisterBeforeSnapshot(async () =>
305+
{
306+
foreach (var request in beforeSnapstartRequests)
307+
{
308+
var invokeTimes = 5;
309+
310+
InvokeFeatures features = new InvokeFeatures();
311+
MarshallRequest(features, request, new SnapStartEmptyLambdaContext());
312+
313+
var context = CreateContext(features);
314+
315+
var lambdaContext = new SnapStartEmptyLambdaContext();
316+
317+
for (var i = 0; i < invokeTimes; i++)
318+
{
319+
await ProcessRequest(lambdaContext, context, features);
320+
}
321+
}
322+
});
323+
324+
#endif
325+
287326
}
288327

289328
/// <summary>

Libraries/src/Amazon.Lambda.AspNetCoreServer/Amazon.Lambda.AspNetCoreServer.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
<ItemGroup>
3333
<FrameworkReference Include="Microsoft.AspNetCore.App" />
34+
<ProjectReference Include="..\Amazon.Lambda.RuntimeSupport\Amazon.Lambda.RuntimeSupport.csproj" />
3435
<ProjectReference Include="..\Amazon.Lambda.Serialization.SystemTextJson\Amazon.Lambda.Serialization.SystemTextJson.csproj" />
3536
</ItemGroup>
3637

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using Amazon.Lambda.Core;
7+
using Amazon.Lambda.RuntimeSupport;
8+
9+
namespace Amazon.Lambda.AspNetCoreServer.Internal;
10+
11+
internal class SnapStartEmptyLambdaContext : ILambdaContext, ICognitoIdentity, IClientContext
12+
{
13+
private LambdaEnvironment _lambdaEnvironment = new();
14+
15+
public string TraceId => string.Empty;
16+
public string AwsRequestId => string.Empty;
17+
public IClientContext ClientContext => this;
18+
public string FunctionName => _lambdaEnvironment.FunctionName;
19+
public string FunctionVersion => _lambdaEnvironment.FunctionVersion;
20+
public ICognitoIdentity Identity => this;
21+
public string InvokedFunctionArn => string.Empty;
22+
public ILambdaLogger Logger => null;
23+
public string LogGroupName => _lambdaEnvironment.LogGroupName;
24+
public string LogStreamName => _lambdaEnvironment.LogStreamName;
25+
public int MemoryLimitInMB => 128;
26+
public TimeSpan RemainingTime => TimeSpan.FromMilliseconds(100);
27+
public string IdentityId { get; }
28+
public string IdentityPoolId { get; }
29+
public IDictionary<string, string> Environment { get; } = new Dictionary<string, string>();
30+
public IClientApplication Client { get; }
31+
public IDictionary<string, string> Custom { get; } = new Dictionary<string, string>();
32+
}

0 commit comments

Comments
 (0)