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: the time.After channel does not close when reusing it #16259

Closed
blackbeans opened this issue Jul 4, 2016 · 3 comments

Comments

Projects
None yet
4 participants
@blackbeans
Copy link

commented Jul 4, 2016

i want use the same channel of time.After for waiting some response timeout.
but when i reuse the channel to read ,it blocks the current goroutine.

Now ,i use another way to solve the problem.

latch := make(chan time.Time, 1)
t := time.AfterFunc(timeout, func() {
    close(latch)
})

defer t.Stop()
tch := (<-chan time.Time)(latch)

//wating for response
for g, f := range self.futures {
    resp, err := f.Get(tch)
    }
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jul 4, 2016

I'm sorry, I don't understand. Can you provide a complete self-contained example that shows the bug?

@ianlancetaylor ianlancetaylor changed the title the time.After channel does not close when reusing it time: the time.After channel does not close when reusing it Jul 4, 2016

@blackbeans

This comment has been minimized.

Copy link
Author

commented Jul 4, 2016

code like this:

package main
import "time"
import "fmt"

func main() {
ch := time.After(10 * time.Second)
<-ch
fmt.Println("timeout")
<-ch
fmt.Println("unreachable")
}

"ch" should be close when timeout has been happen .

@adg

This comment has been minimized.

Copy link
Contributor

commented Jul 4, 2016

That's not how the time.After function works. It doesn't close the channel. The docs say:

After waits for the duration to elapse and then sends the current time on the returned channel. 

and that's what it does.

@adg adg closed this Jul 4, 2016

@golang golang locked and limited conversation to collaborators Jul 4, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.