For TCP sockets, there's SetDeadline, SetReadDeadline and 
SetWriteDeadline.  See https://pkg.go.dev/net

For the originally posted code, I would replace

    time.Sleep(5 * time.Second)

with code which either waits 5 seconds, or terminates on a shutdown signal 
if that is received first.  The idiomatic way to do that is with a context 
<https://go.dev/blog/context>.

select {
  case <-ctx.Done():
    return   // context cancelled
  case <-time.After(5 * time.Second):
}

On Saturday, 16 April 2022 at 16:05:11 UTC+1 rog wrote:

> I'm not sure what you think such a general feature might look like.
>
> In general, it's not a good idea to force an arbitrary thing to stop, 
> because that can leave resources in an undefined state, so the only 
> reasonable approach AFAICS is to ask the function that's running to stop. 
> That functionality is provided by the language with channels and by the 
> standard library with the context package.
>
> On Sat, 16 Apr 2022, 15:25 Zhaoxun Yan, <yan.z...@gmail.com> wrote:
>
>> Thanks rog!
>>
>>   I already dealt with it in my project since the Listen of gorilla 
>> websocket did as you just mentioned - close and even throw out an error. 
>> But I am surprised at why golang still has not provided a general feature 
>> on that.
>>
>> Zhaoxun
>>
>> On Sat, Apr 16, 2022 at 10:14 PM roger peppe <rogp...@gmail.com> wrote:
>>
>>> 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.z...@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...@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/6d4bdd33-f23b-446b-8438-f9e49d0fc8efn%40googlegroups.com.

Reply via email to