sourcegraph/internal/limiter/limiter.go
Camden Cheek 0c42579d07
Consolidate dependencies: remove neelance/parallel (#48159)
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.
2023-02-24 11:24:46 -07:00

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
}
}