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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘