2017-05-28 08:19:01 +00:00
|
|
|
package backend
|
|
|
|
|
2017-11-02 16:38:17 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"io"
|
|
|
|
)
|
2017-06-05 22:17:21 +00:00
|
|
|
|
2017-05-28 08:19:01 +00:00
|
|
|
// Semaphore limits access to a restricted resource.
|
|
|
|
type Semaphore struct {
|
|
|
|
ch chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSemaphore returns a new semaphore with capacity n.
|
2017-06-05 22:17:21 +00:00
|
|
|
func NewSemaphore(n uint) (*Semaphore, error) {
|
|
|
|
if n <= 0 {
|
|
|
|
return nil, errors.New("must be a positive number")
|
|
|
|
}
|
2017-05-28 08:19:01 +00:00
|
|
|
return &Semaphore{
|
|
|
|
ch: make(chan struct{}, n),
|
2017-06-05 22:17:21 +00:00
|
|
|
}, nil
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetToken blocks until a Token is available.
|
|
|
|
func (s *Semaphore) GetToken() {
|
|
|
|
s.ch <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseToken returns a token.
|
|
|
|
func (s *Semaphore) ReleaseToken() {
|
|
|
|
<-s.ch
|
|
|
|
}
|
2017-11-02 16:38:17 +00:00
|
|
|
|
|
|
|
// ReleaseTokenOnClose wraps an io.ReadCloser to return a token on Close. Before returning the token,
|
|
|
|
// cancel, if provided, will be run to free up context resources.
|
|
|
|
func (s *Semaphore) ReleaseTokenOnClose(rc io.ReadCloser, cancel context.CancelFunc) io.ReadCloser {
|
|
|
|
return &wrapReader{rc, false, func() {
|
|
|
|
if cancel != nil {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
s.ReleaseToken()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrapReader wraps an io.ReadCloser to run an additional function on Close.
|
|
|
|
type wrapReader struct {
|
|
|
|
io.ReadCloser
|
|
|
|
eofSeen bool
|
|
|
|
f func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wr *wrapReader) Read(p []byte) (int, error) {
|
|
|
|
if wr.eofSeen {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := wr.ReadCloser.Read(p)
|
|
|
|
if err == io.EOF {
|
|
|
|
wr.eofSeen = true
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wr *wrapReader) Close() error {
|
|
|
|
err := wr.ReadCloser.Close()
|
|
|
|
wr.f()
|
|
|
|
return err
|
|
|
|
}
|