contextlock

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2023 License: MIT-0 Imports: 2 Imported by: 0

README

contextlock

godocs

contextlock is a small library for putting values in a Go context.Context that should not be immediately accessible. The locks placed on the context value could be used by a web application to assert that authentication is followed by proper authorization checks before some value is used.

The library is feature-complete as-is and is unlikely to get any further lock types beyond the boolean, function, and time locks already provided. Many applications might consider already this limited set of locks to be exaggerated, and I encourage you to copy over the code that you need to your codebase to remove the dependency on code you don't.

At the moment, the library is missing documentation and examples.

Licensed under the MIT No Attribution license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FunctionLock

func FunctionLock(parent context.Context, lockKey any, fn lockFunction) context.Context

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

func Lock(parent context.Context, lockKey any) context.Context

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 Unlock

func Unlock(parent context.Context, lockKey any) context.Context

Unlock returns a copy of parent where the lock behind lockKey is unlocked.

func Unlocked

func Unlocked(ctx context.Context, lockKey any) bool

Unlocked returns true if the lock behind lockKey in ctx is unlocked.

func Value

func Value(ctx context.Context, key any) (any, bool)

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

func WithValue(parent context.Context, lockKey, key, value any) context.Context

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

func (c Container) Value(ctx context.Context) (any, bool)

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.

Jump to

Keyboard shortcuts

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