ctxlock

package
v1.5.32 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2024 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Overview

Package ctxlock provides a locking mechanism based on context cancellation.

Contexts derived from a Locker are canceled when the underlying connection to the lock provider is gone, or when a parent context is canceled.

This package makes use of "unsafe" to avoid some allocations, but the "safe" build tag can be provided to use allocating versions of the functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locker

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

Locker provides context-scoped locks.

func New

func New(ctx context.Context, p *pgxpool.Pool) (*Locker, error)

New creates a Locker that will pull connections from the provided pool.

The provided context is only used for logging and initial setup. Close must be called to release held resources.

func (*Locker) Close

func (l *Locker) Close(_ context.Context) (_ error)

Close spins down background goroutines and frees resources.

func (*Locker) Lock

func (l *Locker) Lock(parent context.Context, key string) (context.Context, context.CancelFunc)

Lock attempts to obtain the named lock until it succeeds or the passed Context is canceled.

func (*Locker) TryLock

func (l *Locker) TryLock(parent context.Context, key string) (context.Context, context.CancelFunc)

TryLock attempts to lock on the provided key.

If unsuccessful, an already-canceled Context will be returned.

If successful, the returned Context will be parented to the passed-in Context and also to the underlying connection used for the lock.

Jump to

Keyboard shortcuts

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