func (s *Semaphore) Release() {
	s.rMutex.Lock()
	defer s.rMutex.Unlock()
	s.channel <- struct{}{}
	s.pMutex.Lock()
	s.avail++
	s.pMutex.Unlock()
}
Release releases one permit.
package semaphore
import (
	"context"
	"sync"
	"time"
)
type Semaphore struct {
	permits int
	avail   int
	channel chan struct{}
	aMutex  sync.Mutex    
	rMutex  sync.Mutex    
	pMutex  sync.RWMutex  
}
func New(permits int) *Semaphore {
	if permits < 1 {
		panic("Invalid number of permits. Less than 1")
	}
	channel := make(chan struct{}, permits)
	for i := 0; i < permits; i++ {
		channel <- struct{}{}
	}
	return &Semaphore{
		permits: permits,
		avail:   permits,
		channel: channel,
	}
}
func (s *Semaphore) Acquire() {
	s.aMutex.Lock()
	defer s.aMutex.Unlock()
	s.pMutex.Lock()
	s.avail--
	s.pMutex.Unlock()
	<-s.channel
}
func (s *Semaphore) AcquireMany(n int) {
	if n > s.permits {
		n = s.permits
	}
	for ; n > 0; n-- {
		s.Acquire()
	}
}
func (s *Semaphore) AcquireContext(ctx context.Context, n int) bool {
	acquired := make(chan struct{}, 1)
	reverse := make(chan bool, 1)
	go func() {
		s.AcquireMany(n)
		acquired <- struct{}{}
		if <-reverse {
			s.ReleaseMany(n)
		}
		close(acquired)
		close(reverse)
	}()
	select {
	case <-ctx.Done():
		reverse <- true
		return false
	case <-acquired:
		reverse <- false
		return true
	}
}
func (s *Semaphore) AcquireWithin(n int, d time.Duration) bool {
	ctx, cancel := context.WithTimeout(context.Background(), d)
	defer cancel()
	return s.AcquireContext(ctx, n)
}