From 97e8426e8b0d7d5bebda9053c4a4ed0a96d71595 Mon Sep 17 00:00:00 2001 From: Koji Matsumiya Date: Wed, 23 Mar 2022 09:55:45 +0900 Subject: [PATCH] add retry attempts Signed-off-by: Koji Matsumiya --- cmd/service.go | 4 +++- core/service.go | 10 ++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/service.go b/cmd/service.go index de61a207..827093a6 100644 --- a/cmd/service.go +++ b/cmd/service.go @@ -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{ @@ -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 } diff --git a/core/service.go b/core/service.go index 48ec1e83..a4018c85 100644 --- a/core/service.go +++ b/core/service.go @@ -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) } @@ -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, } } @@ -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