### Worker Pool Example with Semaphore Source: https://pkg.go.dev/golang.org/x/sync/semaphore Demonstrates limiting goroutines for parallel tasks using a semaphore, mimicking a worker pool pattern without explicit shutdown of idle workers. ```go Output: [0 1 7 2 5 8 16 3 19 6 14 9 9 17 17 4 12 20 20 7 7 15 15 10 23 10 111 18 18 18 106 5] ``` -------------------------------- ### Group TryGo Method Source: https://pkg.go.dev/golang.org/x/sync/errgroup Attempts to start a new goroutine only if the active goroutine count is below the limit. Returns true if the goroutine was started, false otherwise. ```go func (g *Group) TryGo(f func() error) bool ``` -------------------------------- ### Group Go Method Source: https://pkg.go.dev/golang.org/x/sync/errgroup Starts a new goroutine within the group. It blocks if the number of active goroutines exceeds the limit. The first error returned by a goroutine cancels the Context. ```go func (g *Group) Go(f func() error) ``` -------------------------------- ### Group SetLimit Method Source: https://pkg.go.dev/golang.org/x/sync/errgroup Limits the number of active goroutines in the group. A negative value means no limit. Setting a limit of zero prevents new goroutines from starting. This method should not be called while goroutines are active. ```go func (g *Group) SetLimit(n int) ``` -------------------------------- ### Try Acquire Semaphore with Weight Source: https://pkg.go.dev/golang.org/x/sync/semaphore Attempts to acquire the semaphore with a given weight without blocking. Returns true on success and false if resources are not immediately available. ```go func (s *Weighted) TryAcquire(n int64) bool ``` -------------------------------- ### Acquire Semaphore with Weight Source: https://pkg.go.dev/golang.org/x/sync/semaphore Acquires the semaphore with a given weight, blocking until resources are available or the context is done. Returns an error if acquisition fails due to context cancellation. ```go func (s *Weighted) Acquire(ctx context.Context, n int64) error ``` -------------------------------- ### Group.DoChan Method Source: https://pkg.go.dev/golang.org/x/sync/singleflight Similar to Do, but returns a channel that receives the results when they are ready. The channel is not closed. ```APIDOC ## func (*Group) DoChan ```go func (g *Group) DoChan(key string, fn func() (any, error)) <-chan Result ``` DoChan is like Do but returns a channel that will receive the results when they are ready. The returned channel will not be closed. ``` -------------------------------- ### Do Method Signature Source: https://pkg.go.dev/golang.org/x/sync/singleflight The Do method executes a function for a given key, ensuring only one execution is active. Subsequent calls with the same key wait for the original to complete and receive the same results. The 'shared' return value indicates if the result was provided to multiple callers. ```go func (g *Group) Do(key string, fn func() (any, error)) (v any, err error, shared bool) ``` -------------------------------- ### DoChan Method Signature Source: https://pkg.go.dev/golang.org/x/sync/singleflight DoChan is similar to Do but returns a channel that receives the results when they are ready. The returned channel is never closed. ```go func (g *Group) DoChan(key string, fn func() (any, error)) <-chan Result ``` -------------------------------- ### Release Semaphore with Weight Source: https://pkg.go.dev/golang.org/x/sync/semaphore Releases the semaphore, adding a specified weight back to the available resources. ```go func (s *Weighted) Release(n int64) ``` -------------------------------- ### Group WithContext Function Source: https://pkg.go.dev/golang.org/x/sync/errgroup Creates a new Group and an associated Context. The Context is canceled when the first goroutine returns an error or when Wait is called. ```go func WithContext(ctx context.Context) (*Group, context.Context) ``` -------------------------------- ### Create New Weighted Semaphore Source: https://pkg.go.dev/golang.org/x/sync/semaphore Creates a new weighted semaphore with a specified maximum combined weight for concurrent access. ```go func NewWeighted(n int64) *Weighted ``` -------------------------------- ### Weighted Semaphore API Source: https://pkg.go.dev/golang.org/x/sync/semaphore Methods for creating and managing a weighted semaphore to control concurrent access. ```APIDOC ## func NewWeighted ### Description Creates a new weighted semaphore with the given maximum combined weight for concurrent access. ### Parameters - **n** (int64) - Required - The maximum weight capacity of the semaphore. ### Response - **Weighted** (struct) - A pointer to the initialized Weighted semaphore instance. --- ## func (*Weighted) Acquire ### Description Acquires the semaphore with a weight of n, blocking until resources are available or the context is done. ### Parameters - **ctx** (context.Context) - Required - The context to handle cancellation or timeout. - **n** (int64) - Required - The weight to acquire. ### Response - **error** - Returns nil on success, or ctx.Err() on failure. --- ## func (*Weighted) Release ### Description Releases the semaphore with a weight of n. ### Parameters - **n** (int64) - Required - The weight to release. --- ## func (*Weighted) TryAcquire ### Description Acquires the semaphore with a weight of n without blocking. ### Parameters - **n** (int64) - Required - The weight to acquire. ### Response - **bool** - Returns true on success, false on failure. ``` -------------------------------- ### Group Wait Method Source: https://pkg.go.dev/golang.org/x/sync/errgroup Blocks until all goroutines in the group have finished. Returns the first non-nil error encountered, or nil if all goroutines completed successfully. ```go func (g *Group) Wait() error ``` -------------------------------- ### Forget Method Signature Source: https://pkg.go.dev/golang.org/x/sync/singleflight Forget instructs the singleflight Group to stop tracking a given key. Future calls to Do for this key will execute the function directly instead of waiting for a prior call to finish. ```go func (g *Group) Forget(key string) ``` -------------------------------- ### Group.Do Method Source: https://pkg.go.dev/golang.org/x/sync/singleflight Executes a function with duplicate suppression. If another call with the same key is in progress, it waits and returns the same result. ```APIDOC ## func (*Group) Do ```go func (g *Group) Do(key string, fn func() (any, error)) (v any, err error, shared bool) ``` Do executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results. The return value shared indicates whether v was given to multiple callers. ``` -------------------------------- ### Errgroup Group Type Source: https://pkg.go.dev/golang.org/x/sync/errgroup Documentation for the Group type and its associated methods. ```APIDOC ## Type: Group A Group is a collection of goroutines working on subtasks that are part of the same overall task. A Group should not be reused for different tasks. A zero Group is valid, has no limit on the number of active goroutines, and does not cancel on error. ### Methods #### func WithContext(ctx context.Context) (*Group, context.Context) WithContext returns a new Group and an associated Context derived from ctx. The derived Context is canceled the first time a function passed to Go returns a non-nil error or the first time Wait returns, whichever occurs first. #### func (g *Group) Go(f func() error) Go calls the given function in a new goroutine. The first call to Go must happen before a Wait. It blocks until the new goroutine can be added without the number of goroutines in the group exceeding the configured limit. The first goroutine in the group that returns a non-nil error will cancel the associated Context, if any. The error will be returned by Wait. #### func (g *Group) SetLimit(n int) SetLimit limits the number of active goroutines in this group to at most n. A negative value indicates no limit. A limit of zero will prevent any new goroutines from being added. Any subsequent call to the Go method will block until it can add an active goroutine without exceeding the configured limit. The limit must not be modified while any goroutines in the group are active. #### func (g *Group) TryGo(f func() error) bool TryGo calls the given function in a new goroutine only if the number of active goroutines in the group is currently below the configured limit. The return value reports whether the goroutine was started. #### func (g *Group) Wait() error Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them. ``` -------------------------------- ### Define Weighted Semaphore Structure Source: https://pkg.go.dev/golang.org/x/sync/semaphore Defines the Weighted semaphore structure used for bounding concurrent access to a resource. ```go type Weighted struct { // contains filtered or unexported fields } ``` -------------------------------- ### Group Type Definition Source: https://pkg.go.dev/golang.org/x/sync/errgroup Defines the Group type, which is a collection of goroutines for subtasks. A zero Group is valid and has no limit on active goroutines. ```go type Group struct { // contains filtered or unexported fields } ``` -------------------------------- ### Type Map Source: https://pkg.go.dev/golang.org/x/sync/syncmap Definition of the Map type, which is an alias for sync.Map. ```APIDOC ## type Map ### Description Map is a concurrent map with amortized-constant-time loads, stores, and deletes. It is safe for multiple goroutines to call a Map's methods concurrently. ### Definition ```go type Map = sync.Map ``` ### Usage Notes - The zero Map is valid and empty. - A Map must not be copied after first use. ``` -------------------------------- ### Result Type Source: https://pkg.go.dev/golang.org/x/sync/singleflight Holds the results of a singleflight operation, including the value, error, and whether the result was shared. ```APIDOC ## type Result ```go type Result struct { Val any Err error Shared bool } ``` Result holds the results of Do, so they can be passed on a channel. ``` -------------------------------- ### Result Type Definition Source: https://pkg.go.dev/golang.org/x/sync/singleflight The Result struct holds the value, error, and shared status returned by the Do method, intended for use with channels. ```go type Result struct { Val any Err error Shared bool } ``` -------------------------------- ### Group.Forget Method Source: https://pkg.go.dev/golang.org/x/sync/singleflight Removes a key from the Group's tracking, allowing future calls for that key to execute the function directly. ```APIDOC ## func (*Group) Forget ```go func (g *Group) Forget(key string) ``` Forget tells the singleflight to forget about a key. Future calls to Do for this key will call the function rather than waiting for an earlier call to complete. ``` -------------------------------- ### Group Type Source: https://pkg.go.dev/golang.org/x/sync/singleflight The Group type represents a namespace for units of work, enabling duplicate suppression. ```APIDOC ## type Group ```go type Group struct { // contains filtered or unexported fields } ``` Group represents a class of work and forms a namespace in which units of work can be executed with duplicate suppression. ### Example ``` Output: Shared: true Equal results: true Result: func 1 ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.