Repository navigation
Move REDIS to engine dir - #16006
Move REDIS to engine dir#16006
Conversation
Signed-off-by: Jin Hai <[email protected]>
📝 WalkthroughWalkthrough
ChangesCache-to-Redis Package Migration
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/handler/agent.go`:
- Line 922: Both locations chain method calls on redis.Get() without nil guards,
violating the established codebase pattern. In internal/handler/agent.go at line
922, store the result of redis.Get() in a variable, check if it is nil, and if
so return the empty-log response as a fallback before calling .Get(key). In
cmd/server_main.go at line 260, guard the redis.Get() call itself (not just the
result of .GetClient()) to ensure that adminRuntimeSelector remains safely nil
if Redis is unavailable at startup.
In `@internal/service/user.go`:
- Around line 1221-1223: The code is calling redis.Get() and immediately
invoking methods like Set() on the result without checking if redis.Get()
returns nil. When Redis initialization is skipped (when the host is unset),
redis.Get() can return nil, causing a panic instead of a controlled error
response. Add a nil check after calling redis.Get() in the captcha storage
operation and all other Redis operations in the forgot-password flow, returning
an appropriate error response (common.CodeServerError with a descriptive error
message) if redis.Get() is nil, similar to how other error conditions are
handled in this function.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: dd1d600c-6be4-4b19-981f-fd674d3c3c60
📒 Files selected for processing (18)
cmd/admin_server.gocmd/ingestor.gocmd/server_main.gointernal/admin/handler.gointernal/admin/service.gointernal/agent/canvas/cancel.gointernal/agent/canvas/checkpoint_store.gointernal/agent/canvas/run_tracker.gointernal/engine/redis/redis.gointernal/handler/agent.gointernal/handler/oauth_login.gointernal/handler/user.gointernal/service/connector.gointernal/service/document.gointernal/service/oauth_login.gointernal/service/system.gointernal/service/tag.gointernal/service/user.go
|
|
||
| key := fmt.Sprintf("%s-%s-logs", canvasID, messageID) | ||
| payload, rerr := cache.Get().Get(key) | ||
| payload, rerr := redis.Get().Get(key) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# Check the exact code at both locations
echo "=== internal/handler/agent.go around line 922 ==="
sed -n '915,930p' internal/handler/agent.go
echo -e "\n=== cmd/server_main.go around line 260 ==="
sed -n '255,265p' cmd/server_main.go
# Find the redis.Get() function definition to understand its signature
echo -e "\n=== Looking for redis.Get() definition ==="
rg -A 10 "func Get\(\)" --type go | head -30Repository: infiniflow/ragflow
Length of output: 2183
🏁 Script executed:
# Get more context around each usage to understand the full picture
echo "=== Broader context for internal/handler/agent.go line 922 ==="
sed -n '910,935p' internal/handler/agent.go
echo -e "\n=== Broader context for cmd/server_main.go line 260 ==="
sed -n '250,270p' cmd/server_main.goRepository: infiniflow/ragflow
Length of output: 2155
🏁 Script executed:
# Search for redis package to understand the implementation
rg "package redis" --type go
rg -B 2 -A 10 "func Get\(\)" --type goRepository: infiniflow/ragflow
Length of output: 1321
🏁 Script executed:
# Check globalClient initialization and type
echo "=== globalClient definition and initialization ==="
rg -B 5 -A 5 "var globalClient" internal/engine/redis/redis.go
# Check how other parts of the code handle redis.Get()
echo -e "\n=== Other redis.Get() usages in codebase ==="
rg "redis\.Get\(\)" --type go -B 2 -A 2 | head -50
# Look for pattern of nil-guarding redis.Get()
echo -e "\n=== Checking if redis.Get() is ever nil-guarded ==="
rg "redis\.Get\(\)" --type go -B 2 -A 2 | grep -A 2 "!= nil\|if.*redis\.Get"Repository: infiniflow/ragflow
Length of output: 5072
🏁 Script executed:
# Check the RedisClient type definition to understand what methods it has
echo "=== RedisClient type definition ==="
rg -B 5 -A 15 "type RedisClient struct" internal/engine/redis/redis.goRepository: infiniflow/ragflow
Length of output: 605
Add nil guard before chaining methods on redis.Get() at both locations.
The established pattern in the codebase (internal/service/tag.go, system.go, connector.go) nil-checks redis.Get() before calling methods on it, but both flagged sites omit this guard:
internal/handler/agent.go#L922: Store result in a variable and check for nil before calling.Get(key); if nil, return the empty-log response as fallback.cmd/server_main.go#L260: Guardredis.Get()itself (not just the result of.GetClient()) soadminRuntimeSelectorremains safely nil if Redis is unavailable at startup.
📍 Affects 2 files
internal/handler/agent.go#L922-L922(this comment)cmd/server_main.go#L260-L260
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/handler/agent.go` at line 922, Both locations chain method calls on
redis.Get() without nil guards, violating the established codebase pattern. In
internal/handler/agent.go at line 922, store the result of redis.Get() in a
variable, check if it is nil, and if so return the empty-log response as a
fallback before calling .Get(key). In cmd/server_main.go at line 260, guard the
redis.Get() call itself (not just the result of .GetClient()) to ensure that
adminRuntimeSelector remains safely nil if Redis is unavailable at startup.
| if ok := redis.Get().Set(utility.CaptchaIDRedisKey(captchaID), text, 60*time.Second); !ok { | ||
| return "", "", common.CodeServerError, fmt.Errorf("failed to store captcha") | ||
| } |
There was a problem hiding this comment.
Guard forgot-password Redis paths when Redis is uninitialized.
Line 1221 calls redis.Get().Set(...) directly, and Lines 1241, 1340, and 1410 use rc := redis.Get() without a nil check before dereference. Since Redis init is skipped when host is unset, these flows can panic instead of returning a controlled error response.
Suggested fix
func (s *UserService) ForgotIssueCaptcha(email string) (captchaID, imageDataURL string, code common.ErrorCode, err error) {
+ rc := redis.Get()
+ if rc == nil {
+ return "", "", common.CodeServerError, fmt.Errorf("redis is not initialized")
+ }
...
- if ok := redis.Get().Set(utility.CaptchaIDRedisKey(captchaID), text, 60*time.Second); !ok {
+ if ok := rc.Set(utility.CaptchaIDRedisKey(captchaID), text, 60*time.Second); !ok {
return "", "", common.CodeServerError, fmt.Errorf("failed to store captcha")
}
}
func (s *UserService) ForgotSendOTP(email, captchaID, captcha string) (common.ErrorCode, error) {
rc := redis.Get()
+ if rc == nil {
+ return common.CodeServerError, fmt.Errorf("redis is not initialized")
+ }
...
}
func (s *UserService) ForgotVerifyOTP(email, otp string) (common.ErrorCode, error) {
rc := redis.Get()
+ if rc == nil {
+ return common.CodeServerError, fmt.Errorf("redis is not initialized")
+ }
...
}
func (s *UserService) ForgotResetPassword(req *ForgotResetPasswordRequest) (*entity.User, common.ErrorCode, error) {
rc := redis.Get()
+ if rc == nil {
+ return nil, common.CodeServerError, fmt.Errorf("redis is not initialized")
+ }
...
}Also applies to: 1241-1244, 1340-1344, 1410-1413
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/service/user.go` around lines 1221 - 1223, The code is calling
redis.Get() and immediately invoking methods like Set() on the result without
checking if redis.Get() returns nil. When Redis initialization is skipped (when
the host is unset), redis.Get() can return nil, causing a panic instead of a
controlled error response. Add a nil check after calling redis.Get() in the
captcha storage operation and all other Redis operations in the forgot-password
flow, returning an appropriate error response (common.CodeServerError with a
descriptive error message) if redis.Get() is nil, similar to how other error
conditions are handled in this function.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16006 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
cmd/server_main.go (1)
261-263:⚠️ Potential issue | 🔴 CriticalGuard
redis.Get()before callingGetClient()to avoid startup panic.Line 261 directly dereferences
redis.Get()without a nil-check. When Redis is not configured (cfg.Hostis empty),redis.Init()successfully skips initialization, leavingredis.Get()returning nil. Calling.GetClient()on nil will panic during bootstrap, preventing the server from starting.Proposed fix
var adminRuntimeSelector *runtime.Selector -if rdb := redis.Get().GetClient(); rdb != nil { - adminRuntimeSelector = runtime.NewSelector(rdb, common.Logger) -} +if rc := redis.Get(); rc != nil { + if rdb := rc.GetClient(); rdb != nil { + adminRuntimeSelector = runtime.NewSelector(rdb, common.Logger) + } +} adminRuntimeHandler := handler.NewAdminRuntimeHandler(adminRuntimeSelector)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@cmd/server_main.go` around lines 261 - 263, The code at line 261 calls redis.Get().GetClient() without verifying that redis.Get() returns a non-nil value. When Redis is not configured, redis.Get() returns nil, which causes a panic when attempting to call GetClient() on it. Add a nil-check for redis.Get() first before calling GetClient() on the result. The condition should verify that redis.Get() is not nil, and only then call GetClient() on that returned client, assigning the result to rdb within the inner block.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@cmd/server_main.go`:
- Around line 261-263: The code at line 261 calls redis.Get().GetClient()
without verifying that redis.Get() returns a non-nil value. When Redis is not
configured, redis.Get() returns nil, which causes a panic when attempting to
call GetClient() on it. Add a nil-check for redis.Get() first before calling
GetClient() on the result. The condition should verify that redis.Get() is not
nil, and only then call GetClient() on that returned client, assigning the
result to rdb within the inner block.
What problem does this PR solve?
as title.
Type of change