forked from mtusi2026/mtusi2026lab4
feat: lab4 - S3 attachments, image validation, failover
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
// Package store is the only layer that talks to PostgreSQL.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
const (
|
||||
maxOpenConns = 20
|
||||
maxIdleConns = 10
|
||||
connMaxLifetime = 5 * time.Minute
|
||||
pingTimeout = 2 * time.Second
|
||||
)
|
||||
|
||||
// Store wraps the shared PostgreSQL connection pool.
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// Open creates the pool; it does not require the database to be reachable yet.
|
||||
func Open(databaseURL string) (*Store, error) {
|
||||
db, err := sql.Open("postgres", databaseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.SetMaxOpenConns(maxOpenConns)
|
||||
db.SetMaxIdleConns(maxIdleConns)
|
||||
db.SetConnMaxLifetime(connMaxLifetime)
|
||||
return &Store{db: db}, nil
|
||||
}
|
||||
|
||||
// Close releases the pool.
|
||||
func (s *Store) Close() error {
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
// Ping reports whether the database answers within a short budget; readiness uses it.
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, pingTimeout)
|
||||
defer cancel()
|
||||
return s.db.PingContext(ctx)
|
||||
}
|
||||
|
||||
// scanTime normalises timestamps returned by the driver.
|
||||
func utc(t time.Time) time.Time {
|
||||
return t.UTC()
|
||||
}
|
||||
Reference in New Issue
Block a user