因此,当使用 AmazonKinesisFireHoseClient 将记录放入 kinesis 时,我的网站会在高负载期间崩溃。
我没有收到任何异常记录,它只是死掉了,您需要重新启动应用程序才能使其再次工作,有时它只是在崩溃后开始自行工作。
我知道这与打开的连接数有关,但我不知道如何解决它。
这是我实例化为单例的记录器
public class KinesisFirehoseLogger<T>
{
private string streamName;
private AmazonKinesisFirehoseClient client;
protected ILogger logger;
public KinesisFirehoseLogger(IKinesisLogConfig config, ILogger logger)
{
this.streamName = config.StreamName;
this.logger = logger;
this.PartitionKey = config.PartitionKey;
AWSCredentials credentials = new BasicAWSCredentials(config.AccessKey, config.SecretKey);
this.client = new AmazonKinesisFirehoseClient(credentials, config.Region);
}
public async Task<bool> WriteToKinesisAsync(T logObject)
{
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(logObject) + "\n")))
{
PutRecordRequest putRecordRequest = new PutRecordRequest() { DeliveryStreamName = this.streamName, Record = new Record() { Data = ms } };
// Put record into the DeliveryStream
return (await client.PutRecordAsync(putRecordRequest)).HttpStatusCode == HttpStatusCode.OK;
}
}
}
我是否应该在某处使用 AmazonKinesisFirehoseClient 的 dispose() 方法?我怎么知道什么时候调用它。
假设
client.PutRecordAsync
打开连接,那么将其放入 using
块中也可能是有意义的。例如:
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(logObject) + "\n")))
{
PutRecordRequest putRecordRequest = new PutRecordRequest() { DeliveryStreamName = this.streamName, Record = new Record() { Data = ms } };
// Put record into the DeliveryStream
using (PutRecordResponse response = await client.PutRecordAsync(putRecordRequest))
{
return response.HttpStatusCode == HttpStatusCode.Ok;
}
}
您可以使用 Queue-Fair 减少站点上打开的连接数量并防止崩溃 - https://queue-fair/virtual-waiting-room - 他们提供免费服务。