aws / aws/aws-lambda-dotnet

Support IDisposable for Function Handlers

Offen
#2,159 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
feature-request module/lambda-client-lib needs-investigation p2
Vorherrschende Sprache
C#
Sterne
1.7k
Forks
503
Ø Merge
1 T. 5 Std.
Gemergte PRs (30 T.)
18

Beschreibung

### Describe the feature

It's the same feature request as https://github.com/aws/aws-lambda-dotnet/issues/342 - to dispose function handler when Lambda service deletes the execution environment.

It was closed because there was no hook from lambda service at that time. But now there is - Lambda sends SIGTERM to runtime before reclaiming the environment.

### Use Case

Any features which requires some cleanup.
Flushing logs and traces, closing DB connections, etc.

### Proposed Solution

I have implemented it for my handlers via smth like that:
```csharp
public abstract class AbstractLambdaHandler : IDisposable
{
private readonly PosixSignalRegistration sigTermRegistration;

protected AbstractLambdaHandler()
{
// AWS Lambda service sends SIGTERM signal before killing lambda container, but
// lambda-dotnet runtime does not handle it yet - https://github.com/aws/aws-lambda-dotnet/issues/342 .
// Until it is implemented, we use custom signal registration to dispose lambda handler.
sigTermRegistration = PosixSignalRegistration.Create(PosixSignal.SIGTERM, _ =>
{
Logger?.LogDebug("SIGTERM signal received. Disposing Lambda Handler.");
Dispose();
});
}

public virtual void Dispose()
{
sigTermRegistration?.Dispose();
// Dispose other things.
}
}
```

It would be nice to have it provided by the runtime itself though.

### Other Information

_No response_

### Acknowledgements

- [x] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### AWS .NET SDK and/or Package version used

Amazon.Lambda.Core 2.7.1

### Targeted .NET Platform

.NET 8

### Operating System and version

AmazonLinux

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit der Überprüfung des Handler-Verhaltens von Amazon.Lambda.Core 2.7.1 und des in diesem Issue beschriebenen SIGTERM-Lebenszyklus und vergleiche ihn anschließend mit dem früheren Issue 342. Als erledigt gilt die Aufgabe, wenn die Runtime eine definierte Möglichkeit bietet, Function-Handler zu disposen, wenn Lambda SIGTERM sendet, einschließlich Bereinigung wie dem Leeren von Logs oder dem Schließen von Verbindungen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
aws, csharp
Bereich
backend, cloud
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.