Skip to content

Add Redis client utility and corresponding tests - #3172

Open
Thenujan-Nagaratnam wants to merge 3 commits into
wso2:mainfrom
Thenujan-Nagaratnam:redisclient
Open

Add Redis client utility and corresponding tests#3172
Thenujan-Nagaratnam wants to merge 3 commits into
wso2:mainfrom
Thenujan-Nagaratnam:redisclient

Conversation

@Thenujan-Nagaratnam

Copy link
Copy Markdown
Contributor

This pull request introduces a new shared Redis client utility to the SDK core, ensuring that identical Redis connection configurations reuse a single *redis.Client instance process-wide, which prevents connection pool leaks and excessive Redis connections. It also adds comprehensive tests for this utility and updates dependencies to support the new functionality.

New Redis client sharing utility:

  • Added redisclient package with GetOrCreateRedisClient, which maintains a process-wide registry of shared *redis.Client instances keyed by connection configuration. This ensures connection pools are reused for identical settings and prevents resource leaks. (sdk/core/utils/redisclient/redisclient.go)
  • Passwords are hashed (SHA-256) before being used as part of the connection key to avoid storing secrets in memory. (sdk/core/utils/redisclient/redisclient.go)

Testing:

  • Added unit tests covering client sharing, distinct client creation for different configs, password handling, and correct ping behavior to ensure reliability and correctness of the utility. (sdk/core/utils/redisclient/redisclient_test.go)

Dependency management:

  • Updated go.mod to include new dependencies: go-redis/v9 for Redis support and miniredis/v2 for in-memory Redis testing, along with related indirect dependencies. (sdk/core/go.mod)

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: bc99e546-ee1c-496d-aa99-f89fcc5ec8a4

📥 Commits

Reviewing files that changed from the base of the PR and between d419ca0 and 961f77a.

📒 Files selected for processing (1)
  • sdk/core/utils/redisclient/redisclient_test.go

📝 Walkthrough

Walkthrough

Adds a process-wide Redis client registry in sdk/core. The registry keys clients by connection settings, hashes passwords, reuses clients without re-pinging, bypasses sharing for TLS and credential providers, and validates these behaviors with Redis tests.

Changes

Redis client registry

Layer / File(s) Summary
Client registry and configuration keys
sdk/core/utils/redisclient/redisclient.go
Adds a mutex-protected registry and configuration keys that include Redis settings and SHA-256 password hashes.
Client creation, pinging, and bypass rules
sdk/core/utils/redisclient/redisclient.go
Adds GetOrCreateRedisClient, timeout-bounded initial pings, client reuse, and independent clients for TLS or credential-provider configurations.
Registry behavior tests and Redis dependencies
sdk/core/utils/redisclient/redisclient_test.go, sdk/core/go.mod
Adds Redis dependencies and tests for reuse, configuration separation, password handling, bypass behavior, ping behavior, and lock release.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PolicyCallSite
  participant GetOrCreateRedisClient
  participant RedisClientRegistry
  participant RedisServer
  PolicyCallSite->>GetOrCreateRedisClient: request client with Redis options
  GetOrCreateRedisClient->>RedisClientRegistry: look up configuration key
  alt client is not registered
    GetOrCreateRedisClient->>RedisServer: ping new client with timeout
    RedisServer-->>GetOrCreateRedisClient: ping result
    GetOrCreateRedisClient->>RedisClientRegistry: register shareable client
  else client is registered
    RedisClientRegistry-->>GetOrCreateRedisClient: return shared client
  end
  GetOrCreateRedisClient-->>PolicyCallSite: client, created status, ping error
Loading

Suggested reviewers: krishanx92, pubudu538, malinthaprasan

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the utility, testing, and dependencies but omits most required template sections, including security checks and test environment. Add the required Purpose, Goals, Approach, User stories, Documentation, Automation tests, Security checks, Samples, Related PRs, and Test environment sections.
Docstring Coverage ⚠️ Warning Docstring coverage is 30.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding a Redis client utility and its tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 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 `@sdk/core/utils/redisclient/redisclient_test.go`:
- Around line 123-135: Update TestHashPassword to replace the
identical-expression comparison with an assertion that
hashRedisPassword("secret") equals the known SHA-256 hexadecimal digest for
“secret”; retain the other password behavior checks unchanged.

In `@sdk/core/utils/redisclient/redisclient.go`:
- Around line 79-91: Update the client-creation flow around the redisClients
mutex: register the newly created client while the mutex is held, then release
the lock before creating the timeout context and calling c.Ping(ctx). Preserve
the existing-client fast path and ensure deferred unlock behavior does not keep
the registry locked during network I/O.
- Around line 68-77: Update the redisConnKey construction in
GetOrCreateRedisClient to distinguish clients with different TLSConfig,
Protocol, and credential-provider settings. Add safe, comparable fingerprints
for these options, or bypass registry reuse when an option cannot be safely
fingerprinted; preserve existing password handling and ensure differing
connection behavior cannot share a cached client.
🪄 Autofix

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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 557670c7-3f35-4077-be82-c233c67f2606

📥 Commits

Reviewing files that changed from the base of the PR and between 79549d0 and 88f4e13.

⛔ Files ignored due to path filters (1)
  • sdk/core/go.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • sdk/core/go.mod
  • sdk/core/utils/redisclient/redisclient.go
  • sdk/core/utils/redisclient/redisclient_test.go

Comment thread sdk/core/utils/redisclient/redisclient_test.go
Comment thread sdk/core/utils/redisclient/redisclient.go
Comment thread sdk/core/utils/redisclient/redisclient.go
…tion handling with TLS and credentials provider

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 `@sdk/core/utils/redisclient/redisclient_test.go`:
- Line 187: Update the deferred listener cleanup around ln.Close to explicitly
handle or discard its returned error, and update the slow-call results at the
referenced synchronization-test locations with blank assignments when
intentionally unused. Ensure all affected calls satisfy errcheck without
changing the test behavior.
- Around line 188-213: The test’s time.Sleep does not guarantee the slow Redis
client goroutine has reached Ping before the fast call is measured. Add a
synchronization channel signaled immediately after ln.Accept() succeeds, then
wait for that signal with a bounded test timeout before creating the unrelated
client, preserving the existing slow-client setup and cleanup behavior.
🪄 Autofix

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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: a79bb6e9-39aa-494e-8dd9-268f1d8ae02e

📥 Commits

Reviewing files that changed from the base of the PR and between 88f4e13 and d419ca0.

📒 Files selected for processing (2)
  • sdk/core/utils/redisclient/redisclient.go
  • sdk/core/utils/redisclient/redisclient_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • sdk/core/utils/redisclient/redisclient.go

Comment thread sdk/core/utils/redisclient/redisclient_test.go Outdated
Comment thread sdk/core/utils/redisclient/redisclient_test.go Outdated
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.

1 participant