在文档上传过程中,有一个用于更新数据库的API端点。该端点通过AWS API网关暴露,并已指向AWS SQS来处理请求。该Queue触发lambda函数并调用lambda内的API方法来更新数据库。当有大量请求时,(15-20个文档上传请求)lambda函数失败,抛出“响应状态代码未指示成功:400'(400错误)。当请求数量很少时,它可以正常工作。原因是什么?
λ代码。
public async Task FunctionHandler(SQSEvent evnt, ILambdaContext context)
{
try
{
HttpClient client = new();
foreach (var message in evnt.Records)
{
await ProcessMessageAsync(message, context, client);
}
}
catch (Exception ex)
{
throw new UploaderException(ex.Message);
}
}
//Private method
private async Task ProcessMessageAsync(SQSEvent.SQSMessage message, ILambdaContext context, HttpClient client) {
string item = string.Empty;
string methodName = string.Empty;
string httpMethod = string.Empty;
foreach (var attribute in message.MessageAttributes)
{
if (attribute.Key.ToLower() == "item")
{
item = attribute.Value.StringValue;
}
if (attribute.Key.ToLower() == "methodname")
{
methodName = attribute.Value.StringValue;
}
if (attribute.Key.ToLower() == "httpmethod")
{
httpMethod = attribute.Value.StringValue;
}
if (attribute.Key.ToLower() != "methodname" || attribute.Key.ToLower() != "httpmethod")
{
client.DefaultRequestHeaders.Add(attribute.Key, attribute.Value.StringValue);
}
}
if (string.IsNullOrWhiteSpace(item))
{
throw new UploaderException("Could not find item");
}
string baseUrl = Environment.GetEnvironmentVariable(item.ToUpper());
var content = new StringContent(message.Body, System.Text.Encoding.UTF8, "application/json");
context.Logger.LogLine($"URL: {baseUrl}{methodName}");
HttpResponseMessage response;
if (httpMethod.ToUpper() == "POST")
{
response = await client.PostAsync($"{baseUrl}{methodName}", content);
}
else
{
response = await client.PutAsync($"{baseUrl}{methodName}", content);
}
response.EnsureSuccessStatusCode();
context.Logger.LogLine("Document upload success");
await Task.CompletedTask;
}
1条答案
按热度按时间z0qdvdin1#
有许多不同的进程同时修改同一个客户端(client.DefaultRequestHeaders.Add(..))-这可能是个问题。
我建议为每个消息/HTTP请求创建一个单独的headers-object,并且完全不依赖于默认的头(除非它们**在所有请求中共享)。