AmazonDynamoDBClient in Lambda - 无法访问已释放的对象。对象名称:'System.Net.Sockets.NetworkStream'



当消息到达队列时,我在lambda中使用AmazonDynamoDBClient将项目移除/插入到dynamodb中。这是删除操作的代码

var request = new DeleteItemRequest()
{
TableName = _tableName,
Key = new Dictionary<string, AttributeValue>()
{
{ "Id", new AttributeValue() { S = id } }
}
};
await _client.DeleteItemAsync(request);

_client在我的DI容器中被设置为singleton,并且存储库类与dynamodb通信。

在负载测试期间(大约1000条消息持续1分钟(,发现有时我可能会收到异常SSLStream: ObjectDisposedException

One or more errors occurred. (Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.) (Cannot access a disposed object.
Object name: 'SslStream'.) (Cannot access a disposed object.
Object name: 'SslStream'.) (Cannot access a disposed object.
Object name: 'SslStream'.) (Cannot access a disposed object.
Object name: 'SslStream'.) ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken cancellationToken)
at Amazon.Runtime.Internal.HttpHandler`1.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.Unmarshaller.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)
at MyLambda.Repository.........

插入btw 的问题相同

我观察了DynamoDb的指标:延迟非常低(10ms(,没有错误。对此,我能做些什么来解决问题并避免此类错误吗?

经过彻底的调查,我发现这个问题的根本原因是lambda MemorySize设置。我用不同的MemorySize做了一个实验,得到了以下结果:

  • 128Mb的SSLStream: ObjectDisposedException
  • 256Mb—例外情况明显减少
  • 512Mb-没有例外

对于每种情况,应用程序使用的内存都不高于110Mb。

因此,我得出了下一个结论:由于AWS根据分配的内存将CPU资源分配给每个lambda,这意味着I/O操作和网络流量也可能受到这些设置的影响。

以下几篇博客文章证实了我的想法:

  • https://www.totalcloud.io/blog/aws-lambda-gotcha-you-must-know-before-configuring-them
  • https://www.jeremydaly.com/15-key-takeaways-from-the-serverless-talk-at-aws-startup-day/

相关内容

最新更新