Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: document that Tick interval gap can exceed duration #30271

Closed
xsigndll opened this issue Feb 16, 2019 · 6 comments
Closed

time: document that Tick interval gap can exceed duration #30271

xsigndll opened this issue Feb 16, 2019 · 6 comments

Comments

@xsigndll
Copy link

@xsigndll xsigndll commented Feb 16, 2019

What version of Go are you using (go version)?

go version go1.11.5 windows/amd64

What did you do?

The documentation example for time.Tick ( https://golang.org/pkg/time/#Tick ) states that the channel returns now. But if the routine inside the tick exceeds the duration, the ticker produces a one-time gap. This may need clarification in the documentation example. You may check this playground example: https://play.golang.org/p/ImIhZTLYciq

package main

import (
	"fmt"
	"time"
)

func main() {
	c := time.Tick(1 * time.Second)
	for now := range c {
		fmt.Printf("now\t\t%v\n", now)
		fmt.Printf("time.Now()\t%v\n", time.Now())
		time.Sleep(3 * time.Second)
	}
}

What did you expect to see?

time.Now() should be the same as now.

What did you see instead?

It produces a gap in the second interval:

now		2009-11-10 23:00:01 +0000 UTC m=+1.000000001
time.Now()	2009-11-10 23:00:01 +0000 UTC m=+1.000000001
now		2009-11-10 23:00:02 +0000 UTC m=+2.000000001
time.Now()	2009-11-10 23:00:04 +0000 UTC m=+4.000000001  <==== +2s
now		2009-11-10 23:00:05 +0000 UTC m=+5.000000001
time.Now()	2009-11-10 23:00:07 +0000 UTC m=+7.000000001
now		2009-11-10 23:00:08 +0000 UTC m=+8.000000001
time.Now()	2009-11-10 23:00:10 +0000 UTC m=+10.000000001
now		2009-11-10 23:00:11 +0000 UTC m=+11.000000001
time.Now()	2009-11-10 23:00:13 +0000 UTC m=+13.000000001
@agnivade

This comment has been minimized.

Copy link
Contributor

@agnivade agnivade commented Feb 16, 2019

It is mentioned that Tick is a convenience wrapper for NewTicker and the behavior you mention is written in https://golang.org/pkg/time/#NewTicker - It adjusts the intervals or drops ticks to make up for slow receivers.

@xsigndll

This comment has been minimized.

Copy link
Author

@xsigndll xsigndll commented Feb 17, 2019

Correct. But „adjusting the interval“ implies that the channel returns the new corrected time („now“), which is not the case. I still think that the documentation should be corrected for time.Tick to prevent people from thinkin that the channel always returns „now“.

@agnivade

This comment has been minimized.

Copy link
Contributor

@agnivade agnivade commented Feb 17, 2019

@agnivade agnivade changed the title time.Tick interval gap if duration exceeded time: document that Tick interval gap can exceed duration Feb 17, 2019
@ianlancetaylor ianlancetaylor added this to the Go1.13 milestone Feb 19, 2019
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Feb 19, 2019

Shall we just change the variable name now in ExampleTick to next? Or t?

@xsigndll

This comment has been minimized.

Copy link
Author

@xsigndll xsigndll commented Feb 19, 2019

I would go for next

@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Nov 10, 2019

Change https://golang.org/cl/206123 mentions this issue: time: change variable name to next

@gopherbot gopherbot closed this in 9eb9c7b Nov 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.