sync2

package
v2.0.0-alpha2+incompat... Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2015 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package sync2 provides extra functionality along the same lines as sync.

Index

Constants

View Source
const (
	SERVICE_STOPPED = iota
	SERVICE_RUNNING
	SERVICE_SHUTTING_DOWN
)

These are the three predefined states of a service.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicDuration

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

AtomicDuration is a wrapper with a simpler interface around atomic.(Add|Store|Load|CompareAndSwap)Int64 functions.

func NewAtomicDuration

func NewAtomicDuration(duration time.Duration) AtomicDuration

NewAtomicDuration initializes a new AtomicDuration with a given value.

func (*AtomicDuration) Add

func (d *AtomicDuration) Add(duration time.Duration) time.Duration

Add atomically adds duration to the value.

func (*AtomicDuration) CompareAndSwap

func (d *AtomicDuration) CompareAndSwap(oldval, newval time.Duration) (swapped bool)

CompareAndSwap atomatically swaps the old with the new value.

func (*AtomicDuration) Get

func (d *AtomicDuration) Get() time.Duration

Get atomically returns the current value.

func (*AtomicDuration) Set

func (d *AtomicDuration) Set(duration time.Duration)

Set atomically sets duration as new value.

type AtomicInt32

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

AtomicInt32 is a wrapper with a simpler interface around atomic.(Add|Store|Load|CompareAndSwap)Int32 functions.

func NewAtomicInt32

func NewAtomicInt32(n int32) AtomicInt32

NewAtomicInt32 initializes a new AtomicInt32 with a given value.

func (*AtomicInt32) Add

func (i *AtomicInt32) Add(n int32) int32

Add atomically adds n to the value.

func (*AtomicInt32) CompareAndSwap

func (i *AtomicInt32) CompareAndSwap(oldval, newval int32) (swapped bool)

CompareAndSwap atomatically swaps the old with the new value.

func (*AtomicInt32) Get

func (i *AtomicInt32) Get() int32

Get atomically returns the current value.

func (*AtomicInt32) Set

func (i *AtomicInt32) Set(n int32)

Set atomically sets n as new value.

type AtomicInt64

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

AtomicInt64 is a wrapper with a simpler interface around atomic.(Add|Store|Load|CompareAndSwap)Int64 functions.

func NewAtomicInt64

func NewAtomicInt64(n int64) AtomicInt64

NewAtomicInt64 initializes a new AtomicInt64 with a given value.

func (*AtomicInt64) Add

func (i *AtomicInt64) Add(n int64) int64

Add atomically adds n to the value.

func (*AtomicInt64) CompareAndSwap

func (i *AtomicInt64) CompareAndSwap(oldval, newval int64) (swapped bool)

CompareAndSwap atomatically swaps the old with the new value.

func (*AtomicInt64) Get

func (i *AtomicInt64) Get() int64

Get atomically returns the current value.

func (*AtomicInt64) Set

func (i *AtomicInt64) Set(n int64)

Set atomically sets n as new value.

type AtomicString

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

AtomicString gives you atomic-style APIs for string, but it's only a convenience wrapper that uses a mutex. So, it's not as efficient as the rest of the atomic types.

func (*AtomicString) CompareAndSwap

func (s *AtomicString) CompareAndSwap(oldval, newval string) (swqpped bool)

CompareAndSwap atomatically swaps the old with the new value.

func (*AtomicString) Get

func (s *AtomicString) Get() string

Get atomically returns the current value.

func (*AtomicString) Set

func (s *AtomicString) Set(str string)

Set atomically sets str as new value.

type Cond

type Cond struct {
	L sync.Locker
	// contains filtered or unexported fields
}

Cond is an alternate implementation of sync.Cond

func NewCond

func NewCond(l sync.Locker) *Cond

func (*Cond) Broadcast

func (c *Cond) Broadcast()

func (*Cond) Signal

func (c *Cond) Signal()

func (*Cond) Wait

func (c *Cond) Wait()

type Consolidator

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

Consolidator consolidates duplicate queries from executing simulaneously and shares results between them.

func NewConsolidator

func NewConsolidator() *Consolidator

NewConsolidator creates a new Consolidator

func (*Consolidator) Create

func (co *Consolidator) Create(query string) (r *Result, created bool)

Create adds a query to currently executing queries and acquires a lock on its Result if it is not already present. If the query is a duplicate, Create returns false.

func (*Consolidator) ServeHTTP

func (co *Consolidator) ServeHTTP(response http.ResponseWriter, request *http.Request)

type Result

type Result struct {
	Result interface{}
	Err    error
	// contains filtered or unexported fields
}

Result is a wrapper for result of a query.

func (*Result) Broadcast

func (rs *Result) Broadcast()

Broadcast removes the entry from current queries and releases the lock on its Result. Broadcast should be invoked when original query completes execution.

func (*Result) Wait

func (rs *Result) Wait()

Wait waits for the original query to complete execution. Wait should be invoked for duplicate queries.

type Semaphore

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

Semaphore is a counting semaphore with the option to specify a timeout.

func NewSemaphore

func NewSemaphore(count int, timeout time.Duration) *Semaphore

NewSemaphore creates a Semaphore. The count parameter must be a positive number. A timeout of zero means that there is no timeout.

func (*Semaphore) Acquire

func (sem *Semaphore) Acquire() bool

Acquire returns true on successful acquisition, and false on a timeout.

func (*Semaphore) Release

func (sem *Semaphore) Release()

Release releases the acquired semaphore. You must not release more than the number of semaphores you've acquired.

func (*Semaphore) TryAcquire

func (sem *Semaphore) TryAcquire() bool

TryAcquire acquires a semaphore if it's immediately available. It returns false otherwise.

type ServiceContext

type ServiceContext struct {
	// ShuttingDown is a channel that the service can select on to be notified
	// when it should shut down. The channel is closed when the state transitions
	// from SERVICE_RUNNING to SERVICE_SHUTTING_DOWN.
	ShuttingDown chan struct{}
}

ServiceContext is passed into the service function to give it access to information about the running service. You can create an empty service context, in which case it will be non-cancelable.

func (*ServiceContext) IsRunning

func (svc *ServiceContext) IsRunning() bool

IsRunning returns true if the ServiceContext.ShuttingDown channel has not been closed yet.

type ServiceManager

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

ServiceManager manages the state of a service through its lifecycle. It's not recommended to nest service managers because they introduce race conditions in the Stop functions that can cause one service to indefinitely wait for the other. You can instead pass the top level ServiceContext around and manage shutdown using the single ServiceManager.

func (*ServiceManager) Go

func (svm *ServiceManager) Go(service func(svc *ServiceContext) error) bool

Go tries to change the state from SERVICE_STOPPED to SERVICE_RUNNING.

If the current state is not SERVICE_STOPPED (already running), it returns false immediately.

On successful transition, it launches the service as a goroutine and returns true. The service function is responsible for returning on its own when requested, either by regularly checking svc.IsRunning(), or by waiting for the svc.ShuttingDown channel to be closed.

When the service func returns, the state is reverted to SERVICE_STOPPED.

func (*ServiceManager) Join

func (svm *ServiceManager) Join() error

Join waits for the service to terminate and returns the value returned by the service function.

func (*ServiceManager) State

func (svm *ServiceManager) State() int64

State returns the current state of the service. This should only be used to report the current state.

func (*ServiceManager) StateName

func (svm *ServiceManager) StateName() string

StateName returns the name of the current state.

func (*ServiceManager) Stop

func (svm *ServiceManager) Stop() bool

Stop tries to change the state from SERVICE_RUNNING to SERVICE_SHUTTING_DOWN. If the current state is not SERVICE_RUNNING, it returns false immediately. On successul transition, it waits for the service to finish, and returns true. You are allowed to Go() again after a Stop().

Jump to

Keyboard shortcuts

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