Go: fix context (#18061)

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-08-10 20:49:32 +08:00
committed by GitHub
parent a0438517bc
commit 38c5d7c338
9 changed files with 39 additions and 30 deletions

View File

@@ -39,12 +39,14 @@ import (
"errors"
"fmt"
"net"
"ragflow/internal/common"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"go.uber.org/zap"
rediscli "ragflow/internal/engine/redis"
)
@@ -107,6 +109,7 @@ func validateWebhookSecurity(
c *gin.Context,
canvasID string,
) error {
ctx := c.Request.Context()
if len(securityCfg) == 0 {
return errWebhookFailClosed
}
@@ -116,7 +119,7 @@ func validateWebhookSecurity(
if err := validateIPWhitelist(c, securityCfg); err != nil {
return err
}
if err := validateRateLimit(canvasID, securityCfg); err != nil {
if err := validateRateLimit(ctx, canvasID, securityCfg); err != nil {
return err
}
return validateAuth(c, securityCfg)
@@ -242,7 +245,7 @@ func validateIPWhitelist(c *gin.Context, cfg map[string]any) error {
//
// Strict fail-closed: any Redis error → error. The webhook handler
// surfaces this as 102 so an operator notices a misconfiguration.
func validateRateLimit(canvasID string, cfg map[string]any) error {
func validateRateLimit(ctx context.Context, canvasID string, cfg map[string]any) error {
rawRL, ok := cfg["rate_limit"].(map[string]any)
if !ok || len(rawRL) == 0 {
return nil
@@ -277,15 +280,20 @@ func validateRateLimit(canvasID string, cfg map[string]any) error {
}
key := fmt.Sprintf("rl:tb:%s", canvasID)
ctx, cancel := context.WithTimeout(context.Background(), webhookRateLimitTimeout)
newCtx, cancel := context.WithTimeout(ctx, webhookRateLimitTimeout)
defer cancel()
rdb := rediscli.Get()
if rdb == nil {
return fmt.Errorf("rate limit error: redis not initialised")
}
allowed, err := rdb.EvalTokenBucketStrict(ctx, key, limitF, limitF/window)
allowed, err := rdb.EvalTokenBucketStrict(newCtx, key, limitF, limitF/window)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
common.Warn("rate limit check ambiguous (timeout/cancel), allowing",
zap.String("canvas_id", canvasID), zap.Error(err))
return nil
}
return fmt.Errorf("rate limit error: %s", err.Error())
}
if !allowed {