mirror of
https://github.com/hanzoai/s3-cli.git
synced 2026-10-30 18:23:12 +00:00
Vendor the CLI helpers into pkg/, use hanzos3/go, rename the module to hanzoai/s3-cli, and gate the names in cmd/names_test.go. Claude-Session: https://claude.ai/code/session_011i2AG9nwpiKUCVxAdDzYuU
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
|
|
json "github.com/hanzoai/s3-cli/pkg/colorjson"
|
|
"github.com/hanzoai/s3-cli/pkg/probe"
|
|
)
|
|
|
|
type retryManager struct {
|
|
retries int
|
|
maxRetries int
|
|
retryInterval time.Duration
|
|
commandCtx context.Context
|
|
retryCtx context.Context
|
|
cancelRetry context.CancelFunc
|
|
}
|
|
|
|
func newRetryManager(ctx context.Context, retryInterval time.Duration, maxRetries int) *retryManager {
|
|
retryCtx, cancelFunc := context.WithCancel(context.Background())
|
|
return &retryManager{
|
|
retryInterval: retryInterval,
|
|
maxRetries: maxRetries,
|
|
commandCtx: ctx,
|
|
retryCtx: retryCtx,
|
|
cancelRetry: cancelFunc,
|
|
}
|
|
}
|
|
|
|
type retryMessage struct {
|
|
SourceURL string `json:"sourceURL"`
|
|
TargetURL string `json:"targetURL"`
|
|
Retries int `json:"retries"`
|
|
}
|
|
|
|
func (r retryMessage) String() string {
|
|
return fmt.Sprintf("<INFO> Retries %d: source `%s` >> target `%s`", r.Retries, r.SourceURL, r.TargetURL)
|
|
}
|
|
|
|
func (r retryMessage) JSON() string {
|
|
jsonMessageBytes, e := json.MarshalIndent(r, "", " ")
|
|
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
|
|
return string(jsonMessageBytes)
|
|
}
|
|
|
|
func (r *retryManager) retry(action func(rm *retryManager) *probe.Error) {
|
|
defer r.cancelRetry()
|
|
for r.retries <= r.maxRetries {
|
|
|
|
err := action(r)
|
|
if err == nil {
|
|
return
|
|
}
|
|
|
|
select {
|
|
case <-r.retryCtx.Done():
|
|
return
|
|
case <-r.commandCtx.Done():
|
|
return
|
|
case <-time.After(r.retryInterval/2 + time.Duration(rand.Int63n(int64(r.retryInterval)))):
|
|
r.retries++
|
|
}
|
|
|
|
}
|
|
}
|