-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SnapStart support to Amazon.Lambda.Core and Amazon.Lambda.Runtime…
…Support The SnapStart support includes adding a new SnapStart.Registry package for registering and invoking hooks during the SnapStart lifecycle. --------- Co-authored-by: Phil Asmar <phil.asmar@gmail.com> Co-authored-by: Philippe El Asmar <53088140+philasmar@users.noreply.github.com> Co-authored-by: Saksham Bhalla <bhallasaksham@gmail.com> Co-authored-by: Saksham Bhalla <sakshmb@amazon.com> Co-authored-by: Philip Pittle <ppittle@users.noreply.github.com>
- Loading branch information
1 parent
34f39fd
commit 943862f
Showing
31 changed files
with
766 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
**/packages | ||
**/launchSettings.json | ||
**/Debug/ | ||
**/build/ | ||
|
||
**/project.lock.json | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Threading.Tasks; | ||
namespace Amazon.Lambda.Core | ||
{ | ||
#if NET8_0_OR_GREATER | ||
/// <summary> | ||
/// Static class to register callback hooks to during the snapshot and restore phases of Lambda SnapStart. Hooks | ||
/// should be registered as part of the constructor of the type containing the function handler or before the | ||
/// `LambdaBootstrap` is started in executable assembly Lambda functions. | ||
/// </summary> | ||
public static class SnapshotRestore | ||
{ | ||
// We don't want Amazon.Lambda.Core to have any dependencies because the packaged handler code | ||
// that gets uploaded to AWS Lambda could have a version mismatch with the version that is already | ||
// included in the managed runtime. This class allows us to define a simple API that both the | ||
// RuntimeClient and handler code can use to register and then call these actions without | ||
// depending on a specific version of SnapshotRestore.Registry. | ||
private static readonly ConcurrentQueue<Func<ValueTask>> BeforeSnapshotRegistry = new(); | ||
private static readonly ConcurrentQueue<Func<ValueTask>> AfterRestoreRegistry = new(); | ||
|
||
internal static void CopyBeforeSnapshotCallbacksToRegistry(Action<Func<ValueTask>> restoreHooksRegistryMethod) | ||
{ | ||
// To preserve the order of registry, BeforeSnapshotRegistry in Core needs to be a Queue | ||
// These callbacks will be added to the Stack that SnapshotRestore.Registry maintains | ||
while (BeforeSnapshotRegistry.TryDequeue(out var registeredAction)) | ||
{ | ||
restoreHooksRegistryMethod?.Invoke(registeredAction); | ||
} | ||
} | ||
|
||
internal static void CopyAfterRestoreCallbacksToRegistry(Action<Func<ValueTask>> restoreHooksRegistryMethod) | ||
{ | ||
while (AfterRestoreRegistry.TryDequeue(out var registeredAction)) | ||
{ | ||
restoreHooksRegistryMethod?.Invoke(registeredAction); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Register callback hook to be called before Lambda creates a snapshot of the running process. This can be used to warm code in the .NET process or close connections before the snapshot is taken. | ||
/// </summary> | ||
/// <param name="beforeSnapshotAction"></param> | ||
public static void RegisterBeforeSnapshot(Func<ValueTask> beforeSnapshotAction) | ||
{ | ||
BeforeSnapshotRegistry.Enqueue(beforeSnapshotAction); | ||
} | ||
|
||
/// <summary> | ||
/// Register callback hook to be called after Lambda restores a snapshot of the running process. This can be used to ensure uniqueness after restoration. For example reseeding random number generators. | ||
/// </summary> | ||
/// <param name="afterRestoreAction"></param> | ||
public static void RegisterAfterRestore(Func<ValueTask> afterRestoreAction) | ||
{ | ||
AfterRestoreRegistry.Enqueue(afterRestoreAction); | ||
} | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.