# Changelog

All notable changes to liteq will be documented in this file.

## [1.0.1] - 2026-03-30

### Added

#### Redis Connection Management
- **Configurable Timeouts** - Added `connection_timeout_secs` and `response_timeout_secs` to `RedisConfig`
  - Default: 30s connection timeout, 20s response timeout
  - Configurable via `.with_connection_timeout()` and `.with_response_timeout()`
  - Optimized for cloud Redis (Aiven, Upstash, AWS ElastiCache)

#### ConnectionManager Integration
- All Redis operations now use `ConnectionManager` with proper timeout configuration
- Applied to:
  - Queue operations (enqueue, dequeue, get_job_counts, etc.)
  - PubSub publish operations
  - Consumer registration and heartbeat
  - Connection supervision

#### Persistent Retry Logic
- Connection supervisor now implements **infinite retry with exponential backoff**
- Never gives up trying to reconnect to Redis
- Smart backoff: 1s → 2s → 4s → ... → 60s max after 3 consecutive failures
- Continues retrying every 5 seconds with backoff applied

#### PubSub Auto-Reconnect
- **NEW**: PubSub consumers now automatically reconnect after connection loss
- Automatic re-subscription to all channels after reconnect
- 5-second delay between reconnect attempts
- Continues trying until reconnection succeeds
- Spawns background task for non-blocking operation

#### Test Examples
- **NEW**: `test_pubsub_reconnect.rs` - Demonstrates PubSub auto-reconnect feature

### Changed

#### Connection Supervisor Behavior
- **Before**: Single reconnection attempt (20 retries), then give up
- **After**: Infinite retry with exponential backoff (1s → 60s max)
- Improved logging shows reconnect attempts and backoff duration
- Better resilience for long-term Redis outages

#### PubSub Subscribe
- **Before**: Stream ends on connection loss, consumer stops receiving
- **After**: Background task auto-reconnects and re-subscribes
- `subscribe()` returns immediately, runs in background
- Callback wrapped in `Arc` for thread-safe sharing across reconnects

### Fixed

#### Connection Timeout Issues
- Fixed "timed out" errors with cloud-hosted Redis (Aiven, Upstash)
- ConnectionManager now properly configured with:
  - 30s connection timeout (was unlimited)
  - 20s response timeout (was unlimited)
  - 20 retry attempts with exponential backoff (1s → 60s)
- Resolves issues with high-latency cloud Redis connections

#### PubSub After Reconnect
- Fixed consumers not receiving messages after Redis restart
- Stream ending now triggers auto-reconnect instead of stopping
- All channels re-subscribed automatically after reconnect

#### Code Quality
- Applied all Clippy fixes (unnecessary casts, needless borrows, etc.)
- Removed unused code where appropriate
- All public APIs maintained (no breaking changes)

### Technical Details

#### ConnectionManager Configuration
```rust
let manager_config = ConnectionManagerConfig::new()
    .set_connection_timeout(Some(Duration::from_secs(30)))
    .set_response_timeout(Some(Duration::from_secs(20)))
    .set_number_of_retries(20)
    .set_min_delay(Duration::from_secs(1))
    .set_max_delay(Duration::from_secs(60));
```

#### PubSub Reconnect Loop
```rust
loop {
    match subscribe_and_listen(...).await {
        Err(e) => {
            warn!("Connection lost: {}. Reconnecting in 5s...", e);
            tokio::time::sleep(Duration::from_secs(5)).await;
        }
        Ok(_) => break,
    }
}
```

### Migration Guide

No breaking changes! Existing code will continue to work.

**Optional: Customize timeouts for your environment**

For cloud Redis:
```rust
RedisConfig::new("rediss://your-redis.cloud.com:6379")
    .with_connection_timeout(30)
    .with_response_timeout(20);
```

For local Redis:
```rust
RedisConfig::new("redis://127.0.0.1:6379")
    .with_connection_timeout(5)
    .with_response_timeout(3);
```

### Testing

Test Redis resilience:
```bash
# Terminal 1: Run consumer
cargo run --example queue_consumer

# Terminal 2: Test reconnect
redis-cli shutdown                    # Stop Redis
# Watch logs: "Reconnection successful after X failures"
redis-server                           # Start Redis
# Consumer resumes automatically ✅
```

Test PubSub resilience:
```bash
# Terminal 1: Run PubSub consumer
cargo run --example test_pubsub_reconnect

# Terminal 2: Test reconnect
redis-cli PUBLISH lite-job:test_channel '{"text":"Hello"}'  # ✅ Works
redis-cli shutdown                                                  # Stop Redis
redis-server                                                         # Start Redis
redis-cli PUBLISH lite-job:test_channel '{"text":"Still!"}'      # ✅ Still works!
```

### Tested Platforms

✅ **Production-tested** with multiple Redis providers:

| Platform | Version | Type | Test Result |
|----------|---------|------|-------------|
| **Self-Hosted Redis** | 7.x | Local/Docker | ✅ All features working |
| **Upstash** | Redis Cloud | Serverless | ✅ Recommended for serverless |
| **Aiven Valkey** | 7.x | Managed Cloud | ✅ TLS + timeouts working |

**Test Coverage:**
- ✅ Connection/Reconnection with exponential backoff
- ✅ PubSub auto-reconnect with re-subscription
- ✅ Multi-consumer fair distribution
- ✅ ETA scheduling with ZSET optimization
- ✅ Heartbeat and consumer registration
- ✅ All timeout configurations (5s-60s range)

**Provider-Specific Notes:**

**Upstash:**
- Works with default timeouts (30s/20s)
- TLS connection: `rediss://` protocol
- Recommended for serverless/edge deployments

**Aiven Valkey:**
- Valkey is Redis-compatible
- Requires TLS: `rediss://` protocol
- Default timeouts optimal for cloud latency
- Connection string format: `rediss://user:pass@host:port`

---

## [1.0.0] - 2026-03-28

### Initial Release

#### Features
- Multi-consumer fair distribution with auto-registration
- ZSET optimization for scheduled jobs (50% reduction in roundtrips)
- Connection supervision with RabbitMQ-style retry
- Dependency injection support
- Health checks and monitoring
- Structured logging with tracing
- Builder pattern API

#### Documentation
- README.md
- MULTI_CONSUMER_FLOW.md
- PERFORMANCE_IMPROVEMENTS.md
- MONITORING.md
- CONNECTION_SUPERVISION.md
- IMPLEMENTATION_COMPLETE.md

