Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry attempts #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func serviceCmd(ctx *config.Context) *cobra.Command {
func startCmd(ctx *config.Context) *cobra.Command {
const (
flagRelayInterval = "relay-interval"
flagRelayAttempts = "relay-attempts"
)

cmd := &cobra.Command{
Expand All @@ -41,9 +42,10 @@ func startCmd(ctx *config.Context) *cobra.Command {
if err != nil {
return err
}
return core.StartService(context.Background(), st, c[src], c[dst], viper.GetDuration(flagRelayInterval))
return core.StartService(context.Background(), st, c[src], c[dst], viper.GetDuration(flagRelayInterval), viper.GetUint(flagRelayAttempts))
},
}
cmd.Flags().Duration(flagRelayInterval, 3*time.Second, "time interval to perform relays")
cmd.Flags().Uint(flagRelayAttempts, 5, "count of retry")
return cmd
}
10 changes: 6 additions & 4 deletions core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
)

// StartService starts a relay service
func StartService(ctx context.Context, st StrategyI, src, dst *ProvableChain, relayInterval time.Duration) error {
func StartService(ctx context.Context, st StrategyI, src, dst *ProvableChain, relayInterval time.Duration, attempts uint) error {
sh, err := NewSyncHeaders(src, dst)
if err != nil {
return err
}
srv := NewRelayService(st, src, dst, sh, relayInterval)
srv := NewRelayService(st, src, dst, sh, relayInterval, attempts)
return srv.Start(ctx)
}

Expand All @@ -25,16 +25,18 @@ type RelayService struct {
st StrategyI
sh SyncHeadersI
interval time.Duration
attempts uint
}

// NewRelayService returns a new service
func NewRelayService(st StrategyI, src, dst *ProvableChain, sh SyncHeadersI, interval time.Duration) *RelayService {
func NewRelayService(st StrategyI, src, dst *ProvableChain, sh SyncHeadersI, interval time.Duration, attempts uint) *RelayService {
return &RelayService{
src: src,
dst: dst,
st: st,
sh: sh,
interval: interval,
attempts: attempts,
}
}

Expand All @@ -48,7 +50,7 @@ func (srv *RelayService) Start(ctx context.Context) error {
default:
return srv.Serve(ctx)
}
}, rtyAtt, rtyDel, rtyErr, retry.OnRetry(func(n uint, err error) {
}, retry.Attempts(srv.attempts), retry.Delay(srv.interval), retry.DelayType(retry.FixedDelay), rtyErr, retry.OnRetry(func(n uint, err error) {
log.Println(fmt.Sprintf("- [%s][%s]try(%d/%d) relay-service: %s", srv.src.ChainID(), srv.dst.ChainID(), n+1, rtyAttNum, err))
})); err != nil {
return err
Expand Down