Skip to content

Conversation

@warning128
Copy link

@warning128 warning128 commented Oct 14, 2025

修复当使用redis 作为 session_conn时,发送事件Queue与处理事件的conn不一致的问题
Fixes #267

Summary by Sourcery

Reuse existing Redis session connections in RedisStore.Get and renew TTL for sessions and ID sets to fix inconsistent session connections when using Redis for sessions.

Bug Fixes:

  • Reuse existing Redis session connections when available to prevent mismatched event queue and processing connections.

Enhancements:

  • Renew TTL for session data and the session ID set when reusing a connection to keep sessions active.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 14, 2025

Reviewer's Guide

Adds a pre-fetch check in RedisStore.Get to reuse existing connections from the active connections map and renew their TTLs, ensuring consistent session usage without recreating connections each time.

Sequence diagram for RedisStore.Get connection reuse and TTL renewal

sequenceDiagram
participant "Caller"
participant "RedisStore"
participant "RedisClient"
participant "Logger"

"Caller"->>"RedisStore": Get(ctx, id)
activate "RedisStore"
"RedisStore"->>"RedisStore": Check connections[id]
alt Connection exists
    "RedisStore"->>"RedisClient": Expire(key, ttl)
    "RedisClient"-->>"RedisStore": (result)
    alt Expire error
        "RedisStore"->>"Logger": Warn("failed to renew session TTL")
    end
    "RedisStore"->>"RedisClient": Expire(prefix+"ids", ttl)
    "RedisClient"-->>"RedisStore": (result)
    alt Expire error
        "RedisStore"->>"Logger": Warn("failed to renew session ID set TTL")
    end
    "RedisStore"-->>"Caller": conn
else Connection does not exist
    "RedisStore"->>"RedisClient": Get(key)
    "RedisClient"-->>"RedisStore": data
    ... (rest of original logic)
end
Loading

File-Level Changes

Change Details Files
Reuse existing Redis session connections
  • Acquire read lock on connections map
  • Check for existing connection by session ID
  • Renew TTL for session key and session ID set
  • Return existing connection early
  • Release read lock after the check
internal/mcp/session/redis.go

Assessment against linked issues

Issue Objective Addressed Explanation
#267 Fix the bug where, with SESSION_STORAGE_TYPE=redis, redis_conn does not receive Queue messages after establishing a long HTTP_GET connection.
#267 Ensure that when tools/call fails, the long connection in redis_conn receives error_message events, matching the behavior of memory session storage.
#267 Prevent creation of a new redis connection for each session if one already exists, so that event sending and handling use the same connection.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • Consider using defer to release the read lock right after acquiring it to simplify control flow and prevent accidental double-unlock.
  • Extract the TTL renewal calls into a helper method to reduce duplication and centralize session expiration logic.
  • Ensure expired connections are removed from s.connections elsewhere to prevent stale entries and potential memory leaks.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider using defer to release the read lock right after acquiring it to simplify control flow and prevent accidental double-unlock.
- Extract the TTL renewal calls into a helper method to reduce duplication and centralize session expiration logic.
- Ensure expired connections are removed from s.connections elsewhere to prevent stale entries and potential memory leaks.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@warning128 warning128 changed the title fix: When create redis_conn,check if connection already exists [cttq] fix: When create redis_conn,check if connection already exists Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] 在配置SESSION_STORAGE_TYPE=redis时, 在HTTP_GET建立长连接后, redis_conn收不到Queue消息

1 participant