RetryAttempt and static lambdas in V8 (Zero heap allocation) #2416
Answered
by
martincostello
ArminShoeibi
asked this question in
Q&A
-
Hello there. I have this code, and I need to improve two parts of it. One is the RetryAttempt, which was present in v7, and another is static lambda. My public sealed class SamanKishHttpClient(HttpClient httpClient, IOptions<SamanKishOptions> options, ResiliencePipelineProvider<string> resiliencePipelineProvider, MeterRegistry meterRegistry) : ISamanKishHttpClient
{
public async Task<ApiCallResult<SamanKishTransactionAckResponseDto?>> VerifyTransactionAsync(SamanKishTransactionAckRequestDto request)
{
var context = ResilienceContextPool.Shared.Get();
context.Properties.Set(PollyResilienceKeys.RETRY_COUNT, 0);
var pipeline = resiliencePipelineProvider.GetPipeline<ApiCallResult<SamanKishTransactionAckResponseDto?>>(Constants.SAMANKISH_RETRY_VERIFY);
try
{
return await pipeline.ExecuteAsync(callback: async (pipelineContext) =>
{
var retryCountExists = pipelineContext.Properties.TryGetValue(PollyResilienceKeys.RETRY_COUNT, out var retryCountValue);
if (retryCountExists)
{
pipelineContext.Properties.Set(PollyResilienceKeys.RETRY_COUNT, retryCountValue + 1);
if (pipelineContext.Properties.GetValue(PollyResilienceKeys.RETRY_COUNT, retryCountValue) > 1)
{
meterRegistry.SetOpayProviderRetryCount(PaymentProviderType.SamanKish, Constants.VERIFY_REQUEST_TYPE);
}
}
return await httpClient.PostAsJsonV2Async<SamanKishTransactionAckRequestDto, SamanKishTransactionAckResponseDto?>(options.Value.Verify, request);
}, context);
}
finally
{
ResilienceContextPool.Shared.Return(context);
}
}
} HttpClient httpClient -> Transient How can we improve this code in the context of Polly and allocate less? |
Beta Was this translation helpful? Give feedback.
Answered by
martincostello
Dec 16, 2024
Replies: 1 comment 2 replies
-
You can do something like this: public sealed class SamanKishHttpClient(HttpClient httpClient, IOptions<SamanKishOptions> options, ResiliencePipelineProvider<string> resiliencePipelineProvider, MeterRegistry meterRegistry) : ISamanKishHttpClient
{
public async Task<ApiCallResult<SamanKishTransactionAckResponseDto?>> VerifyTransactionAsync(SamanKishTransactionAckRequestDto request)
{
var context = ResilienceContextPool.Shared.Get();
context.Properties.Set(PollyResilienceKeys.RETRY_COUNT, 0);
var pipeline = resiliencePipelineProvider.GetPipeline<ApiCallResult<SamanKishTransactionAckResponseDto?>>(Constants.SAMANKISH_RETRY_VERIFY);
try
{
- return await pipeline.ExecuteAsync(callback: async (pipelineContext) =>
+ return await pipeline.ExecuteAsync(static async (pipelineContext, state) =>
{
var retryCountExists = pipelineContext.Properties.TryGetValue(PollyResilienceKeys.RETRY_COUNT, out var retryCountValue);
if (retryCountExists)
{
pipelineContext.Properties.Set(PollyResilienceKeys.RETRY_COUNT, retryCountValue + 1);
if (pipelineContext.Properties.GetValue(PollyResilienceKeys.RETRY_COUNT, retryCountValue) > 1)
{
- meterRegistry.SetOpayProviderRetryCount(PaymentProviderType.SamanKish, Constants.VERIFY_REQUEST_TYPE);
+ state.meterRegistry.SetOpayProviderRetryCount(PaymentProviderType.SamanKish, Constants.VERIFY_REQUEST_TYPE);
}
}
- return await httpClient.PostAsJsonV2Async<SamanKishTransactionAckRequestDto, SamanKishTransactionAckResponseDto?>(options.Value.Verify, request);
+ return await state.httpClient.PostAsJsonV2Async<SamanKishTransactionAckRequestDto, SamanKishTransactionAckResponseDto?>(state.Verify, state.request);
- }, context);
+ }, context, (httpClient, options, options.Value.Verify, meterRegistry, request));
}
finally
{
ResilienceContextPool.Shared.Return(context);
}
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
ArminShoeibi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can do something like this:
public sealed class SamanKishHttpClient(HttpClient httpClient, IOptions<SamanKishOptions> options, ResiliencePipelineProvider<string> resiliencePipelineProvider, MeterRegistry meterRegistry) : ISamanKishHttpClient { public async Task<ApiCallResult<SamanKishTransactionAckResponseDto?>> VerifyTransactionAsync(SamanKishTransactionAckRequestDto request) { var context = ResilienceContextPool.Shared.Get(); context.Properties.Set(PollyResilienceKeys.RETRY_COUNT, 0); var pipeline = resiliencePipelineProvider.GetPipeline<ApiCallResult<SamanKishTransactionAckResponseDto?>>(Constants.SAMANKISH_RETRY_VERIFY); try { - …