forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// SaveSession persists an absolute expiry and a token digest in PostgreSQL.
|
||||
func (s *Store) SaveSession(ctx context.Context, hash string, userID uuid.UUID, expiry time.Time) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
_, err := s.pool.Exec(ctx, `INSERT INTO sessions(token_hash,user_id,expires_at) VALUES(@hash,@user,@expiry)`, pgx.StrictNamedArgs{
|
||||
"hash": hash, "user": userID, "expiry": expiry,
|
||||
})
|
||||
return normalize(err)
|
||||
}
|
||||
|
||||
// SessionUser rejects absent, revoked and expired sessions using database time.
|
||||
func (s *Store) SessionUser(ctx context.Context, hash string) (uuid.UUID, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
var id uuid.UUID
|
||||
err := s.pool.QueryRow(ctx, `SELECT user_id FROM sessions WHERE token_hash=@hash AND expires_at>clock_timestamp()`, pgx.StrictNamedArgs{"hash": hash}).Scan(&id)
|
||||
return id, normalize(err)
|
||||
}
|
||||
|
||||
// RevokeSession deletes the authoritative session before logout succeeds.
|
||||
func (s *Store) RevokeSession(ctx context.Context, hash string) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
_, err := s.pool.Exec(ctx, `DELETE FROM sessions WHERE token_hash=@hash`, pgx.StrictNamedArgs{"hash": hash})
|
||||
return normalize(err)
|
||||
}
|
||||
Reference in New Issue
Block a user