dblock

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: Apache-2.0 Imports: 16 Imported by: 1

README

DBLock

A simple distributed locking library for Go. Heavily inspired by cirello.io/pglock, but built on top of ent instead for better database compatibility.

Go Version

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/shivanshvij/dblock. For more contribution information check out the contribution guide.

License

This project is available as open source under the terms of the Apache License, Version 2.0.

Code of Conduct

Everyone interacting in this project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the CNCF Code of Conduct.

Documentation

Index

Constants

View Source
const PGQuery = `
INSERT INTO ` + lock.Table + `("` + lock.FieldID + `", "` + lock.FieldVersion + `", "` + lock.FieldOwner + `") 
VALUES($1, $2, $3) 
ON CONFLICT ("` + lock.FieldID + `") DO 
UPDATE SET "` + lock.FieldVersion + `" = $2, "` + lock.FieldOwner + `" = $3 
WHERE ` + lock.Table + `."` + lock.FieldVersion + `" IS NULL OR ` + lock.Table + `."` + lock.FieldVersion + `" = $4`
View Source
const SQLiteQuery = `
INSERT INTO ` + lock.Table + ` ("` + lock.FieldID + `", "` + lock.FieldVersion + `", "` + lock.FieldOwner + `")
VALUES( ?, ?, ? )
ON CONFLICT ("` + lock.FieldID + `") DO 
UPDATE SET "` + lock.FieldVersion + `" = ?, "` + lock.FieldOwner + `" = ? 
WHERE ` + lock.Table + `."` + lock.FieldVersion + `" IS NULL OR ` + lock.Table + `."` + lock.FieldVersion + `" = ?`

Variables

View Source
var (
	ErrInvalidOptions    = errors.New("invalid options")
	ErrOpeningDatabase   = errors.New("failed opening connection to database")
	ErrRunningMigrations = errors.New("failed running database migrations")
	ErrClosingDatabase   = errors.New("failed closing connection to database")

	ErrNotAcquired = errors.New("lock not acquired")
	ErrNotReleased = errors.New("lock not released")

	ErrCreateTransaction = errors.New("cannot create transaction")
	ErrCommitTransaction = errors.New("cannot commit transaction")

	ErrSerializationError  = errors.New("serialization error")
	ErrRefreshLease        = errors.New("cannot refresh lease")
	ErrLockAlreadyReleased = errors.New("lock already released")
)
View Source
var (
	ErrInvalidOwner                 = errors.New("invalid owner")
	ErrInvalidDBType                = errors.New("invalid database type")
	ErrInvalidDatabaseURL           = errors.New("invalid database URL")
	ErrInvalidLeaseDuration         = errors.New("invalid lease duration")
	ErrInvalidLeaseRefreshFrequency = errors.New("invalid lease refresh frequency")
)

Functions

This section is empty.

Types

type DBLock added in v0.1.1

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

func New

func New(options *Options) (*DBLock, error)

func (*DBLock) Acquire added in v0.1.1

func (db *DBLock) Acquire(l *Lock, failIfLocked ...bool) error

func (*DBLock) Lock added in v0.1.1

func (db *DBLock) Lock(id string) *Lock

func (*DBLock) Release added in v0.1.1

func (db *DBLock) Release(l *Lock) error

func (*DBLock) Stop added in v0.1.1

func (db *DBLock) Stop() error

type DBType

type DBType int
const (
	Undefined DBType = iota
	Postgres
	SQLite
)

type Lock

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

func (*Lock) Acquire added in v0.1.1

func (l *Lock) Acquire(failIfLocked ...bool) error

func (*Lock) ID added in v0.1.1

func (l *Lock) ID() string

func (*Lock) IsReleased

func (l *Lock) IsReleased() bool

func (*Lock) Release added in v0.1.1

func (l *Lock) Release() error

func (*Lock) Version

func (l *Lock) Version() uuid.UUID

type Options

type Options struct {
	Logger                types.SubLogger
	Owner                 string
	DBType                DBType
	DatabaseURL           string
	LeaseDuration         time.Duration
	LeaseRefreshFrequency time.Duration
}

func (*Options) Validate added in v0.1.5

func (o *Options) Validate() error

Directories

Path Synopsis
ent
pkg

Jump to

Keyboard shortcuts

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