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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化