From 313bb692c72bc194eefaff0133ecbc5966568fbc Mon Sep 17 00:00:00 2001 From: qhd <1766646056@qq.com> Date: Wed, 1 Jul 2026 19:32:52 +0800 Subject: [PATCH 1/6] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=A7=9F=E6=88=B7?= =?UTF-8?q?=E4=BD=99=E9=A2=9D=E5=93=8D=E5=BA=94=E7=BB=93=E6=9E=84=E5=8F=8A?= =?UTF-8?q?=E7=A7=9F=E6=88=B7ID=E5=BC=95=E7=94=A8=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新 TenantSurplusResp 结构以匹配实际的嵌套 JSON 响应格式, 并修正 worker 中 billing 逻辑错误引用的租户ID变量。 --- service/gateway/gateway_http_service.go | 6 ++++-- service/task/worker.go | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/service/gateway/gateway_http_service.go b/service/gateway/gateway_http_service.go index 0ad2635..9df0638 100644 --- a/service/gateway/gateway_http_service.go +++ b/service/gateway/gateway_http_service.go @@ -257,7 +257,9 @@ func DeductBalance(ctx context.Context, tenantId uint64, amount float64) error { // TenantSurplusResp 租户余额返回 type TenantSurplusResp struct { - Surplus float64 `json:"surplus"` + Tenant struct { + Surplus float64 `json:"surplus"` + } `json:"tenant"` } // GetTenantSurplus 获取租户余额 @@ -278,7 +280,7 @@ func GetTenantSurplus(ctx context.Context, tenantId uint64) (float64, error) { g.Log().Warningf(ctx, "[获取余额] 失败 tenantId=%d err=%v", tenantId, err) return 0, err } - return resp.Surplus, nil + return resp.Tenant.Surplus, nil } //// callback 向回调地址 POST 任务结果(与查询接口 GetTaskRes 出参一致) diff --git a/service/task/worker.go b/service/task/worker.go index 79d7b2e..6146cb7 100644 --- a/service/task/worker.go +++ b/service/task/worker.go @@ -50,7 +50,7 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa w.failTask(ctx, task, startTime, "租户余额不足") return } - g.Log().Infof(ctx, "[handleOne] 当前余额 tenantId=%d surplus=%.2f", task.TenantId, surplus) + g.Log().Infof(ctx, "[handleOne] 当前余额 tenantId=%d surplus=%.2f", model.TenantId, surplus) // ============================================ // 2) 调用模型 @@ -127,7 +127,7 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa task.BillingData[0] = billingResult totalFee := gconv.Float64(billingResult["total_fee"]) if totalFee > 0 { - _ = gateway.DeductBalance(util.AsyncCtx(ctx), task.TenantId, -totalFee) + _ = gateway.DeductBalance(util.AsyncCtx(ctx), model.TenantId, -totalFee) } } } From 260d6547f2cf5af3d9205fe893da99b8e76bbee4 Mon Sep 17 00:00:00 2001 From: qhd <1766646056@qq.com> Date: Thu, 2 Jul 2026 08:58:37 +0800 Subject: [PATCH 2/6] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E7=A7=9F=E6=88=B7?= =?UTF-8?q?=E4=BD=99=E9=A2=9D=E4=B8=8D=E8=B6=B3=E5=88=A4=E6=96=AD=E9=98=88?= =?UTF-8?q?=E5=80=BC=E4=B8=BA100?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/task/worker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/task/worker.go b/service/task/worker.go index 6146cb7..5f68996 100644 --- a/service/task/worker.go +++ b/service/task/worker.go @@ -46,7 +46,7 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa // 1) 查询余额 // ============================================ surplus, _ = gateway.GetTenantSurplus(ctx, model.TenantId) - if surplus <= 0 { + if surplus <= 100 { w.failTask(ctx, task, startTime, "租户余额不足") return } From 518f666ac67d936427b3b258e77482906a947825 Mon Sep 17 00:00:00 2001 From: qhd <1766646056@qq.com> Date: Thu, 2 Jul 2026 11:20:11 +0800 Subject: [PATCH 3/6] =?UTF-8?q?fix:=20=E6=8F=90=E9=AB=98=E7=A7=9F=E6=88=B7?= =?UTF-8?q?=E4=BD=99=E9=A2=9D=E4=B8=8D=E8=B6=B3=E7=9A=84=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E9=98=88=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/task/worker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/task/worker.go b/service/task/worker.go index 5f68996..c8e131d 100644 --- a/service/task/worker.go +++ b/service/task/worker.go @@ -46,7 +46,7 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa // 1) 查询余额 // ============================================ surplus, _ = gateway.GetTenantSurplus(ctx, model.TenantId) - if surplus <= 100 { + if surplus <= 200 { w.failTask(ctx, task, startTime, "租户余额不足") return } From dc6375707fed98b519e036e869448da6eebc37dd Mon Sep 17 00:00:00 2001 From: qhd <1766646056@qq.com> Date: Sat, 4 Jul 2026 18:28:51 +0800 Subject: [PATCH 4/6] =?UTF-8?q?chore:=20=E6=B7=BB=E5=8A=A0=E8=AE=A1?= =?UTF-8?q?=E8=B4=B9=E5=A4=84=E7=90=86=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/task/worker.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/service/task/worker.go b/service/task/worker.go index c8e131d..467be4a 100644 --- a/service/task/worker.go +++ b/service/task/worker.go @@ -107,6 +107,7 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa // 计费处理 if len(model.BillingConfig) > 0 && len(task.BillingData) > 0 { + fmt.Printf("[handleOne] 计费处理 BillingConfig=%s, BillingData=%s", model.BillingConfig, task.BillingData) // 取请求阶段数据作为基础 billingInput := make(map[string]any) for k, v := range task.BillingData[0] { @@ -122,10 +123,11 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa if billingResult != nil { task.BillingData[0] = billingResult } - + fmt.Printf("[handleOne] 计费处理 BillingResult=%s", billingResult) if billingResult != nil { task.BillingData[0] = billingResult totalFee := gconv.Float64(billingResult["total_fee"]) + fmt.Printf("[handleOne] 计费处理 totalFee=%f", totalFee) if totalFee > 0 { _ = gateway.DeductBalance(util.AsyncCtx(ctx), model.TenantId, -totalFee) } @@ -324,6 +326,8 @@ func (w *asyncWorker) parseAndRetry(ctx context.Context, body map[string]any, mo // 计费 if len(model.BillingConfig) > 0 && len(task.BillingData) > 0 { + fmt.Println("task.BillingData", task.BillingData) + fmt.Println("task.BillingData", task.BillingData) requestData := task.BillingData[0] retryData := make(map[string]any) for k, v := range requestData { @@ -334,9 +338,11 @@ func (w *asyncWorker) parseAndRetry(ctx context.Context, body map[string]any, mo retryData[k] = v } billingResult := util.CalculateBilling(model.BillingConfig, retryData) + fmt.Println("billingResult", billingResult) if billingResult != nil { task.BillingData = append(task.BillingData, billingResult) totalFee := gconv.Float64(billingResult["total_fee"]) + fmt.Println("totalFee", totalFee) if totalFee > 0 { _ = gateway.DeductBalance(util.AsyncCtx(ctx), task.TenantId, -totalFee) } From fe64d178ada25ceb6bd9fc8b7bec3eecb36ee3ad Mon Sep 17 00:00:00 2001 From: qhd <1766646056@qq.com> Date: Sat, 4 Jul 2026 18:55:47 +0800 Subject: [PATCH 5/6] =?UTF-8?q?chore:=20=E6=B7=BB=E5=8A=A0=E8=AE=A1?= =?UTF-8?q?=E8=B4=B9=E5=A4=84=E7=90=86=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/util/billing.go | 8 ++++++-- service/task/worker.go | 11 ++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/common/util/billing.go b/common/util/billing.go index 1e8eb71..418893b 100644 --- a/common/util/billing.go +++ b/common/util/billing.go @@ -30,10 +30,12 @@ func CalculateBilling(config map[string]any, billingData map[string]any) map[str func calculateInferenceTierBilling(config map[string]any, data map[string]any) map[string]any { promptTokens := gconv.Int64(data["prompt_tokens"]) + fmt.Printf("calculateInferenceTierBilling promptTokens=%v\n", promptTokens) completionTokens := gconv.Int64(data["completion_tokens"]) + fmt.Printf("calculateInferenceTierBilling completionTokens=%v\n", completionTokens) hasAudio := gconv.Bool(data["has_audio"]) inputK := promptTokens / 1000 - + fmt.Printf("calculateInferenceTierBilling inputK=%v\n", inputK) tiers := config["pricing"].(map[string]any)["tiers"].([]any) var matched map[string]any for _, t := range tiers { @@ -53,11 +55,13 @@ func calculateInferenceTierBilling(config map[string]any, data map[string]any) m } else { inputPrice = gconv.Float64(matched["input_price"]) } + fmt.Printf("calculateInferenceTierBilling inputPrice=%v\n", inputPrice) outputPrice := gconv.Float64(matched["output_price"]) - + fmt.Printf("calculateInferenceTierBilling outputPrice=%v\n", outputPrice) inputCost := float64(promptTokens) * inputPrice / 1000000 outputCost := float64(completionTokens) * outputPrice / 1000000 + fmt.Printf("calculateBilling inputCost=%v, outputCost=%v\n", inputCost, outputCost) // 推理模型 return map[string]any{ "model_name": data["model_name"], diff --git a/service/task/worker.go b/service/task/worker.go index 467be4a..0ad44d2 100644 --- a/service/task/worker.go +++ b/service/task/worker.go @@ -107,7 +107,6 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa // 计费处理 if len(model.BillingConfig) > 0 && len(task.BillingData) > 0 { - fmt.Printf("[handleOne] 计费处理 BillingConfig=%s, BillingData=%s", model.BillingConfig, task.BillingData) // 取请求阶段数据作为基础 billingInput := make(map[string]any) for k, v := range task.BillingData[0] { @@ -123,12 +122,12 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa if billingResult != nil { task.BillingData[0] = billingResult } - fmt.Printf("[handleOne] 计费处理 BillingResult=%s", billingResult) if billingResult != nil { task.BillingData[0] = billingResult totalFee := gconv.Float64(billingResult["total_fee"]) - fmt.Printf("[handleOne] 计费处理 totalFee=%f", totalFee) + fmt.Printf("[handleOne] 计费处理 totalFee1=%f", totalFee) if totalFee > 0 { + fmt.Printf("[handleOne] 计费处理 totalFee2=%f", totalFee) _ = gateway.DeductBalance(util.AsyncCtx(ctx), model.TenantId, -totalFee) } } @@ -326,8 +325,6 @@ func (w *asyncWorker) parseAndRetry(ctx context.Context, body map[string]any, mo // 计费 if len(model.BillingConfig) > 0 && len(task.BillingData) > 0 { - fmt.Println("task.BillingData", task.BillingData) - fmt.Println("task.BillingData", task.BillingData) requestData := task.BillingData[0] retryData := make(map[string]any) for k, v := range requestData { @@ -338,12 +335,12 @@ func (w *asyncWorker) parseAndRetry(ctx context.Context, body map[string]any, mo retryData[k] = v } billingResult := util.CalculateBilling(model.BillingConfig, retryData) - fmt.Println("billingResult", billingResult) if billingResult != nil { task.BillingData = append(task.BillingData, billingResult) totalFee := gconv.Float64(billingResult["total_fee"]) - fmt.Println("totalFee", totalFee) + fmt.Println("totalFee==================", totalFee) if totalFee > 0 { + fmt.Println("deduct balance================", totalFee) _ = gateway.DeductBalance(util.AsyncCtx(ctx), task.TenantId, -totalFee) } } From b9dff27e33a7570341d6d90d2a0e6c62884b4239 Mon Sep 17 00:00:00 2001 From: qhd <1766646056@qq.com> Date: Sat, 4 Jul 2026 20:05:52 +0800 Subject: [PATCH 6/6] =?UTF-8?q?chore:=20=E7=A7=BB=E9=99=A4=E8=AE=A1?= =?UTF-8?q?=E8=B4=B9=E7=9B=B8=E5=85=B3=E8=B0=83=E8=AF=95=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/util/billing.go | 6 ------ service/task/worker.go | 4 ---- 2 files changed, 10 deletions(-) diff --git a/common/util/billing.go b/common/util/billing.go index 418893b..2ed6881 100644 --- a/common/util/billing.go +++ b/common/util/billing.go @@ -30,12 +30,9 @@ func CalculateBilling(config map[string]any, billingData map[string]any) map[str func calculateInferenceTierBilling(config map[string]any, data map[string]any) map[string]any { promptTokens := gconv.Int64(data["prompt_tokens"]) - fmt.Printf("calculateInferenceTierBilling promptTokens=%v\n", promptTokens) completionTokens := gconv.Int64(data["completion_tokens"]) - fmt.Printf("calculateInferenceTierBilling completionTokens=%v\n", completionTokens) hasAudio := gconv.Bool(data["has_audio"]) inputK := promptTokens / 1000 - fmt.Printf("calculateInferenceTierBilling inputK=%v\n", inputK) tiers := config["pricing"].(map[string]any)["tiers"].([]any) var matched map[string]any for _, t := range tiers { @@ -55,13 +52,10 @@ func calculateInferenceTierBilling(config map[string]any, data map[string]any) m } else { inputPrice = gconv.Float64(matched["input_price"]) } - fmt.Printf("calculateInferenceTierBilling inputPrice=%v\n", inputPrice) outputPrice := gconv.Float64(matched["output_price"]) - fmt.Printf("calculateInferenceTierBilling outputPrice=%v\n", outputPrice) inputCost := float64(promptTokens) * inputPrice / 1000000 outputCost := float64(completionTokens) * outputPrice / 1000000 - fmt.Printf("calculateBilling inputCost=%v, outputCost=%v\n", inputCost, outputCost) // 推理模型 return map[string]any{ "model_name": data["model_name"], diff --git a/service/task/worker.go b/service/task/worker.go index 0ad44d2..5f466c8 100644 --- a/service/task/worker.go +++ b/service/task/worker.go @@ -125,9 +125,7 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa if billingResult != nil { task.BillingData[0] = billingResult totalFee := gconv.Float64(billingResult["total_fee"]) - fmt.Printf("[handleOne] 计费处理 totalFee1=%f", totalFee) if totalFee > 0 { - fmt.Printf("[handleOne] 计费处理 totalFee2=%f", totalFee) _ = gateway.DeductBalance(util.AsyncCtx(ctx), model.TenantId, -totalFee) } } @@ -338,9 +336,7 @@ func (w *asyncWorker) parseAndRetry(ctx context.Context, body map[string]any, mo if billingResult != nil { task.BillingData = append(task.BillingData, billingResult) totalFee := gconv.Float64(billingResult["total_fee"]) - fmt.Println("totalFee==================", totalFee) if totalFee > 0 { - fmt.Println("deduct balance================", totalFee) _ = gateway.DeductBalance(util.AsyncCtx(ctx), task.TenantId, -totalFee) } }