mysqllock

package
v1.101.9 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package mysqllock provides a distributed locking mechanism that leverages MySQL's internal functions.

This package allows you to acquire and release locks using MySQL's GET_LOCK and RELEASE_LOCK functions. It provides a MySQLLock struct that represents a locker and has methods for acquiring and releasing locks.

Example usage:

// Create a new MySQLLock instance
db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/database")
if err != nil {
    log.Fatal(err)
}
defer db.Close()
lock := mysqllock.New(db)

// Acquire a lock
releaseFunc, err := lock.Acquire(context.Background(), "my_lock_key", 10*time.Second)
if err != nil {
    log.Fatal(err)
}
defer releaseFunc()

// Perform locked operations

// Release the lock
err = releaseFunc()
if err != nil {
    log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeout is an error when the lock is not acquired within the timeout.
	ErrTimeout = errors.New("acquire lock timeout")

	// ErrFailed is an error when the lock is not acquired.
	ErrFailed = errors.New("failed to acquire a lock")
)

Functions

This section is empty.

Types

type MySQLLock

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

MySQLLock represents a locker.

func New

func New(db *sql.DB) *MySQLLock

New creates a new instance of the locker.

func (*MySQLLock) Acquire

func (l *MySQLLock) Acquire(ctx context.Context, key string, timeout time.Duration) (ReleaseFunc, error)

Acquire attempts to acquire a database lock.

type ReleaseFunc

type ReleaseFunc func() error

ReleaseFunc is an alias for a release lock function.

Jump to

Keyboard shortcuts

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