Documentation ¶
Index ¶
- Variables
- func TryLock(conn redis.Conn, key string, expiration time.Duration) (string, error)
- func Unlock(conn redis.Conn, key, value string) (err error)
- type BackOffFactory
- type BackOffFactoryFunc
- type Config
- type Monitor
- type Mutex
- type Once
- type OnceConfig
- type OnceOption
- type Option
- type Pool
- type ScoreFilter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( DefaultBackOffFactory BackOffFactory = BackOffFactoryFunc(func() backoff.BackOff { bo := backoff.NewExponentialBackOff() bo.MaxInterval = 1 * time.Second bo.InitialInterval = 5 * time.Millisecond return bo }) DefaultLockExpiration = 120 * time.Second DefaultOnceExpiration = 3 * 24 * time.Second )
View Source
var ( ErrLocked = errors.New("already locked") ErrConflict = errors.New("locked by another process") )
Functions ¶
Types ¶
type BackOffFactory ¶
type BackOffFactoryFunc ¶
type BackOffFactoryFunc func() backoff.BackOff
func (BackOffFactoryFunc) Create ¶
func (f BackOffFactoryFunc) Create(ctx context.Context) (bo backoff.BackOff)
type Config ¶
type Config struct { BackOffFactory BackOffFactory LockExpiration time.Duration }
type Monitor ¶
type Monitor struct { Config // contains filtered or unexported fields }
Example ¶
defer cleanupTestRedis() var wg sync.WaitGroup ctx := context.Background() monitor := redisync.NewMonitor(pool) for i := 0; i < 5; i++ { wg.Add(1) go func() { defer wg.Done() monitor.Synchronize(ctx, "key", func(context.Context) error { fmt.Println("start") time.Sleep(10 * time.Millisecond) fmt.Println("stop") return nil }) }() } wg.Wait()
Output: start stop start stop start stop start stop start stop
func NewMonitor ¶
type Mutex ¶
type Mutex struct { Config // contains filtered or unexported fields }
Example ¶
defer cleanupTestRedis() var wg sync.WaitGroup ctx := context.Background() mu := redisync.NewMutex(pool, "key") for i := 0; i < 5; i++ { wg.Add(1) go func() { defer wg.Done() mu.Lock(ctx) defer mu.Unlock(ctx) fmt.Println("start") time.Sleep(10 * time.Millisecond) fmt.Println("stop") }() } wg.Wait()
Output: start stop start stop start stop start stop start stop
type Once ¶
type Once struct { OnceConfig // contains filtered or unexported fields }
Example ¶
defer cleanupTestRedis() var wg sync.WaitGroup ctx := context.Background() once := redisync.NewOnce(pool) for i := 0; i < 10; i++ { wg.Add(1) go func() { defer wg.Done() once.Do(ctx, "key", func(context.Context) error { fmt.Println("Only once") return nil }) }() } wg.Wait()
Output: Only once
func NewOnce ¶
func NewOnce(pool Pool, opts ...OnceOption) *Once
type OnceConfig ¶ added in v0.2.0
type OnceOption ¶ added in v0.2.0
type OnceOption func(*OnceConfig)
func WithOnceExpiration ¶ added in v0.2.0
func WithOnceExpiration(d time.Duration) OnceOption
func WithOnceUnlockAfterError ¶ added in v0.2.0
func WithOnceUnlockAfterError() OnceOption
type Option ¶
type Option func(*Config)
func WithBackOffFactory ¶
func WithBackOffFactory(f BackOffFactory) Option
func WithLockExpiration ¶
type ScoreFilter ¶
type ScoreFilter struct { Config // contains filtered or unexported fields }
Example ¶
defer cleanupTestRedis() ctx := context.Background() filter := redisync.NewScoreFilter(pool) for i := 10; i > 0; i-- { i := i ok, err := filter.Filter(ctx, "key", i) if err != nil { // ... } if ok { fmt.Println("Only once") } }
Output: Only once
func NewScoreFilter ¶
func NewScoreFilter(pool Pool, opts ...Option) *ScoreFilter
Source Files ¶
Click to show internal directories.
Click to hide internal directories.