Documentation
¶
Overview ¶
Package concurrent provides common concurrent safe types.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache provides a very simple in-memory cache which is based simply on a map and sync.Mutex.
func (*Cache[K, V]) Get ¶
Get retrieves the value for the given key. If the key does not exist in the cache, then the zero value for V will be returned along with false.
type LazyGroup ¶
type LazyGroup struct {
// contains filtered or unexported fields
}
LazyGroup is a collection of goroutines. The goroutines are not created until LazyGroup.Wait is called. A zero LazyGroup is valid, has no limit on the number of active goroutines.
Example ¶
var computeGroup LazyGroup partialSumCh := make(chan int) for i := range 10 { computeGroup.Go(func(ctx context.Context) error { var total int for j := range 100000 { total += (j + 1) + i*100000 } select { case <-ctx.Done(): return ctx.Err() case partialSumCh <- total: } return nil }) } var sumGroup LazyGroup sumGroup.Go(func(ctx context.Context) error { defer close(partialSumCh) return computeGroup.Wait(ctx) }) sumGroup.Go(func(ctx context.Context) error { var total int for { select { case <-ctx.Done(): return ctx.Err() case partialSum, ok := <-partialSumCh: if !ok { fmt.Println(total) return nil } total += partialSum } } }) err := sumGroup.Wait(context.Background()) if err != nil { fmt.Println(err) return }
Output: 500000500000
func (*LazyGroup) Go ¶
Go registers the given func to be ran on its own goroutine once LazyGroup.Wait is called.
Click to show internal directories.
Click to hide internal directories.