Most network functions provide a way to provide a timeout or cancellation
explicitly. Listen is one such: see this method - if the context that it's
passed is cancelled, the Listen call will return.
https://pkg.go.dev/net#ListenConfig.Listen

The most general way to stop waiting on timeout or cancellation in the
absence of explicitly provided functionality is to start the function in a
separate goroutine and send notification when the function completes. This
is non-ideal because the goroutine will remain around even when the waiter
has given up, but can still be a useful technique in some circumstances.

Hope this helps,
  rog.

On Sat, 16 Apr 2022, 14:30 Zhaoxun Yan, <yan.zhao...@gmail.com> wrote:

> Timeout  is quite common practice in programming. For example, Listen
> function in internet connection with a timeout may just close the
> connection and Listen thread as silence persists for some period, like 60
> seconds.
>
> I found it very hard to implement such a general shutdown feature of a
> thread/goroutine on such I/O functions, here is one unsuccessful try, which
> I want to stop the whole in 9 seconds but it does not function as I wanted:
>
> package main
>
> import (
>     "fmt"
>     "time"
>     )
>
> func wait(){
>     time.Sleep(5 * time.Second)
>     fmt.Println("wait 1st signal")
>     time.Sleep(5 * time.Second)
>     fmt.Println("wait 2nd signal")
> }
>
>
> func main() {
>    ticker := time.NewTicker(3 * time.Second)
>    i := 0
>    for{
>        select{
>         case <- ticker.C:
>             i++
>             fmt.Printf("ticker rings %d\n", i)
>             if i==3{
>                return
>             }
>       default:
>           wait()
>       }
>    }
>
> }
>
> The result is to wait whole 30 seconds:
> wait 1st signal
> wait 2nd signal
> ticker rings 1
> wait 1st signal
> wait 2nd signal
> ticker rings 2
> wait 1st signal
> wait 2nd signal
> ticker rings 3
>
> An online course suggests to wrap up the wait/Listen function with a
> channel (which would return something instead of nothing above)
>
> go func(){
>   resultChan <- Listen()
> }()
>
> select{
> case a := <- resultChan:
>    //analyze a
> case <-ticker.C:
>    //might break or return
> }
>
> But this recipe obviously only kill the select goroutine rather than the
> anonymous goroutine that actually runs Listen function. My question is -
> how to kill the waiting or listening goroutine from outside?
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/e774c5a4-ed53-43da-a1fe-0d617603e223n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/e774c5a4-ed53-43da-a1fe-0d617603e223n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAJhgacgttQcggixh_VrKfFVFvAAJ7_dvX5sP7zK%2Bv23ZCLgxcw%40mail.gmail.com.

Reply via email to