Repository navigation
fix(billing): 原子提交任务差额结算并补充隔离回归(Draft) #774
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
Closed
LIghtJUNction
wants to merge
1
commit into
wip/rust-core-go-extensions
from
fix/funds-task-settlement-atomic-20261011
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| name: Isolated task settlement regression | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| pull_request: | ||
| paths: | ||
| - 'apps/api-go/model/task_quota_settlement.go' | ||
| - 'apps/api-go/service/task_billing.go' | ||
| - 'apps/api-go/service/task_quota_settlement_test.go' | ||
| - '.github/workflows/task-settlement-regression.yml' | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| isolated-regression: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 15 | ||
| steps: | ||
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 | ||
| with: | ||
| persist-credentials: false | ||
| - uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 | ||
| with: | ||
| go-version-file: apps/api-go/go.mod | ||
| cache-dependency-path: apps/api-go/go.sum | ||
| - name: Record source and compile without executing application code | ||
| working-directory: apps/api-go | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| evidence="$RUNNER_TEMP/task-settlement-evidence" | ||
| mkdir -p "$evidence" | ||
| git rev-parse HEAD > "$evidence/revision.txt" | ||
| go version > "$evidence/go-version.txt" | ||
| sha256sum model/task_quota_settlement.go service/task_billing.go service/task_quota_settlement_test.go > "$evidence/source-sha256.txt" | ||
| test -z "$(gofmt -l model/task_quota_settlement.go service/task_billing.go service/task_quota_settlement_test.go)" | ||
| go test -p 2 -race -c -o "$RUNNER_TEMP/task-settlement.test" ./service 2>&1 | tee "$evidence/compile.log" | ||
| go test -p 2 -c -o "$RUNNER_TEMP/payment-units.test" ./pkg/paymentpricing 2>&1 | tee "$evidence/payment-compile.log" | ||
| - name: Run tests with a new network namespace and clean environment | ||
| shell: bash | ||
| run: | | ||
| set -uo pipefail | ||
| evidence="$RUNNER_TEMP/task-settlement-evidence" | ||
| status=0 | ||
| for suite in task-settlement payment-units; do | ||
| pattern='.' | ||
| if [[ "$suite" == task-settlement ]]; then pattern='^TestTaskQuotaSettlement'; fi | ||
| sudo unshare --net -- bash -c ' | ||
| set -euo pipefail | ||
| ip link set lo up | ||
| work=$(mktemp -d) | ||
| trap '\''rm -rf "$work"'\'' EXIT | ||
| cd "$work" | ||
| env -i PATH=/usr/bin:/bin HOME="$work" TMPDIR="$work" \ | ||
| "$1" -test.run "$2" -test.count=1 -test.v -test.timeout=120s | ||
| ' isolated-test "$RUNNER_TEMP/$suite.test" "$pattern" 2>&1 | tee "$evidence/$suite.log" | ||
| code=${PIPESTATUS[0]} | ||
| printf '%s\n' "$code" > "$evidence/$suite.exit-code.txt" | ||
| if [[ "$code" != 0 ]]; then status=1; fi | ||
| done | ||
| (cd "$evidence" && sha256sum *.log > logs-sha256.txt) | ||
| exit "$status" | ||
| - name: Upload source-matched test evidence | ||
| if: always() | ||
| uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 | ||
| with: | ||
| name: task-settlement-evidence | ||
| path: ${{ runner.temp }}/task-settlement-evidence | ||
| retention-days: 7 | ||
| if-no-files-found: error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package model | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/LIghtJUNction/api.lmm.best/common" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| var ErrTaskQuotaSettlementConflict = errors.New("task quota settlement conflicts with persisted state") | ||
|
|
||
| // ApplyTaskQuotaSettlement commits the funding, token, usage counters and task | ||
| // marker together. expectedQuota prevents a stale result from replacing a newer | ||
| // settlement. A retry for an already committed target has no financial effect. | ||
| // This function does not choose a price, change rounding, or authorize a refund. | ||
| func ApplyTaskQuotaSettlement(id int64, expectedQuota, actualQuota int) (int, bool, error) { | ||
| if DB == nil || id <= 0 || expectedQuota < 0 || expectedQuota > common.MaxQuota || | ||
| actualQuota <= 0 || actualQuota > common.MaxQuota { | ||
| return 0, false, gorm.ErrInvalidData | ||
| } | ||
| var previousQuota, userID int | ||
| var tokenKey string | ||
| var applied bool | ||
| err := DB.Transaction(func(tx *gorm.DB) error { | ||
| var task Task | ||
| if err := lockForUpdate(tx).Where("id = ?", id).First(&task).Error; err != nil { | ||
| return err | ||
| } | ||
| previousQuota = task.Quota | ||
| if task.UserId <= 0 || task.Quota < 0 || task.Quota > common.MaxQuota || | ||
| task.RefundStatus != "" || task.Status == TaskStatusFailure { | ||
| return ErrTaskQuotaSettlementConflict | ||
| } | ||
| if task.Quota == actualQuota { | ||
| return nil | ||
| } | ||
| if task.Quota != expectedQuota { | ||
| return ErrTaskQuotaSettlementConflict | ||
| } | ||
| delta := actualQuota - task.Quota | ||
| userID = task.UserId | ||
| // Keep this compare-and-swap in the same transaction as every balance. | ||
| // It is also required on databases where SELECT FOR UPDATE is omitted. | ||
| result := tx.Model(&Task{}). | ||
| Where("id = ? AND quota = ? AND (refund_status = '' OR refund_status IS NULL)", id, expectedQuota). | ||
| Updates(map[string]interface{}{"quota": actualQuota, "updated_at": common.GetTimestamp()}) | ||
| if result.Error != nil { | ||
| return result.Error | ||
| } | ||
| if result.RowsAffected != 1 { | ||
| return ErrTaskQuotaSettlementConflict | ||
| } | ||
|
|
||
| if task.PrivateData.BillingSource == "subscription" && task.PrivateData.SubscriptionId > 0 { | ||
| if err := postConsumeUserSubscriptionDeltaTx(tx, task.PrivateData.SubscriptionId, int64(delta)); err != nil { | ||
| return err | ||
| } | ||
| } else if err := ApplyWalletQuotaDelta(tx, userID, -delta); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if task.PrivateData.TokenId > 0 { | ||
| var token Token | ||
| err := lockForUpdate(tx).Where("id = ?", task.PrivateData.TokenId).First(&token).Error | ||
| if err == nil { | ||
| if token.UserId != userID { | ||
| return ErrTaskQuotaSettlementConflict | ||
| } | ||
| tokenKey = token.Key | ||
| if err := tx.Model(&Token{}).Where("id = ? AND user_id = ?", token.Id, userID). | ||
| Updates(map[string]interface{}{ | ||
| "remain_quota": gorm.Expr("remain_quota - ?", delta), | ||
| "used_quota": gorm.Expr("used_quota + ?", delta), | ||
| "accessed_time": common.GetTimestamp(), | ||
| }).Error; err != nil { | ||
| return err | ||
| } | ||
| } else if !errors.Is(err, gorm.ErrRecordNotFound) { | ||
| return err | ||
| } | ||
| // A deleted token must not prevent settlement of its completed task. | ||
| } | ||
| if err := tx.Model(&User{}).Where("id = ?", userID). | ||
| UpdateColumn("used_quota", boundedQuotaCounterExpr("used_quota", delta)).Error; err != nil { | ||
| return err | ||
| } | ||
| if task.ChannelId > 0 { | ||
| if err := tx.Model(&Channel{}).Where("id = ?", task.ChannelId). | ||
| UpdateColumn("used_quota", boundedQuotaCounterExpr("used_quota", delta)).Error; err != nil { | ||
| return err | ||
| } | ||
| } | ||
| applied = true | ||
| return nil | ||
| }) | ||
| if err != nil || !applied { | ||
| return previousQuota, false, err | ||
| } | ||
| // Cache failures cannot turn a committed settlement into a second debit. | ||
| if err := invalidateUserCache(userID); err != nil { | ||
| common.SysLog("invalidate settled task user cache: " + err.Error()) | ||
| } | ||
| if tokenKey != "" { | ||
| if err := invalidateTokenCacheForMutation(tokenKey); err != nil { | ||
| common.SysLog("invalidate settled task token cache: " + err.Error()) | ||
| } | ||
| } | ||
| return previousQuota, true, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛡️ Codex Security Review · Automatically triggered
When a subscription-backed async task with a positive final adjustment completes while the same API-token user starts another subscription-billed request, this transaction locks subscription→token→user, while pre-consume locks user→subscription→token. PostgreSQL/MySQL can deadlock; if settlement is aborted,
RecalculateTaskQuotaonly logs the error after SUCCESS was stored, and terminal tasks are never polled again. A user can repeatedly overlap requests to leave final deltas uncharged. Lock the user before the subscription and durably retry failed settlements.Useful? React with 👍 / 👎.