dongzhiyan5693 2018-05-28 10:18
浏览 52
已采纳

及时轮询控制台应用程序,其时间指示器在ctrl + c上退出

I would like to pool for a token in on a timely base. The Token itself got also information about when it expires. This should run forever until the user enters ctrl+c.

I tried the same with

span := timeLeft(*expDate)
timer := time.NewTimer(span).C
ticker := time.NewTicker(time.Second * 5).C

which also does not work (the application hangs after count down). So I decided to try it with <- time.After(...)

This is my code that does not work. You will see the count down but it never breaks on expiration.

This is is a small extract with the polling logic for simplicity sake in a main.go:

func refreshToken() (time.Time, error) {
        //This should simulate a http request and returns the new target date for the next refresh
        time.Sleep(2 * time.Second)
        return time.Now().Add(10 * time.Second), nil
    }

func timeLeft(d time.Time) time.Duration {
    exactLeft := d.Sub(time.Now())
    floorSeconds := math.Floor(exactLeft.Seconds())
    return time.Duration(floorSeconds) * time.Second
}

func poller(expDate *time.Time) {
    exp := timeLeft(*expDate)

    done := make(chan bool)
    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt)
    for {
        select {

        // print time left on the screen
        case <-time.After(3 * time.Second):
            go func() {
                fmt.Printf("Next Token refresh will be in: %v", timeLeft(*expDate))
            }()
        // mark as done when date is due
        case <-time.After(exp):
            fmt.Println("Refresh token now!")
            done <- true

        // exit app
        case <-c:
            os.Exit(0)
            break

        // exit function when done
        case <-done:
            break
        }

    }
}

func main() {

    var expiration time.Time
    expiration = time.Now().Add(10 * time.Second)
    // loop and refresh token as long as the app does not exit
    for {
        poller(&expiration)

        ex, err := refreshToken()
        expiration = ex
        if err != nil {
            panic(err)
        }

        fmt.Println("next round poller")
    }
}

I am also not sure if I need the done channel at all? What is required to listen to two timers and call itself until someone hits ctrl+c?

  • 写回答

1条回答 默认 最新

  • dongsi2317 2018-05-29 05:10
    关注

    Found a solution. While @ain was right wit the buffered done channel, it is not really required in the code now. It worked without it.

    The trick did have the timeroutside of the for loop and the ticker within it. Reason is the time.Afteris a functhat return a new channel on every iteration. This seams perfectly fine for the ticker, but not for the timer. With the following changes it worked =) ...

       func poller(expDate *time.Time) {
            exp := timeLeft(*expDate)
            timer := time.After(exp)
    
            fmt.Printf("Next Token refresh will be in: %v
    ", exp)
    
            c := make(chan os.Signal, 1)
            signal.Notify(c, os.Interrupt)
            for {
                select {
    
                // print time left on the screen
                case <-time.After(3 * time.Second):
                    go func() {
                        fmt.Printf("     ")
                        fmt.Printf("%v", timeLeft(*expDate))
                    }()
                // mark as done when date is due
                case <-timer:
                    fmt.Println("Refresh token now!")
                    return
    
                // exit app
                case <-c:
                    os.Exit(0)
                    break
    
                }
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵