gxsync

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2020 License: Apache-2.0 Imports: 11 Imported by: 3

Documentation

Overview

Copyright 2016 ~ 2018 AlexStocks(https://github.com/AlexStocks). All rights reserved. Use of this source code is governed by Apache License 2.0.

refers to github.com/jonhoo/drwmutex

Copyright 2016 ~ 2018 AlexStocks(https://github.com/AlexStocks). All rights reserved. Use of this source code is governed by Apache License 2.0.

refers to github.com/jonhoo/drwmutex

Package gxsync provides some synchronization primitives such as trylock & semaphore.

Copyright 2016 ~ 2018 AlexStocks(https://github.com/AlexStocks). All rights reserved. Use of this source code is governed by Apache License 2.0.

refers to github.com/jonhoo/drwmutex

Copyright 2016 ~ 2018 AlexStocks(https://github.com/AlexStocks). All rights reserved. Use of this source code is governed by Apache License 2.0.

refers from github.com/gofrs/flock

this file provides a kind of unbouned channel

Package gxsync provides some synchronization primitives such as trylock & semaphore.

Index

Examples

Constants

View Source
const (
	QSize = 64
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DRWMutex added in v0.3.0

type DRWMutex []paddedRWMutex

drwmutex provides a DRWMutex, a distributed RWMutex for use when there are many readers spread across many cores, and relatively few cores. DRWMutex is meant as an almost drop-in replacement for sync.RWMutex.

func NewDRWMutex added in v0.3.0

func NewDRWMutex() DRWMutex

New returns a new, unlocked, distributed RWMutex.

func (DRWMutex) Lock added in v0.3.0

func (mx DRWMutex) Lock()

Lock takes out an exclusive writer lock similar to sync.Mutex.Lock. A writer lock also excludes all readers.

func (DRWMutex) RLock added in v0.3.0

func (mx DRWMutex) RLock() (l sync.Locker)

RLock takes out a non-exclusive reader lock, and returns the lock that was taken so that it can later be released.

func (DRWMutex) RLocker added in v0.3.0

func (mx DRWMutex) RLocker() sync.Locker

RLocker returns a sync.Locker presenting Lock() and Unlock() methods that take and release a non-exclusive *reader* lock. Note that this call may be relatively slow, depending on the underlying system architechture, and so its result should be cached if possible.

func (DRWMutex) Unlock added in v0.3.0

func (mx DRWMutex) Unlock()

Unlock releases an exclusive writer lock similar to sync.Mutex.Unlock.

type Empty

type Empty struct{}

type Flock added in v0.3.2

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

Flock is the struct type to handle file locking. All fields are unexported, with access to some of the fields provided by getter methods (Path() and Locked()).

func NewFlock added in v0.3.2

func NewFlock(filePath string) *Flock

NewFlock returns a new instance of *Flock. The only parameter it takes is the path to the desired lockfile.

func (*Flock) Close added in v0.3.2

func (f *Flock) Close() error

Close is equivalent to calling Unlock.

This will release the lock and close the underlying file descriptor. It will not remove the file from disk, that's up to your application.

func (*Flock) Lock added in v0.3.2

func (f *Flock) Lock() error

Lock is a blocking call to try and take an exclusive file lock. It will wait until it is able to obtain the exclusive file lock. It's recommended that TryLock() be used over this function. This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.

If we are already exclusive-locked, this function short-circuits and returns immediately assuming it can take the mutex lock.

If the *Flock has a shared lock (RLock), this may transparently replace the shared lock with an exclusive lock on some UNIX-like operating systems. Be careful when using exclusive locks in conjunction with shared locks (RLock()), because calling Unlock() may accidentally release the exclusive lock that was once a shared lock.

func (*Flock) Locked added in v0.3.2

func (f *Flock) Locked() bool

Locked returns the lock state (locked: true, unlocked: false).

Warning: by the time you use the returned value, the state may have changed.

Example
f := NewFlock(os.TempDir() + "/go-lock.lock")
f.TryLock() // unchecked errors here

fmt.Printf("locked: %v\n", f.Locked())

f.Unlock()

fmt.Printf("locked: %v\n", f.Locked())
Output:

locked: true
locked: false

func (*Flock) Path added in v0.3.2

func (f *Flock) Path() string

Path returns the path as provided in NewFlock().

func (*Flock) RLock added in v0.3.2

func (f *Flock) RLock() error

RLock is a blocking call to try and take a shared file lock. It will wait until it is able to obtain the shared file lock. It's recommended that TryRLock() be used over this function. This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.

If we are already shared-locked, this function short-circuits and returns immediately assuming it can take the mutex lock.

func (*Flock) RLocked added in v0.3.2

func (f *Flock) RLocked() bool

RLocked returns the read lock state (locked: true, unlocked: false).

Warning: by the time you use the returned value, the state may have changed.

func (*Flock) String added in v0.3.2

func (f *Flock) String() string

func (*Flock) TryLock added in v0.3.2

func (f *Flock) TryLock() (bool, error)

TryLock is the preferred function for taking an exclusive file lock. This function takes an RW-mutex lock before it tries to lock the file, so there is the possibility that this function may block for a short time if another goroutine is trying to take any action.

The actual file lock is non-blocking. If we are unable to get the exclusive file lock, the function will return false instead of waiting for the lock. If we get the lock, we also set the *Flock instance as being exclusive-locked.

Example
// should probably put these in /var/lock
fileLock := NewFlock(os.TempDir() + "/go-lock.lock")

locked, err := fileLock.TryLock()

if err != nil {
	// handle locking error
}

if locked {
	fmt.Printf("path: %s; locked: %v\n", fileLock.Path(), fileLock.Locked())

	if err := fileLock.Unlock(); err != nil {
		// handle unlock error
	}
}

fmt.Printf("path: %s; locked: %v\n", fileLock.Path(), fileLock.Locked())
Output:

func (*Flock) TryLockContext added in v0.3.2

func (f *Flock) TryLockContext(ctx context.Context, retryDelay time.Duration) (bool, error)

TryLockContext repeatedly tries to take an exclusive lock until one of the conditions is met: TryLock succeeds, TryLock fails with error, or Context Done channel is closed.

Example
// should probably put these in /var/lock
fileLock := NewFlock(os.TempDir() + "/go-lock.lock")

lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
locked, err := fileLock.TryLockContext(lockCtx, 678*time.Millisecond)

if err != nil {
	// handle locking error
}

if locked {
	fmt.Printf("path: %s; locked: %v\n", fileLock.Path(), fileLock.Locked())

	if err := fileLock.Unlock(); err != nil {
		// handle unlock error
	}
}

fmt.Printf("path: %s; locked: %v\n", fileLock.Path(), fileLock.Locked())
Output:

func (*Flock) TryRLock added in v0.3.2

func (f *Flock) TryRLock() (bool, error)

TryRLock is the preferred function for taking a shared file lock. This function takes an RW-mutex lock before it tries to lock the file, so there is the possibility that this function may block for a short time if another goroutine is trying to take any action.

The actual file lock is non-blocking. If we are unable to get the shared file lock, the function will return false instead of waiting for the lock. If we get the lock, we also set the *Flock instance as being share-locked.

func (*Flock) TryRLockContext added in v0.3.2

func (f *Flock) TryRLockContext(ctx context.Context, retryDelay time.Duration) (bool, error)

TryRLockContext repeatedly tries to take a shared lock until one of the conditions is met: TryRLock succeeds, TryRLock fails with error, or Context Done channel is closed.

func (*Flock) Unlock added in v0.3.2

func (f *Flock) Unlock() error

Unlock is a function to unlock the file. This file takes a RW-mutex lock, so while it is running the Locked() and RLocked() functions will be blocked.

This function short-circuits if we are unlocked already. If not, it calls syscall.LOCK_UN on the file and closes the file descriptor. It does not remove the file from disk. It's up to your application to do.

Please note, if your shared lock became an exclusive lock this may unintentionally drop the exclusive lock if called by the consumer that believes they have a shared lock. Please see Lock() for more details.

type UnboundedChan

type UnboundedChan struct {

	// Q      *gxqueue.Queue
	Q *gxdeque.Deque
	// contains filtered or unexported fields
}

refer from redisgo/redis/pool.go

func NewUnboundedChan

func NewUnboundedChan() *UnboundedChan

func (*UnboundedChan) Close

func (q *UnboundedChan) Close()

func (*UnboundedChan) Len

func (q *UnboundedChan) Len() int

func (*UnboundedChan) Pop

func (q *UnboundedChan) Pop() interface{}

func (*UnboundedChan) Push

func (q *UnboundedChan) Push(v interface{})

func (*UnboundedChan) SetWaitOption

func (q *UnboundedChan) SetWaitOption(wait bool)

在pop时,如果没有资源,是否等待 即使用乐观锁还是悲观锁

func (*UnboundedChan) TryPop

func (q *UnboundedChan) TryPop() (interface{}, bool)

Directories

Path Synopsis
Package gxatomic provides simple wrappers around numerics to enforce atomic access.
Package gxatomic provides simple wrappers around numerics to enforce atomic access.
Package gxerrgroup implements an actor-runner with deterministic teardown.
Package gxerrgroup implements an actor-runner with deterministic teardown.
Package pool implements a pool of Object interfaces to manage and reuse them.
Package pool implements a pool of Object interfaces to manage and reuse them.

Jump to

Keyboard shortcuts

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