fix(auth): return 503 when logout cache deletion fails

This commit is contained in:
kk10
2026-11-06 16:00:11 +03:00
parent bca081428a
commit 8a2d895274
3 changed files with 17 additions and 9 deletions
+6 -3
View File
@@ -64,11 +64,14 @@ func (h *AuthHandler) RevokeSession(w http.ResponseWriter, r *http.Request) {
}
err := h.service.Logout(r.Context(), token)
if errors.Is(err, service.ErrUnauthorized) {
switch {
case errors.Is(err, service.ErrSessionCacheUnavailable):
httpx.WriteError(w, http.StatusServiceUnavailable, dto.ErrorCodeUnavailable, "session cache unavailable", requestID)
return
case errors.Is(err, service.ErrUnauthorized):
httpx.WriteUnauthorized(w, httpx.BearerChallenge, requestID)
return
}
if err != nil {
case err != nil:
httpx.WriteInternalError(w, requestID, err)
return
}
+10 -5
View File
@@ -19,8 +19,9 @@ const sessionCacheTTL = 60 * time.Second
const cacheOpTimeout = 300 * time.Millisecond
var (
ErrInvalidCredentials = errors.New("invalid credentials")
ErrUnauthorized = errors.New("unauthorized")
ErrInvalidCredentials = errors.New("invalid credentials")
ErrUnauthorized = errors.New("unauthorized")
ErrSessionCacheUnavailable = errors.New("session cache unavailable")
)
type authQueries interface {
@@ -118,7 +119,9 @@ func (s *AuthService) Logout(ctx context.Context, token string) error {
return err
}
s.cacheDel(ctx, cacheKey(tokenHash))
if err := s.cacheDel(ctx, cacheKey(tokenHash)); err != nil {
return ErrSessionCacheUnavailable
}
if n == 0 {
return ErrUnauthorized
@@ -169,9 +172,9 @@ func (s *AuthService) cacheSet(ctx context.Context, key string, id uuid.UUID, tt
}
}
func (s *AuthService) cacheDel(ctx context.Context, key string) {
func (s *AuthService) cacheDel(ctx context.Context, key string) error {
if s.cache == nil {
return
return nil
}
cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout)
@@ -179,5 +182,7 @@ func (s *AuthService) cacheDel(ctx context.Context, key string) {
if err := s.cache.Del(cctx, key).Err(); err != nil {
s.log.Warn("session cache delete failed", "error", err)
return err
}
return nil
}