lab4: S3-вложения, presigned upload и проверки
This commit is contained in:
@@ -17,10 +17,11 @@ type Store struct {
|
||||
queryTimeout time.Duration
|
||||
idempotencyTTL time.Duration
|
||||
|
||||
users *UserRepository
|
||||
chats *ChatRepository
|
||||
messages *MessageRepository
|
||||
sessions *SessionRepository
|
||||
users *UserRepository
|
||||
chats *ChatRepository
|
||||
messages *MessageRepository
|
||||
sessions *SessionRepository
|
||||
attachments *AttachmentRepository
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg config.Postgres, idempotencyTTL time.Duration) (*Store, error) {
|
||||
@@ -65,6 +66,7 @@ func New(ctx context.Context, cfg config.Postgres, idempotencyTTL time.Duration)
|
||||
store.chats = &ChatRepository{store: store}
|
||||
store.messages = &MessageRepository{store: store}
|
||||
store.sessions = &SessionRepository{store: store}
|
||||
store.attachments = &AttachmentRepository{store: store}
|
||||
|
||||
return store, nil
|
||||
}
|
||||
@@ -77,6 +79,8 @@ func (s *Store) Messages() *MessageRepository { return s.messages }
|
||||
|
||||
func (s *Store) Sessions() *SessionRepository { return s.sessions }
|
||||
|
||||
func (s *Store) Attachments() *AttachmentRepository { return s.attachments }
|
||||
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
if err := s.pool.Ping(ctx); err != nil {
|
||||
return fmt.Errorf("ping database: %w", err)
|
||||
@@ -87,6 +91,43 @@ func (s *Store) Ping(ctx context.Context) error {
|
||||
|
||||
func (s *Store) Close() { s.pool.Close() }
|
||||
|
||||
// sweepLockKey is a process-independent constant: every replica contends for the
|
||||
// same PostgreSQL session lock, so only one sweeper runs at a time.
|
||||
const sweepLockKey int64 = 0x4c414234 // "LAB4"
|
||||
|
||||
// AcquireSweepLock holds a session-level advisory lock on a dedicated
|
||||
// connection. The returned release function must be called on that same
|
||||
// connection before it is returned to the pool.
|
||||
func (s *Store) AcquireSweepLock(ctx context.Context) (func(), bool, error) {
|
||||
conn, err := s.pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("acquire sweep connection: %w", err)
|
||||
}
|
||||
|
||||
var acquired bool
|
||||
if err := conn.QueryRow(ctx, "SELECT pg_try_advisory_lock($1)", sweepLockKey).Scan(&acquired); err != nil {
|
||||
conn.Release()
|
||||
|
||||
return nil, false, fmt.Errorf("acquire sweep lock: %w", err)
|
||||
}
|
||||
|
||||
if !acquired {
|
||||
conn.Release()
|
||||
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
release := func() {
|
||||
unlockCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, _ = conn.Exec(unlockCtx, "SELECT pg_advisory_unlock($1)", sweepLockKey)
|
||||
conn.Release()
|
||||
}
|
||||
|
||||
return release, true, nil
|
||||
}
|
||||
|
||||
func (s *Store) timeout(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
if s.queryTimeout <= 0 {
|
||||
return context.WithCancel(ctx)
|
||||
|
||||
Reference in New Issue
Block a user