mirror of
https://github.com/restic/restic.git
synced 2025-12-20 23:00:24 -05:00
24 lines
449 B
Go
24 lines
449 B
Go
|
|
package backend
|
||
|
|
|
||
|
|
// Semaphore limits access to a restricted resource.
|
||
|
|
type Semaphore struct {
|
||
|
|
ch chan struct{}
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewSemaphore returns a new semaphore with capacity n.
|
||
|
|
func NewSemaphore(n int) *Semaphore {
|
||
|
|
return &Semaphore{
|
||
|
|
ch: make(chan struct{}, n),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetToken blocks until a Token is available.
|
||
|
|
func (s *Semaphore) GetToken() {
|
||
|
|
s.ch <- struct{}{}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ReleaseToken returns a token.
|
||
|
|
func (s *Semaphore) ReleaseToken() {
|
||
|
|
<-s.ch
|
||
|
|
}
|