Documentation ¶
Index ¶
- Constants
- Variables
- func Collect(errchans ...<-chan error) <-chan error
- func CollectContext(ctx context.Context, errchans ...<-chan error) <-chan error
- type Counter
- type MPMCQueue
- type Map
- func (m *Map) Delete(key string)
- func (m *Map) Load(key string) (value interface{}, ok bool)
- func (m *Map) LoadAndDelete(key string) (value interface{}, loaded bool)
- func (m *Map) LoadOrStore(key string, value interface{}) (actual interface{}, loaded bool)
- func (m *Map) Range(f func(key string, value interface{}) bool)
- func (m *Map) Store(key string, value interface{})
- type Mutex
- type RBMutex
- type RToken
- type Semaphore
- type UnboundedChan
Examples ¶
Constants ¶
const (
QSize = 64
)
Variables ¶
var SpinFor = 50
SpinFor controls the number of iterations Lock/Clock is allowed to spin for before resorting to a slower (channel based) way of locking.
Functions ¶
func Collect ¶ added in v0.0.3
Example ¶
doWork := func(url string) chan error { errs := make(chan error) go func(url string) { // Some expensive work.. _, err := http.Get(url) if err != nil { errs <- err } }(url) return errs } for err := range Collect( doWork("1"), doWork("2"), ) { if err != nil { fmt.Println("Error:", err) } }
Output:
Types ¶
type MPMCQueue ¶ added in v0.0.7
type MPMCQueue struct {
// contains filtered or unexported fields
}
func NewMPMCQueue ¶ added in v0.0.7
NewMPMCQueue creates a new MPMCQueue instance with the given capacity.
func (*MPMCQueue) Dequeue ¶ added in v0.0.7
func (q *MPMCQueue) Dequeue() interface{}
Dequeue retrieves and removes the item from the head of the queue. Blocks, if the queue is empty.
func (*MPMCQueue) Enqueue ¶ added in v0.0.7
func (q *MPMCQueue) Enqueue(item interface{})
Enqueue inserts the given item into the queue. Blocks, if the queue is full.
func (*MPMCQueue) TryDequeue ¶ added in v0.0.7
TryDequeue retrieves and removes the item from the head of the queue. Does not block and returns immediately. The ok result indicates that the queue isn't empty and an item was retrieved.
func (*MPMCQueue) TryEnqueue ¶ added in v0.0.7
TryEnqueue inserts the given item into the queue. Does not block and returns immediately. The result indicates that the queue isn't full and the item was inserted.
type Map ¶ added in v0.0.7
type Map struct {
// contains filtered or unexported fields
}
Map is like a Go map[string]interface{} but is safe for concurrent use by multiple goroutines without additional locking or coordination. It follows the interface of sync.Map.
A Map must not be copied after first use.
Map uses a modified version of Cache-Line Hash Table (CLHT) data structure: https://github.com/LPD-EPFL/CLHT
CLHT is built around idea to organize the hash table in cache-line-sized buckets, so that on all modern CPUs update operations complete with at most one cache-line transfer. Also, Get operations involve no write to memory, as well as no mutexes or any other sort of locks. Due to this design, in all considered scenarios Map outperforms sync.Map.
One important difference with sync.Map is that only string keys are supported. That's because Golang standard library does not expose the built-in hash functions for interface{} values.
Also note that, unlike in sync.Map, the underlying hash table used by Map never shrinks and only grows on demand. However, this should not be an issue in many cases since updates happen in-place leaving no tombstone entries.
func (*Map) Load ¶ added in v0.0.7
Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
func (*Map) LoadAndDelete ¶ added in v0.0.7
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
func (*Map) LoadOrStore ¶ added in v0.0.7
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
func (*Map) Range ¶ added in v0.0.7
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently, Range may reflect any mapping for that key from any point during the Range call.
It is safe to modify the map while iterating it. However, the concurrent modification rule apply, i.e. the changes may be not reflected in the subsequently iterated entries.
type Mutex ¶ added in v0.0.7
type Mutex struct {
// contains filtered or unexported fields
}
Mutex is a context-aware mutual exclusion lock. Mutexes can be created as part of other structures; the zero value for a Mutex is an unlocked mutex. See https://golang.org/pkg/sync/#Mutex Supports CLock and TryLock.
func (*Mutex) CLock ¶ added in v0.0.7
CLock tries to lock the mutex. If the mutex is is already locked, the calling goroutine blocks until the mutex is available or the context expires. Returns nil if the lock was taken successfully.
func (*Mutex) Lock ¶ added in v0.0.7
func (m *Mutex) Lock()
Lock the mutex. If the mutex is is already locked, the calling goroutine blocks until the mutex is available.
func (*Mutex) TryLock ¶ added in v0.0.7
TryLock tries to lock the mutex. Does not block. If the mutex is is already locked, returns false immediately. Returns true if the lock was taken successfully.
func (*Mutex) Unlock ¶ added in v0.0.7
func (m *Mutex) Unlock()
Unlock the mutex. Does not block. It is a run-time error (panic) if m is not locked on entry to Unlock.
A locked Mutex is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it.
type RToken ¶ added in v0.0.8
type RToken struct {
// contains filtered or unexported fields
}
RToken is a reader lock token.
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
func NewSemaphore ¶
type UnboundedChan ¶ added in v0.0.6
type UnboundedChan struct { // Q *gxqueue.Queue Q *deque.Deque // contains filtered or unexported fields }
refer from redisgo/redis/pool.go
func NewUnboundedChan ¶ added in v0.0.6
func NewUnboundedChan() *UnboundedChan
func (*UnboundedChan) Close ¶ added in v0.0.6
func (q *UnboundedChan) Close()
func (*UnboundedChan) Len ¶ added in v0.0.6
func (q *UnboundedChan) Len() int
func (*UnboundedChan) Pop ¶ added in v0.0.6
func (q *UnboundedChan) Pop() interface{}
func (*UnboundedChan) Push ¶ added in v0.0.6
func (q *UnboundedChan) Push(v interface{})
func (*UnboundedChan) SetWaitOption ¶ added in v0.0.6
func (q *UnboundedChan) SetWaitOption(wait bool)
在pop时,如果没有资源,是否等待 即使用乐观锁还是悲观锁
func (*UnboundedChan) TryPop ¶ added in v0.0.6
func (q *UnboundedChan) TryPop() (interface{}, bool)