gpt4 book ai didi

elasticsearch - 通用策略类型返回

转载 作者:行者123 更新时间:2023-12-02 22:57:22 25 4
gpt4 key购买 nike

当NEST错误在HttpCodeResponse范围内并且我有以下一般策略时,我想重试:

 public Policy<D> CreatePolicy<T, D>(
PolicyType policyType)
where T : Exception where D : IApiCallDetails
{
switch (policyType)
{
case PolicyType.WaitAndRetryAsync:

var httpStatusCodesWorthRetrying = new List<string>(this.policyConfiguration.HttpStatusCodesToRetrying.Split(','));

return Policy.Handle<T>()
.OrResult<D>(r => httpStatusCodesWorthRetrying.Select(int.Parse).ToList().Contains(r.HttpStatusCode.Value))
.WaitAndRetryAsync(
this.policyConfiguration.NumberOfRequestRetries,
retryAttempt => TimeSpan.FromSeconds(this.policyConfiguration.TimeInSecondsBetweenRetries),
onRetry: (exception, retryCount, context) =>
{
Log.Error($"[{context.PolicyKey}] Retry {retryCount} due to {exception.Exception.Message}.");
throw exception.Exception;
})
.WithPolicyKey(nameof(PolicyType.WaitAndRetryAsync));

default:
throw new ArgumentOutOfRangeException(nameof(policyType), policyType, null);
}

但是,当我尝试应用到elasticClient调用时,收到错误消息:
cannot implicity convert System.Threading.Tasks.Task<Nest.ISearchResponse<Model.Product>> to System.Threading.Tasks.Task<Elasticsearch.Net.IApiCallDetails>


Policy<IApiCallDetails> policyWaintAndRetry = this.policyFactory.CreatePolicy<ElasticsearchClientException, IApiCallDetails>(PolicyType.WaitAndRetryAsync);


var searchResponse = await policyWaintAndRetry.ExecuteAsync
action: () =>
this.client.SearchAsync<Product>(s => s
.From((request.PageNumber - 1) * request.PageSize)
.Size(request.PageSize)
.Index(GetIndexName(request.TenantId))
.Query(q => tq), CancellationToken.None),
contextData: new Polly.Context("SearchProductSearchAsync"))
.ConfigureAwait(false);

最佳答案

对于NEST 5.x,我认为D应该具有 IResponse 的通用参数约束; NEST中的每个响应都实现IResponse,从ApiCall继承的 IBodyWithApiCallDetails property包含带有HTTP status codeIApiCallDetails

关于elasticsearch - 通用策略类型返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47871267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com