mirror of
https://github.com/restic/restic.git
synced 2025-08-23 13:08:08 +00:00
local/sftp: move limiter setup into backend
This commit is contained in:
@@ -7,6 +7,21 @@ import (
|
||||
"github.com/restic/restic/internal/restic"
|
||||
)
|
||||
|
||||
func WrapBackendConstructor[B restic.Backend, C any](constructor func(ctx context.Context, cfg C) (B, error)) func(ctx context.Context, cfg C, lim Limiter) (restic.Backend, error) {
|
||||
return func(ctx context.Context, cfg C, lim Limiter) (restic.Backend, error) {
|
||||
var be restic.Backend
|
||||
be, err := constructor(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if lim != nil {
|
||||
be = LimitBackend(be, lim)
|
||||
}
|
||||
return be, nil
|
||||
}
|
||||
}
|
||||
|
||||
// LimitBackend wraps a Backend and applies rate limiting to Load() and Save()
|
||||
// calls on the backend.
|
||||
func LimitBackend(be restic.Backend, l Limiter) restic.Backend {
|
||||
|
@@ -31,11 +31,7 @@ type Local struct {
|
||||
var _ restic.Backend = &Local{}
|
||||
|
||||
func NewFactory() location.Factory {
|
||||
return location.NewLimitedBackendFactory(ParseConfig, location.NoPassword, func(ctx context.Context, cfg Config, _ limiter.Limiter) (*Local, error) {
|
||||
return Create(ctx, cfg)
|
||||
}, func(ctx context.Context, cfg Config, _ limiter.Limiter) (*Local, error) {
|
||||
return Open(ctx, cfg)
|
||||
})
|
||||
return location.NewLimitedBackendFactory(ParseConfig, location.NoPassword, limiter.WrapBackendConstructor(Create), limiter.WrapBackendConstructor(Open))
|
||||
}
|
||||
|
||||
const defaultLayout = "default"
|
||||
|
@@ -44,11 +44,7 @@ type SFTP struct {
|
||||
var _ restic.Backend = &SFTP{}
|
||||
|
||||
func NewFactory() location.Factory {
|
||||
return location.NewLimitedBackendFactory(ParseConfig, location.NoPassword, func(ctx context.Context, cfg Config, _ limiter.Limiter) (*SFTP, error) {
|
||||
return Create(ctx, cfg)
|
||||
}, func(ctx context.Context, cfg Config, _ limiter.Limiter) (*SFTP, error) {
|
||||
return Open(ctx, cfg)
|
||||
})
|
||||
return location.NewLimitedBackendFactory(ParseConfig, location.NoPassword, limiter.WrapBackendConstructor(Create), limiter.WrapBackendConstructor(Open))
|
||||
}
|
||||
|
||||
const defaultLayout = "default"
|
||||
|
Reference in New Issue
Block a user