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
    
                }
            }
        }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题