From 65a0e4898870be3ba90bbfe63b92d4a3c5b8b08b Mon Sep 17 00:00:00 2001 From: MaysWind Date: Thu, 1 May 2025 22:09:13 +0800 Subject: [PATCH] fix repeated request error when submitting import transaction again after the first submission failed --- pkg/api/base.go | 9 ++++++++- pkg/api/transactions.go | 1 + pkg/duplicatechecker/duplicate_checker.go | 1 + pkg/duplicatechecker/duplicate_checker_container.go | 7 ++++++- pkg/duplicatechecker/in_memory_duplicate_checker.go | 5 +++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/api/base.go b/pkg/api/base.go index e5275558..62ed6c5b 100644 --- a/pkg/api/base.go +++ b/pkg/api/base.go @@ -113,13 +113,20 @@ func (a *ApiUsingDuplicateChecker) GetSubmissionRemark(checkerType duplicatechec return a.container.GetSubmissionRemark(checkerType, uid, identification) } -// SetSubmissionRemarkIfEnable saves the identification and remark to in-memory cache by the current duplicate checker if the duplicate submission check is enabled +// SetSubmissionRemarkIfEnable saves the identification and remark by the current duplicate checker if the duplicate submission check is enabled func (a *ApiUsingDuplicateChecker) SetSubmissionRemarkIfEnable(checkerType duplicatechecker.DuplicateCheckerType, uid int64, identification string, remark string) { if a.CurrentConfig().EnableDuplicateSubmissionsCheck { a.container.SetSubmissionRemark(checkerType, uid, identification, remark) } } +// RemoveSubmissionRemarkIfEnable removes the identification and remark by the current duplicate checker if the duplicate submission check is enabled +func (a *ApiUsingDuplicateChecker) RemoveSubmissionRemarkIfEnable(checkerType duplicatechecker.DuplicateCheckerType, uid int64, identification string) { + if a.CurrentConfig().EnableDuplicateSubmissionsCheck { + a.container.RemoveSubmissionRemark(checkerType, uid, identification) + } +} + // CheckFailureCount returns whether the failure count of the specified IP and user has reached the limit and increases the failure count func (a *ApiUsingDuplicateChecker) CheckFailureCount(c *core.WebContext, uid int64) error { if a.CurrentConfig().MaxFailuresPerIpPerMinute > 0 { diff --git a/pkg/api/transactions.go b/pkg/api/transactions.go index defd8c49..f9da48b8 100644 --- a/pkg/api/transactions.go +++ b/pkg/api/transactions.go @@ -1439,6 +1439,7 @@ func (a *TransactionsApi) TransactionImportHandler(c *core.WebContext) (any, *er count := len(newTransactions) if err != nil { + a.RemoveSubmissionRemarkIfEnable(duplicatechecker.DUPLICATE_CHECKER_TYPE_IMPORT_TRANSACTIONS, uid, transactionImportReq.ClientSessionId) log.Errorf(c, "[transactions.TransactionImportHandler] failed to import %d transactions for user \"uid:%d\", because %s", count, uid, err.Error()) return nil, errs.Or(err, errs.ErrOperationFailed) } diff --git a/pkg/duplicatechecker/duplicate_checker.go b/pkg/duplicatechecker/duplicate_checker.go index dfb5a472..cc87728b 100644 --- a/pkg/duplicatechecker/duplicate_checker.go +++ b/pkg/duplicatechecker/duplicate_checker.go @@ -6,6 +6,7 @@ import "time" type DuplicateChecker interface { GetSubmissionRemark(checkerType DuplicateCheckerType, uid int64, identification string) (bool, string) SetSubmissionRemark(checkerType DuplicateCheckerType, uid int64, identification string, remark string) + RemoveSubmissionRemark(checkerType DuplicateCheckerType, uid int64, identification string) GetOrSetCronJobRunningInfo(jobName string, runningInfo string, runningInterval time.Duration) (bool, string) RemoveCronJobRunningInfo(jobName string) GetFailureCount(failureKey string) uint32 diff --git a/pkg/duplicatechecker/duplicate_checker_container.go b/pkg/duplicatechecker/duplicate_checker_container.go index 8ab1e388..3381a355 100644 --- a/pkg/duplicatechecker/duplicate_checker_container.go +++ b/pkg/duplicatechecker/duplicate_checker_container.go @@ -34,11 +34,16 @@ func (c *DuplicateCheckerContainer) GetSubmissionRemark(checkerType DuplicateChe return c.Current.GetSubmissionRemark(checkerType, uid, identification) } -// SetSubmissionRemark saves the identification and remark to in-memory cache by the current duplicate checker +// SetSubmissionRemark saves the identification and remark by the current duplicate checker func (c *DuplicateCheckerContainer) SetSubmissionRemark(checkerType DuplicateCheckerType, uid int64, identification string, remark string) { c.Current.SetSubmissionRemark(checkerType, uid, identification, remark) } +// RemoveSubmissionRemark removes the identification and remark by the current duplicate checker +func (c *DuplicateCheckerContainer) RemoveSubmissionRemark(checkerType DuplicateCheckerType, uid int64, identification string) { + c.Current.RemoveSubmissionRemark(checkerType, uid, identification) +} + // GetOrSetCronJobRunningInfo returns the running info when the cron job is running or saves the running info by the current duplicate checker func (c *DuplicateCheckerContainer) GetOrSetCronJobRunningInfo(jobName string, runningInfo string, runningInterval time.Duration) (bool, string) { return c.Current.GetOrSetCronJobRunningInfo(jobName, runningInfo, runningInterval) diff --git a/pkg/duplicatechecker/in_memory_duplicate_checker.go b/pkg/duplicatechecker/in_memory_duplicate_checker.go index 73365a1f..0f68d695 100644 --- a/pkg/duplicatechecker/in_memory_duplicate_checker.go +++ b/pkg/duplicatechecker/in_memory_duplicate_checker.go @@ -42,6 +42,11 @@ func (c *InMemoryDuplicateChecker) SetSubmissionRemark(checkerType DuplicateChec c.cache.Set(c.getCacheKey(checkerType, uid, identification), remark, cache.DefaultExpiration) } +// RemoveSubmissionRemark removes the identification and remark in in-memory cache +func (c *InMemoryDuplicateChecker) RemoveSubmissionRemark(checkerType DuplicateCheckerType, uid int64, identification string) { + c.cache.Delete(c.getCacheKey(checkerType, uid, identification)) +} + // GetOrSetCronJobRunningInfo returns the running info when the cron job is running or saves the running info by the current duplicate checker func (c *InMemoryDuplicateChecker) GetOrSetCronJobRunningInfo(jobName string, runningInfo string, runningInterval time.Duration) (bool, string) { c.mutex.Lock()