From 4922ba7e06531e57f71f87cdcecd467d784e31a5 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Fri, 29 Dec 2023 20:04:55 +0800 Subject: [PATCH 1/3] fix timer goroutine exit --- internal/app/utils/time.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/app/utils/time.go b/internal/app/utils/time.go index cd547f04..f7c00aa1 100644 --- a/internal/app/utils/time.go +++ b/internal/app/utils/time.go @@ -1,19 +1,32 @@ package utils -import "time" +import ( + "fmt" + "os" + "os/signal" + "syscall" + "time" +) func RunTimer(num int, uint Unit, fn func()) { go func() { + sigChan := make(chan os.Signal, 1) // run once right now + // 捕获指定的系统信号 + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) + t := time.NewTimer(time.Second * 1) fn() for { now := time.Now() next := now.Add(ParseDuration(num, uint)) next = TruncateTime(next, uint) - t := time.NewTimer(next.Sub(now)) + t.Reset(next.Sub(now)) select { case <-t.C: fn() + case <-sigChan: + fmt.Println("timer is exist...") + return } } }() From 59ba1f27611247671a42028e324da5bd49abe374 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Fri, 29 Dec 2023 20:12:31 +0800 Subject: [PATCH 2/3] stop timer when exit --- internal/app/utils/time.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/app/utils/time.go b/internal/app/utils/time.go index f7c00aa1..0c684a13 100644 --- a/internal/app/utils/time.go +++ b/internal/app/utils/time.go @@ -13,18 +13,21 @@ func RunTimer(num int, uint Unit, fn func()) { sigChan := make(chan os.Signal, 1) // run once right now // 捕获指定的系统信号 - signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) - t := time.NewTimer(time.Second * 1) + signal.Notify(sigChan, os.Interrupt, os.Kill, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) + // 创建一个timer + t := time.NewTimer(time.Second * 5) fn() for { now := time.Now() next := now.Add(ParseDuration(num, uint)) next = TruncateTime(next, uint) + //使用 time.Reset 重置 timer,重复利用 timer t.Reset(next.Sub(now)) select { case <-t.C: fn() case <-sigChan: + t.Stop() fmt.Println("timer is exist...") return } From 5ebf72056069c0b89eb127bde6fcef7e8500f248 Mon Sep 17 00:00:00 2001 From: heke <1280113751@qq.com> Date: Thu, 4 Jan 2024 16:53:26 +0800 Subject: [PATCH 3/3] fix get channels --- internal/app/task/ibc_chain_config_task.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/app/task/ibc_chain_config_task.go b/internal/app/task/ibc_chain_config_task.go index 866bde01..b0040f21 100644 --- a/internal/app/task/ibc_chain_config_task.go +++ b/internal/app/task/ibc_chain_config_task.go @@ -107,7 +107,7 @@ func (t *IbcChainConfigTask) getChainConf() ([]*entity.ChainConfig, error) { } // getIbcChannels 通过lcd channels_path 接口获取链上存在的所有channel信息 -func (t *IbcChainConfigTask) getIbcChannels(chain, lcd, apiPath string) ([]*entity.ChannelPath, error) { +func (t *IbcChainConfigTask) getIbcChannels(chain, lcd, originApiPath string) ([]*entity.ChannelPath, error) { if lcd == "" { logrus.Errorf("task %s %s getIbcChannels error, lcd error", t.Name(), chain) return nil, fmt.Errorf("lcd error") @@ -118,7 +118,7 @@ func (t *IbcChainConfigTask) getIbcChannels(chain, lcd, apiPath string) ([]*enti var channelPathList []*entity.ChannelPath for { - apiPath = strings.ReplaceAll(apiPath, replaceHolderOffset, strconv.Itoa(offset)) + apiPath := strings.ReplaceAll(originApiPath, replaceHolderOffset, strconv.Itoa(offset)) apiPath = strings.ReplaceAll(apiPath, replaceHolderLimit, strconv.Itoa(limit)) url := fmt.Sprintf("%s%s", lcd, apiPath) bz, err := utils.HttpGet(url)