parent
081e75146c
commit
b96019be7c
@ -0,0 +1,26 @@
|
||||
package retry
|
||||
|
||||
import "time"
|
||||
|
||||
// Backoff used for retry sleep backoff
|
||||
type Backoff interface {
|
||||
Next() bool
|
||||
}
|
||||
|
||||
// ConstantBackoff implements Backoff interface with constant sleep time
|
||||
type ConstantBackoff struct {
|
||||
Sleep time.Duration
|
||||
Max int
|
||||
|
||||
tried int
|
||||
}
|
||||
|
||||
func (c ConstantBackoff) Next() bool {
|
||||
c.tried++
|
||||
if c.tried >= c.Max {
|
||||
return false
|
||||
}
|
||||
|
||||
time.Sleep(c.Sleep)
|
||||
return true
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package retry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||
"io"
|
||||
)
|
||||
|
||||
type ChunkProcessFunc func(index int, chunk io.Reader) error
|
||||
|
||||
func Chunk(index int, chunkSize uint64, file fsctx.FileHeader, processor ChunkProcessFunc, backoff Backoff) error {
|
||||
err := processor(index, file)
|
||||
if err != nil {
|
||||
if err != context.Canceled && file.Seekable() && backoff.Next() {
|
||||
if _, seekErr := file.Seek(int64(uint64(index)*chunkSize), io.SeekStart); err != nil {
|
||||
return fmt.Errorf("failed to seek back to chunk start: %w, last error: %w", seekErr, err)
|
||||
}
|
||||
|
||||
util.Log().Debug("Retrying chunk %d, last error: %s", index, err)
|
||||
return Chunk(index, chunkSize, file, processor, backoff)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in new issue