Files
automana/client/concurrencylimit.go

35 lines
586 B
Go
Raw Normal View History

2021-09-26 04:21:03 +00:00
package client
import "context"
import "golang.org/x/sync/semaphore"
type ConcurrencyLimit struct {
sem *semaphore.Weighted
}
func NewConcurrencyLimit(limit int64) *ConcurrencyLimit {
return &ConcurrencyLimit{
sem: semaphore.NewWeighted(limit),
}
}
func (cl *ConcurrencyLimit) Acquire1() {
cl.AcquireN(1)
}
func (cl *ConcurrencyLimit) AcquireN(cost int64) {
err := cl.sem.Acquire(context.TODO(), cost)
if err != nil {
panic(err)
}
}
func (cl *ConcurrencyLimit) Release1() {
cl.ReleaseN(1)
}
func (cl *ConcurrencyLimit) ReleaseN(cost int64) {
cl.sem.Release(cost)
}