shared

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: BSD-2-Clause Imports: 1 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package shared runs the paired operations on a resource several owners share once each, however many owners call them.

An input and an output transport share a single client, so both set it up, both join its room, and both tear it down. Processors are set up and cleaned up concurrently, so those calls overlap.

Give the resource a Resource and route the paired operations through Acquire and Release, and the work runs once: the first owner to acquire runs the body while the rest wait for it, and only the last owner to release runs the undo.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Resource

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

Resource counts the owners holding a shared resource and serializes them. Use one Resource per resource, so paired operations on the same object (setting a client up and cleaning it up, joining a room and leaving it) count their owners separately.

The zero value is ready to use. It is safe for concurrent use.

func (*Resource) Acquire

func (r *Resource) Acquire(build func() error) error

Acquire runs build for the first owner to acquire the resource.

Owners that arrive while the first is still running wait for it to finish, so no caller continues against a half-built resource. Later owners return nil without running build again.

A build that fails leaves the resource unbuilt, and every owner of it is told: the error is returned to each one that arrives later, rather than build being attempted again. So the owners of a resource share its verdict, and a pair of processors sharing a client either both come up or both fail.

The lock is not reentrant, so build must not acquire or release the same resource.

func (*Resource) Release

func (r *Resource) Release(undo func() error) error

Release runs undo for the last owner to release the resource.

Every other owner returns nil without running it, as does a release that no acquire matched.

The lock is not reentrant, so undo must not acquire or release the same resource.

Jump to

Keyboard shortcuts

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