Problem

Warning

Silent failure — If the request context is cancelled (timeout, panic), Del returns immediately with no error — the lock is never released, the queue backs up, consumers stall.

// ❌ Uses request context — can be cancelled mid-cleanup
func (r *RedisCache) Unlock(ctx context.Context, key string) error {
    err := r.cache.Del(ctx, key).Err()
    ...
}

Fix

Cleanup operations that must succeed should use context.Background() — never cancelled, no deadline, lives for the entire process lifetime.

// ✅
func (r *RedisCache) Unlock(ctx context.Context, key string) error {
    cleanupCtx := context.Background()
    err := r.cache.Del(cleanupCtx, key).Err()
    ...
}

If you want a safety timeout on the cleanup itself (not the parent request):

cleanupCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := r.cache.Del(cleanupCtx, key).Err()

Tip

Rule of thumb — Request context → business logic. context.Background() → cleanup, teardown, deferred unlocks.

See also