MCPcopy
hub / github.com/kopia/kopia / NewPool

Function NewPool

internal/workshare/workshare_pool.go:37–69  ·  view source on GitHub ↗

NewPool creates a worker pool that launches a given number of goroutines that can invoke shared work.

(numWorkers int)

Source from the content-addressed store, hash-verified

35
36// NewPool creates a worker pool that launches a given number of goroutines that can invoke shared work.
37func NewPool[T any](numWorkers int) *Pool[T] {
38 if numWorkers < 0 {
39 numWorkers = 0
40 }
41
42 w := &Pool[T]{
43 // channel must be unbuffered so that it has exactly as many slots as there are goroutines capable of reading from it
44 // this way by pushing to the channel we can be sure that a pre-spun goroutine will pick it up soon.
45 work: make(chan workItem[T]),
46 closed: make(chan struct{}),
47 semaphore: make(chan struct{}, numWorkers),
48 }
49
50 for range numWorkers {
51 w.wg.Go(func() {
52 for {
53 select {
54 case it := <-w.work:
55 w.activeWorkers.Add(1)
56 it.process(w, it.request)
57 w.activeWorkers.Add(-1)
58 <-w.semaphore
59 it.wg.Done()
60
61 case <-w.closed:
62 return
63 }
64 }
65 })
66 }
67
68 return w
69}
70
71// Close closes the worker pool.
72func (w *Pool[T]) Close() {

Callers

nothing calls this directly

Calls 2

DoneMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected