locker

package
v2.26.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: MIT Imports: 4 Imported by: 0

README

Locker

Usage

package main

import (
	"context"
	"time"

	"github.com/redis/go-redis/v9"

	"github.com/go-kratos-ecosystem/components/v2/cache"
	redisStore "github.com/go-kratos-ecosystem/components/v2/cache/redis"
	redisLocker "github.com/go-kratos-ecosystem/components/v2/locker/redis"
)

func main() {
	client := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	})

	// ex1: 直接用
	locker := redisLocker.NewLocker(client, redisLocker.WithName("lock"), redisLocker.WithTTL(5*time.Minute))
	_ = locker.Try(context.Background(), func() {
		// do something
	})

	// ex2: 基于缓存用
	repository := cache.NewRepository(
		redisStore.New(client, redisStore.Prefix("cache")),
	)

	_ = repository.Lock("lock", 5*time.Minute).Try(context.Background(), func() {
		// do something
	})
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLocked    = errors.New("locker: the locker is locked")
	ErrTimeout   = errors.New("locker: the locker is timeout")
	ErrNotLocked = errors.New("locker: the locker is not locked")
)

Functions

This section is empty.

Types

type Locker

type Locker interface {
	// Try tries to get the lock.
	// if the lock is locked, it will return an error.
	Try(ctx context.Context, fn func()) error

	// Until tries to get the lock until the timeout.
	// if the lock is locked, it will wait until the lock is released or the timeout is reached.
	Until(ctx context.Context, timeout time.Duration, fn func()) error

	// Get tries to get the lock.
	// if the lock is locked, it will return the owner of the lock or an error.
	Get(ctx context.Context) (Owner, error)

	// Release releases the locked owner.
	// if the owner is not the locked owner, it will return an error.
	Release(ctx context.Context, owner Owner) error

	// ForceRelease releases the lock forcibly.
	ForceRelease(ctx context.Context) error

	// LockedOwner returns the owner of the lock.
	LockedOwner(ctx context.Context) (Owner, error)
}

type NoopLocker added in v2.23.0

type NoopLocker struct{}

func (NoopLocker) ForceRelease added in v2.23.0

func (l NoopLocker) ForceRelease(context.Context) error

func (NoopLocker) Get added in v2.23.0

func (l NoopLocker) Get(context.Context) (Owner, error)

func (NoopLocker) LockedOwner added in v2.23.0

func (l NoopLocker) LockedOwner(context.Context) (Owner, error)

func (NoopLocker) Release added in v2.23.0

func (l NoopLocker) Release(context.Context, Owner) error

func (NoopLocker) Try added in v2.23.0

func (l NoopLocker) Try(context.Context, func()) error

func (NoopLocker) Until added in v2.23.0

func (l NoopLocker) Until(context.Context, time.Duration, func()) error

type Owner added in v2.23.0

type Owner interface {
	// Name returns the name of the owner.
	Name() string

	// Release releases the lock.
	Release(ctx context.Context) error
}

func NewOwner added in v2.23.0

func NewOwner(locker Locker, opts ...OwnerOption) Owner

type OwnerOption added in v2.23.0

type OwnerOption func(*owner)

func WithOwnerName added in v2.23.0

func WithOwnerName(name string) OwnerOption

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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