Skip to content

Conversation

@samchristenoliphant
Copy link
Contributor

@samchristenoliphant samchristenoliphant commented Jan 25, 2026

Summary

  • Add PrefixedRedis wrapper to support key prefixing in shared Redis instances (e.g., GCP Memorystore)
  • Add Valkey IAM authentication support for GCP Memorystore with AUTH_MODE_IAM
  • When REDIS_KEY_PREFIX is set, all Redis keys are prefixed with the specified value
  • When USE_VALKEY_IAM=true, uses GCP Workload Identity for authentication

Changes

Redis Key Prefix (original)

  • api/src/redis.rs - New PrefixedRedis wrapper with setex, get, del methods
  • keycast/src/main.rs - Read REDIS_KEY_PREFIX env var and pass to both components

Valkey IAM Authentication (new)

  • cluster-hashring/src/valkey_auth.rs - NEW: ValkeyConnectionFactory with GCP IAM token support
  • cluster-hashring/src/coordinator.rs - Add start_with_config() with IAM param, token refresh in heartbeat
  • cluster-hashring/src/registry.rs - Add register_with_factory() and refresh_connection()
  • api/src/redis.rs - Add new_with_factory() for factory-based connection refresh
  • keycast/src/main.rs - Read USE_VALKEY_IAM env var, share factory between coordinator and API

Environment Variables

Variable Default Description
REDIS_URL (required) Redis/Valkey connection URL
REDIS_KEY_PREFIX (none) Optional prefix for all Redis keys
USE_VALKEY_IAM false Enable GCP IAM authentication for Memorystore Valkey

Token Refresh Strategy

  • Tokens last ~3600 seconds
  • Refresh when TTL < 300 seconds (5 min buffer)
  • Heartbeat task (runs every 5s) checks token TTL
  • On refresh: create new connection with fresh token

Test plan

  • Build passes: cargo build
  • Unit tests pass: cargo test in cluster-hashring crate
  • Local test: Run with USE_VALKEY_IAM=false against local Redis
  • GCP test: Deploy to staging with USE_VALKEY_IAM=true, verify:
    • Cluster coordination works
    • OAuth polling works
    • Service runs > 1 hour without auth failures

@chatgpt-codex-connector
Copy link

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

Add PrefixedRedis wrapper to support key prefixing in shared Redis
instances (e.g., GCP Memorystore). When REDIS_KEY_PREFIX is set, all
Redis keys are prefixed with the specified value.

- Create api/src/redis.rs with PrefixedRedis wrapper
- Update cluster-hashring to use start_with_prefix()
- Update OAuth polling to use prefixed keys
- Document REDIS_KEY_PREFIX in CLAUDE.md
- Add ValkeyConnectionFactory with GCP IAM token support
- Tokens cached and auto-refreshed 5 minutes before expiry
- Coordinator heartbeat monitors token TTL for connection refresh
- PrefixedRedis supports factory-based connection refresh
- New USE_VALKEY_IAM env var (default: false)
- Backward compatible with standard Redis when IAM disabled
- Prevent token exposure in logs by not logging URLs with credentials
- Add auto-refresh on auth errors in PrefixedRedis (handles token expiry)
- Preserve URL scheme/path/query when injecting IAM credentials
- Replace expect() panic with proper error handling
- Add retry with exponential backoff for token fetch (3 attempts)
- Deduplicate TOKEN_REFRESH_BUFFER_SECS constant
- Use redis ErrorKind::AuthenticationFailed instead of string matching
- Add jitter to token fetch retry to prevent thundering herd
- Expand is_auth_error() to catch NOAUTH, WRONGPASS, and other auth
  patterns from expired IAM tokens (not just AuthenticationFailed)
- Add warning log when token TTL conversion fails in valkey_auth
- Add unit tests for auth error detection patterns
- Add integration tests for PrefixedRedis (requires local Redis)
@samchristenoliphant samchristenoliphant changed the title feat: add optional REDIS_KEY_PREFIX for multi-app deployments feat: add optional REDIS_KEY_PREFIX for multi-app deployments, enable GCP Memorystore Valkey Jan 26, 2026
- Add Debug impl for ValkeyConnectionFactory (redacts sensitive fields)
- Add Debug impl for PrefixedRedis (redacts connection)
- Document magic values with named constants (retry params)
- Improve doc comments: shorter first sentences, add # Errors sections
- Add Send+Sync compile-time assertions for async types
- Improve auth error detection using e.code() for NOAUTH/WRONGPASS
- Keep string matching as fallback for robustness
- Fix refresh_connection: return () instead of Result since errors are logged
Copy link
Contributor

@dcadenas dcadenas left a comment

Choose a reason for hiding this comment

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

looks good! I added some nit picks on rust guidelines stuff, merge whenever if it looks good to you

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.

3 participants