Documentation
¶
Overview ¶
Package pools provides functionality to manage and reuse resources like connections.
Modified by Duncan Jones to reduce the number of external dependencies.
Package timer provides various enhanced timer functions.
Index ¶
- Variables
- type AtomicBool
- type AtomicDuration
- type AtomicInt32
- type AtomicInt64
- type AtomicString
- type Factory
- type Resource
- type ResourcePool
- func (rp *ResourcePool) Active() int64
- func (rp *ResourcePool) Available() int64
- func (rp *ResourcePool) Capacity() int64
- func (rp *ResourcePool) Close()
- func (rp *ResourcePool) Get(ctx context.Context) (resource Resource, err error)
- func (rp *ResourcePool) IdleClosed() int64
- func (rp *ResourcePool) IdleTimeout() time.Duration
- func (rp *ResourcePool) InUse() int64
- func (rp *ResourcePool) IsClosed() (closed bool)
- func (rp *ResourcePool) MaxCap() int64
- func (rp *ResourcePool) Put(resource Resource)
- func (rp *ResourcePool) SetCapacity(capacity int) error
- func (rp *ResourcePool) SetIdleTimeout(idleTimeout time.Duration)
- func (rp *ResourcePool) StatsJSON() string
- func (rp *ResourcePool) WaitCount() int64
- func (rp *ResourcePool) WaitTime() time.Duration
- type Semaphore
- type Timer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClosed is returned if ResourcePool is used when it's closed. ErrClosed = errors.New("resource pool is closed") // ErrTimeout is returned if a resource get times out. ErrTimeout = errors.New("resource pool timed out") )
Functions ¶
This section is empty.
Types ¶
type AtomicBool ¶
type AtomicBool struct {
// contains filtered or unexported fields
}
AtomicBool gives an atomic boolean variable.
func NewAtomicBool ¶
func NewAtomicBool(n bool) AtomicBool
NewAtomicBool initializes a new AtomicBool with a given value.
func (*AtomicBool) CompareAndSwap ¶
func (i *AtomicBool) CompareAndSwap(o, n bool) bool
CompareAndSwap atomatically swaps the old with the new value.
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.
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.
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 Resource ¶
type Resource interface {
Close()
}
Resource defines the interface that every resource must provide. Thread synchronization between Close() and IsClosed() is the responsibility of the caller.
type ResourcePool ¶
type ResourcePool struct {
// contains filtered or unexported fields
}
ResourcePool allows you to use a pool of resources.
func NewResourcePool ¶
func NewResourcePool(factory Factory, capacity, maxCap int, idleTimeout time.Duration, prefillParallelism int) *ResourcePool
NewResourcePool creates a new ResourcePool pool. capacity is the number of possible resources in the pool: there can be up to 'capacity' of these at a given time. maxCap specifies the extent to which the pool can be resized in the future through the SetCapacity function. You cannot resize the pool beyond maxCap. If a resource is unused beyond idleTimeout, it's replaced with a new one. An idleTimeout of 0 means that there is no timeout. A non-zero value of prefillParallelism causes the pool to be pre-filled. The value specifies how many resources can be opened in parallel.
func (*ResourcePool) Active ¶
func (rp *ResourcePool) Active() int64
Active returns the number of active (i.e. non-nil) resources either in the pool or claimed for use
func (*ResourcePool) Available ¶
func (rp *ResourcePool) Available() int64
Available returns the number of currently unused and available resources.
func (*ResourcePool) Capacity ¶
func (rp *ResourcePool) Capacity() int64
Capacity returns the capacity.
func (*ResourcePool) Close ¶
func (rp *ResourcePool) Close()
Close empties the pool calling Close on all its resources. You can call Close while there are outstanding resources. It waits for all resources to be returned (Put). After a Close, Get is not allowed.
func (*ResourcePool) Get ¶
func (rp *ResourcePool) Get(ctx context.Context) (resource Resource, err error)
Get will return the next available resource. If capacity has not been reached, it will create a new one using the factory. Otherwise, it will wait till the next resource becomes available or a timeout. A timeout of 0 is an indefinite wait.
func (*ResourcePool) IdleClosed ¶
func (rp *ResourcePool) IdleClosed() int64
IdleClosed returns the count of resources closed due to idle timeout.
func (*ResourcePool) IdleTimeout ¶
func (rp *ResourcePool) IdleTimeout() time.Duration
IdleTimeout returns the idle timeout.
func (*ResourcePool) InUse ¶
func (rp *ResourcePool) InUse() int64
InUse returns the number of claimed resources from the pool
func (*ResourcePool) IsClosed ¶
func (rp *ResourcePool) IsClosed() (closed bool)
IsClosed returns true if the resource pool is closed.
func (*ResourcePool) MaxCap ¶
func (rp *ResourcePool) MaxCap() int64
MaxCap returns the max capacity.
func (*ResourcePool) Put ¶
func (rp *ResourcePool) Put(resource Resource)
Put will return a resource to the pool. For every successful Get, a corresponding Put is required. If you no longer need a resource, you will need to call Put(nil) instead of returning the closed resource. This will cause a new resource to be created in its place.
func (*ResourcePool) SetCapacity ¶
func (rp *ResourcePool) SetCapacity(capacity int) error
SetCapacity changes the capacity of the pool. You can use it to shrink or expand, but not beyond the max capacity. If the change requires the pool to be shrunk, SetCapacity waits till the necessary number of resources are returned to the pool. A SetCapacity of 0 is equivalent to closing the ResourcePool.
func (*ResourcePool) SetIdleTimeout ¶
func (rp *ResourcePool) SetIdleTimeout(idleTimeout time.Duration)
SetIdleTimeout sets the idle timeout. It can only be used if there was an idle timeout set when the pool was created.
func (*ResourcePool) StatsJSON ¶
func (rp *ResourcePool) StatsJSON() string
StatsJSON returns the stats in JSON format.
func (*ResourcePool) WaitCount ¶
func (rp *ResourcePool) WaitCount() int64
WaitCount returns the total number of waits.
func (*ResourcePool) WaitTime ¶
func (rp *ResourcePool) WaitTime() time.Duration
WaitTime returns the total wait time.
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore is a counting semaphore with the option to specify a timeout.
func NewSemaphore ¶
NewSemaphore creates a Semaphore. The count parameter must be a positive number. A timeout of zero means that there is no 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 ¶
TryAcquire acquires a semaphore if it's immediately available. It returns false otherwise.
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
Timer provides timer functionality that can be controlled by the user. You start the timer by providing it a callback function, which it will call at the specified interval.
var t = timer.NewTimer(1e9)
t.Start(KeepHouse)
func KeepHouse() {
// do house keeping work
}
You can stop the timer by calling t.Stop, which is guaranteed to wait if KeepHouse is being executed.
You can create an untimely trigger by calling t.Trigger. You can also schedule an untimely trigger by calling t.TriggerAfter.
The timer interval can be changed on the fly by calling t.SetInterval. A zero value interval will cause the timer to wait indefinitely, and it will react only to an explicit Trigger or Stop.
func (*Timer) SetInterval ¶
SetInterval changes the wait interval. It will cause the timer to restart the wait.
func (*Timer) Stop ¶
func (tm *Timer) Stop()
Stop will stop the timer. It guarantees that the timer will not execute any more calls to keephouse once it has returned.
func (*Timer) Trigger ¶
func (tm *Timer) Trigger()
Trigger will cause the timer to immediately execute the keephouse function. It will then cause the timer to restart the wait.
func (*Timer) TriggerAfter ¶
TriggerAfter waits for the specified duration and triggers the next event.