Documentation ¶
Overview ¶
Package semaphore provides a semaphore/queuing implementation based on https://pauladamsmith.com/blog/2016/04/max-clients-go-net-http.html
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Example ¶
package main import ( "fmt" "time" "github.com/jamesrr39/semaphore" ) func main() { s := semaphore.NewSemaphore(4) for i := 0; i < 22; i++ { s.Add() go func(i int) { defer func() { s.Done() fmt.Printf("finished %d\n", i) }() fmt.Printf("running: %d\n", i) time.Sleep(time.Second * time.Duration(i)) }(i) } s.Wait() }
Output:
Example (WithErrorHandling) ¶
ExampleSemaphoreWithErrorHandling shows an example with some error handling that avoids doing the hard work if there has already been an error
package main import ( "fmt" "os" "github.com/jamesrr39/semaphore" ) func main() { s := semaphore.NewSemaphore(4) var outerErr error for i := 0; i < 3; i++ { s.Add() go func(i int) { defer s.Done() if outerErr != nil { // skip the hard work below if there has previously been an error return } file, err := os.Open(fmt.Sprintf("myfile%d.txt", i)) if err != nil { outerErr = err return } defer file.Close() // do some hard work with the file here... }(i) } s.Wait() if outerErr != nil { // handle error here } }
Output:
func NewSemaphore ¶
func (*Semaphore) CurrentlyRunning ¶
Click to show internal directories.
Click to hide internal directories.