1
vendor/github.com/zenthangplus/goccm/.gitignore
generated
vendored
Normal file
1
vendor/github.com/zenthangplus/goccm/.gitignore
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.idea
|
||||
16
vendor/github.com/zenthangplus/goccm/.travis.yml
generated
vendored
Normal file
16
vendor/github.com/zenthangplus/goccm/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.x
|
||||
- 1.10.x
|
||||
- 1.11.x
|
||||
- 1.12.x
|
||||
|
||||
before_install:
|
||||
- go get -t -v ./...
|
||||
|
||||
script:
|
||||
- go test -coverprofile=coverage.txt -covermode=atomic
|
||||
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
||||
21
vendor/github.com/zenthangplus/goccm/LICENSE
generated
vendored
Normal file
21
vendor/github.com/zenthangplus/goccm/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2019 ZENTHANG
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
76
vendor/github.com/zenthangplus/goccm/README.md
generated
vendored
Normal file
76
vendor/github.com/zenthangplus/goccm/README.md
generated
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
# Golang Concurrency Manager [](https://travis-ci.com/zenthangplus/goccm)
|
||||
|
||||
Golang Concurrency Manager package limits the number of goroutines that are allowed to run concurrently.
|
||||
|
||||
### Installation
|
||||
|
||||
Run the following command to install this package:
|
||||
|
||||
```
|
||||
$ go get -u github.com/zenthangplus/goccm
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/zenthangplus/goccm"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Limit 3 goroutines to run concurrently.
|
||||
c := goccm.New(3)
|
||||
|
||||
for i := 1; i <= 10; i++ {
|
||||
|
||||
// This function has to call before any goroutine
|
||||
c.Wait()
|
||||
|
||||
go func(i int) {
|
||||
fmt.Printf("Job %d is running\n", i)
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// This function has to when a goroutine has finished
|
||||
// Or you can use `defer c.Done()` at the top of goroutine.
|
||||
c.Done()
|
||||
}(i)
|
||||
}
|
||||
|
||||
// This function has to call to ensure all goroutines have finished
|
||||
// after close the main program.
|
||||
c.WaitAllDone()
|
||||
}
|
||||
```
|
||||
|
||||
### List of supported functions
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "github.com/zenthangplus/goccm"
|
||||
|
||||
func main() {
|
||||
// Create the concurrency manager
|
||||
// The first argument is the maximum number of goroutines to run concurrently.
|
||||
c := goccm.New(10)
|
||||
|
||||
// Wait until a slot is available for the new goroutine.
|
||||
c.Wait()
|
||||
|
||||
// Mark a goroutine as finished
|
||||
c.Done()
|
||||
|
||||
// Wait for all goroutines are done
|
||||
c.WaitAllDone()
|
||||
|
||||
// Close the manager manually
|
||||
c.Close()
|
||||
|
||||
// Returns the number of goroutines which are running
|
||||
c.RunningCount()
|
||||
}
|
||||
```
|
||||
124
vendor/github.com/zenthangplus/goccm/goccm.go
generated
vendored
Normal file
124
vendor/github.com/zenthangplus/goccm/goccm.go
generated
vendored
Normal file
@@ -0,0 +1,124 @@
|
||||
package goccm
|
||||
|
||||
import "sync/atomic"
|
||||
|
||||
type (
|
||||
// ConcurrencyManager Interface
|
||||
ConcurrencyManager interface {
|
||||
// Wait until a slot is available for the new goroutine.
|
||||
Wait()
|
||||
|
||||
// Mark a goroutine as finished
|
||||
Done()
|
||||
|
||||
// Close the manager manually
|
||||
Close()
|
||||
|
||||
// Wait for all goroutines are done
|
||||
WaitAllDone()
|
||||
|
||||
// Returns the number of goroutines which are running
|
||||
RunningCount() int32
|
||||
}
|
||||
|
||||
concurrencyManager struct {
|
||||
// The number of goroutines that are allowed to run concurrently
|
||||
max int
|
||||
|
||||
// The manager channel to coordinate the number of concurrent goroutines.
|
||||
managerCh chan interface{}
|
||||
|
||||
// The done channel indicates when a single goroutine has finished its job.
|
||||
doneCh chan bool
|
||||
|
||||
// This channel indicates when all goroutines have finished their job.
|
||||
allDoneCh chan bool
|
||||
|
||||
// The close flag allows we know when we can close the manager
|
||||
closed bool
|
||||
|
||||
// The running count allows we know the number of goroutines are running
|
||||
runningCount int32
|
||||
}
|
||||
)
|
||||
|
||||
// New concurrencyManager
|
||||
func New(maxGoRoutines int) *concurrencyManager {
|
||||
// Initiate the manager object
|
||||
c := concurrencyManager{
|
||||
max: maxGoRoutines,
|
||||
managerCh: make(chan interface{}, maxGoRoutines),
|
||||
doneCh: make(chan bool),
|
||||
allDoneCh: make(chan bool),
|
||||
}
|
||||
|
||||
// Fill the manager channel by placeholder values
|
||||
for i := 0; i < c.max; i++ {
|
||||
c.managerCh <- nil
|
||||
}
|
||||
|
||||
// Start the controller to collect all the jobs
|
||||
go c.controller()
|
||||
|
||||
return &c
|
||||
}
|
||||
|
||||
// Create the controller to collect all the jobs.
|
||||
// When a goroutine is finished, we can release a slot for another goroutine.
|
||||
func (c *concurrencyManager) controller() {
|
||||
for {
|
||||
// This will block until a goroutine is finished
|
||||
<-c.doneCh
|
||||
|
||||
// Say that another goroutine can now start
|
||||
c.managerCh <- nil
|
||||
|
||||
// When the closed flag is set,
|
||||
// we need to close the manager if it doesn't have any running goroutine
|
||||
if c.closed == true && c.runningCount == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Say that all goroutines are finished, we can close the manager
|
||||
c.allDoneCh <- true
|
||||
}
|
||||
|
||||
// Wait until a slot is available for the new goroutine.
|
||||
// A goroutine have to start after this function.
|
||||
func (c *concurrencyManager) Wait() {
|
||||
|
||||
// Try to receive from the manager channel. When we have something,
|
||||
// it means a slot is available and we can start a new goroutine.
|
||||
// Otherwise, it will block until a slot is available.
|
||||
<-c.managerCh
|
||||
|
||||
// Increase the running count to help we know how many goroutines are running.
|
||||
atomic.AddInt32(&c.runningCount, 1)
|
||||
}
|
||||
|
||||
// Mark a goroutine as finished
|
||||
func (c *concurrencyManager) Done() {
|
||||
// Decrease the number of running count
|
||||
atomic.AddInt32(&c.runningCount, -1)
|
||||
c.doneCh <- true
|
||||
}
|
||||
|
||||
// Close the manager manually
|
||||
func (c *concurrencyManager) Close() {
|
||||
c.closed = true
|
||||
}
|
||||
|
||||
// Wait for all goroutines are done
|
||||
func (c *concurrencyManager) WaitAllDone() {
|
||||
// Close the manager automatic
|
||||
c.Close()
|
||||
|
||||
// This will block until allDoneCh was marked
|
||||
<-c.allDoneCh
|
||||
}
|
||||
|
||||
// Returns the number of goroutines which are running
|
||||
func (c *concurrencyManager) RunningCount() int32 {
|
||||
return c.runningCount
|
||||
}
|
||||
Reference in New Issue
Block a user