summaryrefslogtreecommitdiff
path: root/cmd/podman/shared/workers.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2019-04-09 17:46:28 -0700
committerGitHub <noreply@github.com>2019-04-09 17:46:28 -0700
commit60ef8f8da90ec5200e62b79b24324fefde2c7036 (patch)
treeec403dcd9b0b80d188e400a18d432539b67faf28 /cmd/podman/shared/workers.go
parent40a1df38d1b6971dd1df9e06bf190c924dbad5a3 (diff)
parent09ff62429a324e01ad2c584afe9a5f66f580ae78 (diff)
downloadpodman-60ef8f8da90ec5200e62b79b24324fefde2c7036.tar.gz
podman-60ef8f8da90ec5200e62b79b24324fefde2c7036.tar.bz2
podman-60ef8f8da90ec5200e62b79b24324fefde2c7036.zip
Merge pull request #2663 from jwhonce/wip/remote_umount
Implement podman-remote umount and rm command
Diffstat (limited to 'cmd/podman/shared/workers.go')
-rw-r--r--cmd/podman/shared/workers.go133
1 files changed, 133 insertions, 0 deletions
diff --git a/cmd/podman/shared/workers.go b/cmd/podman/shared/workers.go
new file mode 100644
index 000000000..112af89cc
--- /dev/null
+++ b/cmd/podman/shared/workers.go
@@ -0,0 +1,133 @@
+package shared
+
+import (
+ "reflect"
+ "runtime"
+ "strings"
+ "sync"
+
+ "github.com/sirupsen/logrus"
+)
+
+// JobFunc provides the function signature for the pool'ed functions
+type JobFunc func() error
+
+// Job defines the function to run
+type Job struct {
+ ID string
+ Fn JobFunc
+}
+
+// JobResult defines the results from the function ran
+type JobResult struct {
+ Job Job
+ Err error
+}
+
+// Pool defines the worker pool and queues
+type Pool struct {
+ id string
+ wg *sync.WaitGroup
+ jobs chan Job
+ results chan JobResult
+ size int
+ capacity int
+}
+
+// NewPool creates and initializes a new Pool
+func NewPool(id string, size int, capacity int) *Pool {
+ var wg sync.WaitGroup
+
+ // min for int...
+ s := size
+ if s > capacity {
+ s = capacity
+ }
+
+ return &Pool{
+ id,
+ &wg,
+ make(chan Job, capacity),
+ make(chan JobResult, capacity),
+ s,
+ capacity,
+ }
+}
+
+// Add Job to pool for parallel processing
+func (p *Pool) Add(job Job) {
+ p.wg.Add(1)
+ p.jobs <- job
+}
+
+// Run the Job's in the pool, gather and return results
+func (p *Pool) Run() ([]string, map[string]error, error) {
+ var (
+ ok = []string{}
+ failures = map[string]error{}
+ )
+
+ for w := 0; w < p.size; w++ {
+ w := w
+ go p.newWorker(w)
+ }
+ close(p.jobs)
+ p.wg.Wait()
+
+ close(p.results)
+ for r := range p.results {
+ if r.Err == nil {
+ ok = append(ok, r.Job.ID)
+ } else {
+ failures[r.Job.ID] = r.Err
+ }
+ }
+
+ if logrus.GetLevel() == logrus.DebugLevel {
+ for i, f := range failures {
+ logrus.Debugf("Pool[%s, %s: %s]", p.id, i, f.Error())
+ }
+ }
+
+ return ok, failures, nil
+}
+
+// newWorker creates new parallel workers to monitor jobs channel from Pool
+func (p *Pool) newWorker(slot int) {
+ for job := range p.jobs {
+ err := job.Fn()
+ p.results <- JobResult{job, err}
+ if logrus.GetLevel() == logrus.DebugLevel {
+ n := strings.Split(runtime.FuncForPC(reflect.ValueOf(job.Fn).Pointer()).Name(), ".")
+ logrus.Debugf("Worker#%d finished job %s/%s (%v)", slot, n[2:], job.ID, err)
+ }
+ p.wg.Done()
+ }
+}
+
+// DefaultPoolSize provides the maximum number of parallel workers (int) as calculated by a basic
+// heuristic. This can be overriden by the --max-workers primary switch to podman.
+func DefaultPoolSize(name string) int {
+ numCpus := runtime.NumCPU()
+ switch name {
+ case "kill":
+ case "pause":
+ case "rm":
+ case "unpause":
+ if numCpus <= 3 {
+ return numCpus * 3
+ }
+ return numCpus * 4
+ case "ps":
+ return 8
+ case "restart":
+ return numCpus * 2
+ case "stop":
+ if numCpus <= 2 {
+ return 4
+ } else {
+ return numCpus * 3
+ }
+ }
+ return 3
+}