mirror of
https://github.com/restic/restic.git
synced 2025-08-12 11:17:42 +00:00
backend: Improve Save()
As mentioned in issue [#1560](https://github.com/restic/restic/pull/1560#issuecomment-364689346) this changes the signature for `backend.Save()`. It now takes a parameter of interface type `RewindReader`, so that the backend implementations or our `RetryBackend` middleware can reset the reader to the beginning and then retry an upload operation. The `RewindReader` interface also provides a `Length()` method, which is used in the backend to get the size of the data to be saved. This removes several ugly hacks we had to do to pull the size back out of the `io.Reader` passed to `Save()` before. In the `s3` and `rest` backend this is actively used.
This commit is contained in:
@@ -21,8 +21,23 @@ type rateLimitedBackend struct {
|
||||
limiter Limiter
|
||||
}
|
||||
|
||||
func (r rateLimitedBackend) Save(ctx context.Context, h restic.Handle, rd io.Reader) error {
|
||||
return r.Backend.Save(ctx, h, r.limiter.Upstream(rd))
|
||||
func (r rateLimitedBackend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
|
||||
limited := limitedRewindReader{
|
||||
RewindReader: rd,
|
||||
limited: r.limiter.Upstream(rd),
|
||||
}
|
||||
|
||||
return r.Backend.Save(ctx, h, limited)
|
||||
}
|
||||
|
||||
type limitedRewindReader struct {
|
||||
restic.RewindReader
|
||||
|
||||
limited io.Reader
|
||||
}
|
||||
|
||||
func (l limitedRewindReader) Read(b []byte) (int, error) {
|
||||
return l.limited.Read(b)
|
||||
}
|
||||
|
||||
func (r rateLimitedBackend) Load(ctx context.Context, h restic.Handle, length int, offset int64, consumer func(rd io.Reader) error) error {
|
||||
|
Reference in New Issue
Block a user