Support IDisposable for Function Handlers
- Ngôn ngữ chính
- C#
- Star
- 1.7k
- Fork
- 503
- Merge trung bình
- 1 ngày 18 giờ
- Pull request đã merge (30 ngày)
- 21
Mô tả
### 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
Hướng dẫn đóng góp
Hướng nghiên cứu
Bắt đầu bằng việc xem xét hành vi của handler trong Amazon.Lambda.Core 2.7.1 và vòng đời SIGTERM được mô tả trong issue này, sau đó so sánh với issue 342 trước đó. Được coi là hoàn tất khi runtime cung cấp một cách được xác định để dispose các function handler khi Lambda gửi SIGTERM, bao gồm việc dọn dẹp như flush log hoặc đóng các kết nối.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- aws, csharp
- Lĩnh vực
- backend, cloud
- Loại issue
- Tính năng
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100