rundown_protection

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: Apache-2.0 Imports: 3 Imported by: 2

README

Rundown-Protection

Golang implementation of a rundown protection for accessing a shared object.

Some times we need to access a shared resource than can be created and deleted by a routine we don't control. Or the opposite, we know when we create and delete the resource but we don't control when it is accessed.

Unlike sync.WaitGroup, this library does not panic if you try to access an unavailable resource. Just check the return Acquire method to ensure if you can safely use the resource.

Usage

import rp "github.com/randlabs/rundown-protection"
//create a new rundown protection object.
r := rp.Create()

if r.Acquire() {
    //access granted
    go func() {
        //use the shared resource
        r.Release()
    }()
}

//wait until all references to the shared resource are released
r.Wait()

//trying to acquire the shared resource will fail
if r.Acquire() {
    //this code is never executed
}

Run demo: go run ./_examples/demo.go

License

See LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RundownProtection

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

func Create

func Create() *RundownProtection

Create creates a new rundown protection object.

func (*RundownProtection) Acquire

func (r *RundownProtection) Acquire() bool

Acquire increments the usage counter unless a rundown is in progress.

func (*RundownProtection) Deadline added in v1.1.0

func (_ *RundownProtection) Deadline() (deadline time.Time, ok bool)

Deadline returns the time when work done on behalf of this context should be canceled.

func (*RundownProtection) Done added in v1.1.0

func (r *RundownProtection) Done() <-chan struct{}

Done returns a channel that's closed when work done on behalf of this context should be canceled.

func (*RundownProtection) Err added in v1.1.0

func (r *RundownProtection) Err() error

Err returns a non-nil error explaining why the channel was closed or nil if still open.

func (*RundownProtection) Initialize added in v1.0.2

func (r *RundownProtection) Initialize()

Initialize initializes a rundown protection object.

func (*RundownProtection) Release

func (r *RundownProtection) Release()

Release decrements the usage counter.

func (*RundownProtection) Value added in v1.1.0

func (_ *RundownProtection) Value(_ any) any

Value returns the value associated with this context for key, if any exists, or nil.

func (*RundownProtection) Wait

func (r *RundownProtection) Wait()

Wait initiates the shutdown process and waits until all acquisitions are released.

Jump to

Keyboard shortcuts

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