Skip to content

Commit 1df3281

Browse files
committed
Introduce wait function to exit immediately on some signals
Signed-off-by: Takeshi Arabiki <takeshi.arabiki@datachain.jp>
1 parent 0b719e8 commit 1df3281

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

core/channel.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ func CreateChannel(ctx context.Context, pathName string, src, dst *ProvableChain
6767
}
6868

6969
logger.Warn("Retrying transaction...")
70-
time.Sleep(5 * time.Second)
70+
if err := wait(ctx, 5*time.Second); err != nil {
71+
return err
72+
}
7173
}
7274

7375
select {

core/connection.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ func CreateConnection(ctx context.Context, pathName string, src, dst *ProvableCh
7575
}
7676

7777
logger.Warn("Retrying transaction...")
78-
time.Sleep(5 * time.Second)
78+
if err := wait(ctx, 5*time.Second); err != nil {
79+
return err
80+
}
7981
}
8082

8183
select {

core/send.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ func GetFinalizedMsgResult(ctx context.Context, chain ProvableChain, msgID MsgID
3838
} else {
3939
waitTime = avgBlockTime * time.Duration(msgHeight.GetRevisionHeight()-lfHeight.GetRevisionHeight())
4040
}
41-
time.Sleep(waitTime)
41+
if err := wait(ctx, waitTime); err != nil {
42+
return err
43+
}
4244
return fmt.Errorf("msg(id=%v) not finalied: msg.height(%v) > lfh.height(%v)", msgID, msgHeight, lfHeight)
4345
}
4446

core/service.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ func (srv *RelayService) Start(ctx context.Context) error {
9696
})); err != nil {
9797
return err
9898
}
99-
time.Sleep(srv.interval)
99+
if err := wait(ctx, srv.interval); err != nil {
100+
return err
101+
}
100102
}
101103
}
102104

core/utils.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package core
22

33
import (
4+
"context"
45
"encoding/hex"
56
"fmt"
67
"strconv"
78
"strings"
9+
"time"
810

911
abci "github.com/cometbft/cometbft/abci/types"
1012
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
@@ -171,3 +173,17 @@ func strToUint64(s string) uint64 {
171173
}
172174
return uint64(v)
173175
}
176+
177+
func wait(ctx context.Context, d time.Duration) error {
178+
// NOTE: We can use time.After with Go 1.23 or later
179+
// cf. https://pkg.go.dev/time@go1.23.0#After
180+
t := time.NewTimer(d)
181+
defer t.Stop()
182+
183+
select {
184+
case <-ctx.Done():
185+
return ctx.Err()
186+
case <-t.C:
187+
return nil
188+
}
189+
}

0 commit comments

Comments
 (0)