lockmap

package module
v0.0.0-...-e17aea3 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2024 License: BSD-3-Clause Imports: 3 Imported by: 3

README

LockMap

GoDoc

Motivation

This is a recurrent pattern I met in different situations. For example:

  • When we need to process an HTTP request if the result is cacheable: we want to wait one request to finish and then to copy the result to everybody who requested it.
  • When we need to allow only one goroutine to send a request out (which depends on variables).
  • etc.

Quick start

Synchronous locking:

lm := lockmap.New()

...

    ctx := context.Background()

    unlocker := lm.Lock(ctx, "key1")
    defer unlocker.Unlock(ctx, "key1")

    // do something with key1

Asynchronous locking:

lm := lockmap.New()

...

    ctx, cancelFn := context.WithTimeout(context.Background(), time.Second*30)
    defer cancelFn()

    unlocker, waiter := lm.LockAsync(ctx, "key1")
    defer unlocker.Unlock("key1")

    select {
    case <-waiter.C:
        if !unlocker.IsLocked() {
            // reached 30sec timeout
            return
        }
        // do something with key1
    case <-someOtherChan:
        // do something else
    }

Documentation

Overview

The code was copied from https://github.com/immune-gmbh/attestation-sdk/blob/main/pkg/lockmap and then modified by Dmitrii Okunev in 2024 (the license is the same).

Copyright 2023 Meta Platforms, Inc. and affiliates.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The code was copied from https://github.com/immune-gmbh/attestation-sdk/blob/main/pkg/lockmap and then modified by Dmitrii Okunev in 2024 (the license is the same).

Copyright 2023 Meta Platforms, Inc. and affiliates.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LockMap

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

LockMap is a naive implementation of locking by specified key.

func NewLockMap

func NewLockMap() *LockMap

NewLockMap returns an instance of LockMap.

func (*LockMap) Lock

func (m *LockMap) Lock(ctx context.Context, key any) *Unlocker

Lock locks the key.

func (*LockMap) LockAsync

func (m *LockMap) LockAsync(ctx context.Context, key any) (*Unlocker, *Waiter)

type Unlocker

type Unlocker struct {
	// UserData is a field for arbitrary data, which could be used by
	// external packages
	UserData any
	// contains filtered or unexported fields
}

Unlocker provides method Unlock, which could be used to unlock the key.

func (*Unlocker) IsLocked

func (u *Unlocker) IsLocked() bool

func (*Unlocker) Unlock

func (u *Unlocker) Unlock()

Unlock releases the lock for the key.

type Waiter

type Waiter struct {
	C <-chan struct{}
}

Jump to

Keyboard shortcuts

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