Complete lab2 grade 4 implementation and report
Application checks / application (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
Daria0w0
2026-10-10 00:28:43 +03:00
parent 83e3810d33
commit c0ca56f208
24 changed files with 1107 additions and 137 deletions
+56
View File
@@ -0,0 +1,56 @@
package cache
import (
"context"
"encoding/json"
"time"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model"
"github.com/redis/go-redis/v9"
)
const ProfileTTL = 60 * time.Second
type Profiles struct{ client *redis.Client }
func NewProfiles(address string) *Profiles {
return &Profiles{client: redis.NewClient(&redis.Options{
Addr: address,
Protocol: 2,
MaxRetries: -1,
DialTimeout: 200 * time.Millisecond,
ReadTimeout: 200 * time.Millisecond,
WriteTimeout: 200 * time.Millisecond,
PoolTimeout: 200 * time.Millisecond,
ContextTimeoutEnabled: true,
PoolSize: 10,
MaxActiveConns: 10,
MinIdleConns: 0,
})}
}
func (p *Profiles) Close() error { return p.client.Close() }
func (p *Profiles) Get(ctx context.Context, id string) (model.User, bool) {
ctx, cancel := context.WithTimeout(ctx, 250*time.Millisecond)
defer cancel()
raw, err := p.client.Get(ctx, "profile:v1:"+id).Bytes()
if err != nil {
return model.User{}, false
}
var user model.User
if json.Unmarshal(raw, &user) != nil || user.ID != id || user.Username == "" || user.DisplayName == "" || user.CreatedAt.IsZero() {
return model.User{}, false
}
return user, true
}
func (p *Profiles) Set(ctx context.Context, user model.User) {
raw, err := json.Marshal(user)
if err != nil {
return
}
ctx, cancel := context.WithTimeout(ctx, 250*time.Millisecond)
defer cancel()
_ = p.client.Set(ctx, "profile:v1:"+user.ID, raw, ProfileTTL).Err()
}
+1 -1
View File
@@ -140,7 +140,7 @@ func (s *PostgresStore) MessagePage(ctx context.Context, chatID string, limit in
query += ` AND (created_at,id)>($3,$4::uuid)`
args = append(args, after.CreatedAt, after.ID)
}
query += ` ORDER BY created_at,id LIMIT $2`
query += ` ORDER BY messages.created_at,messages.id LIMIT $2`
rows, err := s.pool.Query(ctx, query, args...)
if err != nil {
return nil, ErrUnavailable
+5 -6
View File
@@ -42,18 +42,17 @@ func (s *PostgresStore) CreateSession(ctx context.Context, hash []byte, userID s
func (s *PostgresStore) SessionUser(ctx context.Context, hash []byte) (model.User, error) {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
var user model.User
err := s.pool.QueryRow(ctx, `SELECT u.id::text, u.username, u.display_name, u.created_at
FROM sessions s JOIN users u ON u.id=s.user_id
WHERE s.token_hash=$1 AND s.expires_at > clock_timestamp()`, hash).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt)
var userID string
// Validate the session in the source of truth BEFORE consulting any cache.
err := s.pool.QueryRow(ctx, `SELECT user_id::text FROM sessions
WHERE token_hash=$1 AND expires_at > clock_timestamp()`, hash).Scan(&userID)
if errors.Is(err, pgx.ErrNoRows) {
return model.User{}, ErrSessionNotFound
}
if err != nil {
return model.User{}, ErrUnavailable
}
user.CreatedAt = user.CreatedAt.UTC()
return user, nil
return s.profile(ctx, userID)
}
func (s *PostgresStore) DeleteSession(ctx context.Context, hash []byte) error {
+35 -1
View File
@@ -12,12 +12,46 @@ import (
var ErrUnavailable = errors.New("storage unavailable")
type PostgresStore struct{ pool *pgxpool.Pool }
type ProfileCache interface {
Get(context.Context, string) (model.User, bool)
Set(context.Context, model.User)
}
type PostgresStore struct {
pool *pgxpool.Pool
profiles ProfileCache
}
func NewPostgresStore(pool *pgxpool.Pool) *PostgresStore {
return &PostgresStore{pool: pool}
}
// Configure once before the HTTP server starts.
func (s *PostgresStore) WithProfiles(profiles ProfileCache) *PostgresStore {
s.profiles = profiles
return s
}
func (s *PostgresStore) profile(ctx context.Context, id string) (model.User, error) {
if s.profiles != nil {
if user, ok := s.profiles.Get(ctx, id); ok {
return user, nil
}
}
var user model.User
err := s.pool.QueryRow(ctx, `SELECT id::text, username, display_name, created_at FROM users WHERE id=$1`, id).Scan(
&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt,
)
if err != nil {
return model.User{}, ErrUnavailable
}
user.CreatedAt = user.CreatedAt.UTC()
if s.profiles != nil {
s.profiles.Set(ctx, user)
}
return user, nil
}
func (s *PostgresStore) CreateUser(ctx context.Context, username, displayName, passwordHash string) (model.User, error) {
id, err := newUUID()
if err != nil {