Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VBase Throttling #173

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed
- Reduce calls to VBase
- Improve logging of VBase calls
- Retry GetPaymentData VBase calls

## [1.22.1] - 2024-06-25

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion dotnet/Controlers/RoutesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
{
//(createPaymentResponse, paymentsResponse) = await this._cybersourcePaymentService.CreatePayment(createPaymentRequest);
var createPaymentTask = this._cybersourcePaymentService.CreatePayment(createPaymentRequest);
var winner = await Task.WhenAny(createPaymentTask, DelayedDummyResultTask<(CreatePaymentResponse, PaymentsResponse)>(TimeSpan.FromSeconds(25)));
var winner = await Task.WhenAny(createPaymentTask, DelayedDummyResultTask<(CreatePaymentResponse, PaymentsResponse)>(TimeSpan.FromSeconds(20)));
if (winner == createPaymentTask)
{
//_context.Vtex.Logger.Debug("CreatePayment", "Timeout", $"Processed {createPaymentRequest.PaymentId} in time!");
Expand Down Expand Up @@ -874,7 +874,7 @@
return Json(vtexTaxResponse);
}

public async Task<IActionResult> HealthCheck()

Check warning on line 877 in dotnet/Controlers/RoutesController.cs

View workflow job for this annotation

GitHub Actions / QE / Lint .Net

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
return Json("--result goes here");
}
Expand Down
1 change: 1 addition & 0 deletions dotnet/Data/CybersourceConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class CybersourceConstants
public const string CACHE_BUCKET = "tax-cache";
public const string SIGNATURE_ENCODING = "base64";
public const string SIGNATURE_KEY_FORMAT = "base64";
public const string TOO_MANY_REQUESTS = "TooManyRequests";

public const string PAYMENTS = "/pts/v2/";
public const string RISK = "/risk/v1/";
Expand Down
17 changes: 14 additions & 3 deletions dotnet/Data/CybersourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ public async Task<PaymentData> GetPaymentData(string paymentIdentifier)
null, true);
if (sendResponse.StatusCode == HttpStatusCode.NotFound.ToString())
{
_context.Vtex.Logger.Warn("GetPaymentData", null, $"Payment Id '{paymentIdentifier}' Not Found.");
_context.Vtex.Logger.Debug("GetPaymentData", null, $"Payment Id '{paymentIdentifier}' Not Found.");
return null;
}
else if (!sendResponse.Success)
{
_context.Vtex.Logger.Error("GetPaymentData", null, $"Could not retrieve Payment Id '{paymentIdentifier}' [{sendResponse.StatusCode}] ");
return null;
}

Expand All @@ -114,7 +119,7 @@ public async Task<PaymentData> GetPaymentData(string paymentIdentifier)
return paymentData;
}

public async Task SavePaymentData(string paymentIdentifier, PaymentData paymentData)
public async Task SavePaymentData(string paymentIdentifier, PaymentData paymentData, int retryNumber = 0)
{
if (paymentData == null)
{
Expand All @@ -128,7 +133,7 @@ public async Task SavePaymentData(string paymentIdentifier, PaymentData paymentD
jsonSerializedCreatePaymentRequest);
if (!sendResponse.Success)
{
_context.Vtex.Logger.Error("SavePaymentData", null, $"Failed",
_context.Vtex.Logger.Error("SavePaymentData", null, $"({retryNumber}) Failed",
null,
new[]
{
Expand All @@ -137,6 +142,12 @@ public async Task SavePaymentData(string paymentIdentifier, PaymentData paymentD
( "paymentIdentifier", paymentIdentifier ),
( "paymentData", JsonConvert.SerializeObject(paymentData) )
});

if (sendResponse.StatusCode.Equals(CybersourceConstants.TOO_MANY_REQUESTS) && retryNumber < 5)
{
await Task.Delay(retryNumber * 100);
await this.SavePaymentData(paymentIdentifier, paymentData, ++retryNumber);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion dotnet/Data/ICybersourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface ICybersourceRepository
Task<bool> SetMerchantSettings(MerchantSettings merchantSettings);

Task<PaymentData> GetPaymentData(string paymentIdentifier);
Task SavePaymentData(string paymentIdentifier, PaymentData paymentData);
Task SavePaymentData(string paymentIdentifier, PaymentData paymentData, int retryNumber = 0);

Task<CreatePaymentRequest> GetCreatePaymentRequest(string id);
Task SaveCreatePaymentRequest(string id, CreatePaymentRequest createPaymentRequest);
Expand Down
7 changes: 5 additions & 2 deletions dotnet/Services/CybersourcePaymentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@
{
// Setting the TimedOut flag to true so that any follow up requests will check for previous captures
paymentData.TimedOut = true;
await _cybersourceRepository.SavePaymentData(paymentData.PaymentId, paymentData);
// await _cybersourceRepository.SavePaymentData(paymentData.PaymentId, paymentData);
}

bool isPayerAuth = false;
Expand Down Expand Up @@ -787,7 +787,10 @@
paymentData.Value = capturedAmount;
}

await _cybersourceRepository.SavePaymentData(createPaymentRequest.PaymentId, paymentData);
if (!createPaymentResponse.Equals(CybersourceConstants.VtexAuthStatus.Denied))
{
await _cybersourceRepository.SavePaymentData(createPaymentRequest.PaymentId, paymentData);
}
}

if (doCancel)
Expand Down Expand Up @@ -2440,7 +2443,7 @@
}
}
}
catch (Exception ex)

Check warning on line 2446 in dotnet/Services/CybersourcePaymentService.cs

View workflow job for this annotation

GitHub Actions / QE / Lint .Net

The variable 'ex' is declared but never used
{
_context.Vtex.Logger.Warn("GetPropertyValue", null,
"Error: ",
Expand Down
Loading