mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 20:51:43 +00:00
We use the `neelance/parallel` package for two things right now: 1) a semaphore 2) a waitgroup/errgroup It makes for a more-complex-than-necessary semaphore, and a non-standard errgroup. We use it along with the `goroutine` package to log panics, but most of the time, logging a panic is not really what we want. This change was inspired by me getting confused by panics being swallowed in tests. This replaces uses of the package with a combination of `sourcegraph/conc` and a new `internal/limiter`. The new `internal/limiter` package is a very simple channel-based semaphore, and I merged in the `internal/mutablelimiter` package so now we have `limiter.New(n)` and `limiter.NewMutable(n)`. `sourcegraph/conc` replaces the combination of `goroutine.Go()` and `run.Acquire()`/`run.Release()` along with error collection and cancellation in some cases. Additionally, it propagates panics rather than just logging and ignoring them, which is often not good behavior.
22 lines
331 B
Go
22 lines
331 B
Go
package limiter
|
|
|
|
// Limiter is a fixed-sized unweighted semaphore.
|
|
// The zero value is usable and applies no limiting.
|
|
type Limiter chan struct{}
|
|
|
|
func New(n int) Limiter {
|
|
return make(chan struct{}, n)
|
|
}
|
|
|
|
func (l Limiter) Acquire() {
|
|
if l != nil {
|
|
l <- struct{}{}
|
|
}
|
|
}
|
|
|
|
func (l Limiter) Release() {
|
|
if l != nil {
|
|
<-l
|
|
}
|
|
}
|