lock

package
v0.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultHeartbeat = DefaultLockTtl / 3

The defaulth heartbeat interval is 20 sec

View Source
const DefaultLockTtl = 60 * time.Second

The default TTL is 60 sec

View Source
const DefaultLockedAtField = "locked_at_ts"
View Source
const DefaultNameField = "name"
View Source
const DefaultOwnerField = "owner"
View Source
const DefaultTableName = "barn_lock"

Variables

This section is empty.

Functions

This section is empty.

Types

type LeaderElectionHandler

type LeaderElectionHandler func(leader bool) error

type LeaderElector

type LeaderElector struct {
	// contains filtered or unexported fields
}

func NewLeaderElector

func NewLeaderElector(config *LeaderElectorConfig) *LeaderElector

func (*LeaderElector) Start

func (l *LeaderElector) Start()

func (*LeaderElector) StartContext

func (l *LeaderElector) StartContext(ctx context.Context)

func (*LeaderElector) Stop

func (l *LeaderElector) Stop()

type LeaderElectorConfig

type LeaderElectorConfig struct {
	Log     *slog.Logger
	Lock    *Locker
	Handler LeaderElectionHandler
}

type Lock

type Lock struct {
	Name     string
	LockedAt *time.Time
	Owner    *string
}

func (Lock) LogValue

func (l Lock) LogValue() slog.Value

type LockQueryConfig

type LockQueryConfig struct {
	TableName     string
	NameField     string
	LockedAtField string
	OwnerField    string
}

type LockRepository

type LockRepository interface {
	FindOne(tx *sql.Tx, name string) (*Lock, error)
	Create(tx *sql.Tx, name string) error
	Save(tx *sql.Tx, lock *Lock) error
}

func NewPostgresLockRepository

func NewPostgresLockRepository(config ...LockQueryConfig) LockRepository

type Locker

type Locker struct {
	// contains filtered or unexported fields
}

Locker is the mutex entry in the database.

func NewDefaultLock

func NewDefaultLock(db *sql.DB) *Locker

func NewLockWithConfig

func NewLockWithConfig(db *sql.DB, config *LockerConfig) *Locker

NewLockWithConfig create new Lock with config, during initialization config is adjusted

func (*Locker) Confirm

func (l *Locker) Confirm() (bool, error)

Update the acquired lock data

func (*Locker) Hearbeat

func (l *Locker) Hearbeat() time.Duration

func (*Locker) IsLocked

func (l *Locker) IsLocked() bool

func (*Locker) Lock

func (l *Locker) Lock(interval time.Duration) (bool, error)

Acquire the lock

func (*Locker) LockContext

func (l *Locker) LockContext(ctx context.Context, interval time.Duration) (bool, error)

Acquire the lock

func (*Locker) LockName

func (l *Locker) LockName() string

func (*Locker) LockedAt

func (l *Locker) LockedAt() *time.Time

func (*Locker) Name

func (l *Locker) Name() string

func (*Locker) TryLock

func (l *Locker) TryLock() (bool, error)

Try to acquire the lock

func (*Locker) Ttl

func (l *Locker) Ttl() time.Duration

func (*Locker) Unlock

func (l *Locker) Unlock() (bool, error)

Release lock

type LockerConfig

type LockerConfig struct {
	Log        *slog.Logger
	Name       string
	Repository LockRepository
	LockName   string
	Ttl        time.Duration
	Hearbeat   time.Duration
}

type PostgresLockRepository

type PostgresLockRepository struct {
	// contains filtered or unexported fields
}

func (*PostgresLockRepository) Create

func (r *PostgresLockRepository) Create(tx *sql.Tx, name string) error

func (*PostgresLockRepository) CreateTable

func (r *PostgresLockRepository) CreateTable(tx *sql.Tx) error

func (*PostgresLockRepository) DeleteAll added in v0.2.0

func (r *PostgresLockRepository) DeleteAll(tx *sql.Tx) error

func (*PostgresLockRepository) FindOne

func (r *PostgresLockRepository) FindOne(tx *sql.Tx, name string) (*Lock, error)

func (*PostgresLockRepository) Save

func (r *PostgresLockRepository) Save(tx *sql.Tx, lock *Lock) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL