add basic leader election on top of postgres
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-01-06 23:19:03 +01:00
parent 43685291b6
commit f2ef496b82
10 changed files with 114 additions and 7 deletions

View File

@@ -2,18 +2,24 @@ package scheduler
import (
"context"
"errors"
"fmt"
"log/slog"
"math/rand"
"time"
"github.com/jackc/pgx/v5"
)
type Scheduler struct {
logger *slog.Logger
db *pgx.Conn
}
func NewScheduler(logger *slog.Logger) *Scheduler {
func NewScheduler(logger *slog.Logger, db *pgx.Conn) *Scheduler {
return &Scheduler{
logger: logger,
db: db,
}
}
@@ -48,8 +54,21 @@ func (s *Scheduler) acquireLeader(ctx context.Context) (bool, error) {
return false, nil
default:
// Attempt to acquire leader
//
var acquiredLock bool
if err := s.db.QueryRow(ctx, "SELECT pg_try_advisory_lock(1234)").Scan(&acquiredLock); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return false, nil
}
}
if !acquiredLock {
wait := time.Second * time.Duration(rand.Float32()*9+1)
s.logger.Debug("failed to acquire lock, parking non-elected scheduler", "wait_seconds", wait)
time.Sleep(wait)
continue
}
return true, nil
}