diff --git a/src/restic/backend/b2/b2.go b/src/restic/backend/b2/b2.go index c209c13ab..8fe18bcd9 100644 --- a/src/restic/backend/b2/b2.go +++ b/src/restic/backend/b2/b2.go @@ -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}) diff --git a/src/restic/backend/b2/config.go b/src/restic/backend/b2/config.go index 221e4ff02..922605636 100644 --- a/src/restic/backend/b2/config.go +++ b/src/restic/backend/b2/config.go @@ -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. diff --git a/src/restic/backend/semaphore.go b/src/restic/backend/semaphore.go index dbbd72966..4bb1ad3ab 100644 --- a/src/restic/backend/semaphore.go +++ b/src/restic/backend/semaphore.go @@ -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.