func (l *PgLeaderLock) BecomeLeader() (bool, error) {
	return l.tryLock()
}
BecomeLeader tries to become a leader by acquiring the lock.
package util
import (
	"context"
	"fmt"
	"strconv"
	"sync"
	"time"
	pgx "github.com/jackc/pgx/v4"
	"github.com/timescale/promscale/pkg/log"
)
const defaultConnectionTimeout = time.Minute
var SharedLeaseFailure = fmt.Errorf("failed to acquire shared lease")
func GetSharedLease(ctx context.Context, conn *pgx.Conn, id int64) error {
	gotten, err := runLockFunction(ctx, conn, "SELECT pg_try_advisory_lock_shared($1)", id)
	if err != nil {
		return fmt.Errorf("Unable to get shared schema lock. Please make sure that no operations requiring exclusive locking are running: %w", err)
	}
	if !gotten {
		return SharedLeaseFailure
	}
	return nil
}
type PgLeaderLock struct {
	PgAdvisoryLock
	obtained        bool
	numLockAttempts int
}
type AfterConnectFunc = func(ctx context.Context, conn *pgx.Conn) error
func NewPgLeaderLock(groupLockID int64, connStr string, afterConnect AfterConnectFunc) (*PgLeaderLock, error) {
	if afterConnect == nil {
		afterConnect = checkConnection
	}
	lock := &PgLeaderLock{
		PgAdvisoryLock{
			connStr:      connStr,
			groupLockID:  groupLockID,
			afterConnect: afterConnect,
		},
		false,
		0,
	}
	_, err := lock.tryLock()
	if err != nil {
		return nil, err
	}
	return lock, nil
}
func (l *PgLeaderLock) ID() string {
	return strconv.FormatInt(int64(l.groupLockID), 10)
}