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
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@xsigndll
Copy link

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

/cc @ianlancetaylor

@agnivade agnivade added Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels 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
Copy link
Contributor

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

@xsigndll
Copy link
Author

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
Copy link

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

@golang golang locked and limited conversation to collaborators Nov 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants