2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 14:56:29 +00:00

Add error handling to semaphore

This commit is contained in:
Alexander Neumann 2017-06-06 00:17:21 +02:00
parent a9a2798910
commit 5010e95c23
3 changed files with 20 additions and 5 deletions

View File

@ -50,6 +50,11 @@ func Open(cfg Config) (restic.Backend, error) {
return nil, errors.Wrap(err, "Bucket")
}
sem, err := backend.NewSemaphore(cfg.Connections)
if err != nil {
return nil, err
}
be := &b2Backend{
client: client,
bucket: bucket,
@ -58,7 +63,7 @@ func Open(cfg Config) (restic.Backend, error) {
Join: path.Join,
Path: cfg.Prefix,
},
sem: backend.NewSemaphore(cfg.Connections),
sem: sem,
}
return be, nil
@ -85,6 +90,11 @@ func Create(cfg Config) (restic.Backend, error) {
return nil, errors.Wrap(err, "NewBucket")
}
sem, err := backend.NewSemaphore(cfg.Connections)
if err != nil {
return nil, err
}
be := &b2Backend{
client: client,
bucket: bucket,
@ -93,7 +103,7 @@ func Create(cfg Config) (restic.Backend, error) {
Join: path.Join,
Path: cfg.Prefix,
},
sem: backend.NewSemaphore(cfg.Connections),
sem: sem,
}
present, err := be.Test(restic.Handle{Type: restic.ConfigFile})

View File

@ -17,7 +17,7 @@ type Config struct {
Bucket string
Prefix string
Connections int `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"`
Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"`
}
// NewConfig returns a new config with default options applied.

View File

@ -1,15 +1,20 @@
package backend
import "restic/errors"
// 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 {
func NewSemaphore(n uint) (*Semaphore, error) {
if n <= 0 {
return nil, errors.New("must be a positive number")
}
return &Semaphore{
ch: make(chan struct{}, n),
}
}, nil
}
// GetToken blocks until a Token is available.