Back to

Package memlock

Latest Go to latest

The latest major version is .

Published: Aug 13, 2020 | License: Apache-2.0 | Module:


Package memlock allows multiple appengine handlers to coordinate best-effort mutual execution via memcache. "best-effort" here means "best-effort"... memcache is not reliable. However, colliding on memcache is a lot cheaper than, for example, colliding with datastore transactions.



var ErrEmptyClientID = errors.New("memlock: empty clientID")

ErrEmptyClientID is returned from TryWithLock when you specify an empty clientID.

var ErrFailedToLock = errors.New("memlock: failed to obtain lock")

ErrFailedToLock is returned from TryWithLock when it fails to obtain a lock prior to invoking the user-supplied function.

func TryWithLock

func TryWithLock(ctx context.Context, key, clientID string, f func(context.Context) error) error

TryWithLock attempts to obtains the lock once, and then invokes f if successful. The context provided to f will be canceled (e.g. ctx.Done() will be closed) if memlock detects that we've lost the lock.

TryWithLock function returns ErrFailedToLock if it fails to obtain the lock, otherwise returns the error that f returns.

`key` is the memcache key to use (i.e. the name of the lock). Clients locking the same data must use the same key. clientID is the unique identifier for this client (lock-holder). If it's empty then TryWithLock() will return ErrEmptyClientID.

Note that the lock provided by TryWithLock is a best-effort lock... some other form of locking or synchronization should be used inside of f (such as Datastore transactions) to ensure that f is, in fact, operating exclusively. The purpose of TryWithLock is to have a cheap filter to prevent unnecessary contention on heavier synchronization primitives like transactions.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier