Documentation
¶
Index ¶
- func FunctionLock(parent context.Context, lockKey any, fn lockFunction) context.Context
- func Lock(parent context.Context, lockKey any) context.Context
- func TimeLock(parent context.Context, lockKey any, t time.Time, opts ...TimestampOption) context.Context
- func Unlock(parent context.Context, lockKey any) context.Context
- func Unlocked(ctx context.Context, lockKey any) bool
- func Value(ctx context.Context, key any) (any, bool)
- func WithValue(parent context.Context, lockKey, key, value any) context.Context
- type Container
- type TimestampOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FunctionLock ¶
FunctionLock returns a copy of parent where the lock calls fn to check whether it's unlocked.
The lock is unlocked when fn returns true and locked when fn returns false. The context at the time of calling Unlocked will be passed as the sole argument to the fn function. The fn function must have the following signature:
fn(ctx context.Context) bool
func Lock ¶
Lock returns a copy of parent where the lock behind lockKey is locked.
Locks do not need to be initialized and will remain locked until explicitly unlocked, calling this function is only necessary if you want to lock a previously unlocked lock.
func TimeLock ¶
func TimeLock(parent context.Context, lockKey any, t time.Time, opts ...TimestampOption) context.Context
TimeLock returns a copy of parent where the lock will be unlocked at a provided point in time.
func Value ¶
Value returns the stored value for the given key. If the key is a Container added with WithValue, the container will be unwrapped and Container.Value will be called.
If the value is not a container, (value, false) is returned. If the value is a container and the lock is locked, (nil, false) is returned. If the value is a container and the lock is unlocked, (value, true) is returned.
Calls context.Context.Value for the ctx with the given key.
func WithValue ¶
WithValue returns a copy of parent in which the key is associated with a Container containing the value behind a lockKey.
This works like context.WithValue except the value is always of type Container and will refuse to return the value until the lockKey has been unlocked with Unlock.
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
A Container is a wrapper for any type and a lock.
This type is used for any key-value pairs added to a context.Context using WithValue. Cannot be initialized from outside the contextlock package.
func (Container) Value ¶
Value returns the value contained in the container if and only if the container's lock in ctx is unlocked.
The first value is the item stored in the container, or nil if the lock is locked. The second value returned is a boolean which is false if the container is locked and true otherwise.
type TimestampOption ¶
type TimestampOption func(timestamp) timestamp
TimestampOption provides functional options for a TimeLock.
func TimeSource ¶
func TimeSource(fn func() time.Time) TimestampOption
TimeSource can be passed as a functional option to TimeLock to override time.Now when checking whether a lock is open or not.