How can stop a Read of net.Conn without closing it

2,437 views
Skip to first unread message

杜沁园

unread,
Jun 4, 2019, 12:56:38 PM6/4/19
to golang-nuts
When there is no data in tcp connection,   net.Conn.Read   will block.

Then I want to cancel the net.Conn.Read  from another goroutine,   But I do not want to close the connection, Because I will reuse it latter. How to write the code???

 For example:





func (p *proxy) toClient(ctx context.Context, conn net.Conn) {

for {
               // block here
n, err := conn.Read(buff)
               //.... handle something
}
}

func main() {
        //... build connection
        go toClient(ctx, conn)
       
        //... Then I want to cancel above block read and reuse the conn,  How to write????
        cancle()
        reuse(conn)
}

Ian Lance Taylor

unread,
Jun 4, 2019, 8:45:55 PM6/4/19
to 杜沁园, golang-nuts
As far as I know there is no way to do this.

You'll have to structure your program differently.

Ian

Inada Naoki

unread,
Jun 5, 2019, 2:53:16 AM6/5/19
to golang-nuts
conn.SetReadDeadline(time.Now())

Kurtis Rader

unread,
Jun 5, 2019, 3:10:41 AM6/5/19
to Inada Naoki, golang-nuts
On Tue, Jun 4, 2019 at 11:53 PM Inada Naoki <songof...@gmail.com> wrote:
conn.SetReadDeadline(time.Now())

Did you test that solution? Setting a deadline only affects "future Read calls" according to the documentation. It cannot be used to timeout an extant read. Which is what I would expect given the available UNIX like kernel APIs. So while I have not tested this using a program written in Go I would not expect it to work and the equivalent solution definitely does not work in other languages I use regularly.
 
--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Ian Lance Taylor

unread,
Jun 5, 2019, 10:14:22 AM6/5/19
to Kurtis Rader, 杜沁园, Inada Naoki, golang-nuts
On Wed, Jun 5, 2019 at 12:10 AM Kurtis Rader <kra...@skepticism.us> wrote:
>
> On Tue, Jun 4, 2019 at 11:53 PM Inada Naoki <songof...@gmail.com> wrote:
>>
>> conn.SetReadDeadline(time.Now())
>
>
> Did you test that solution? Setting a deadline only affects "future Read calls" according to the documentation. It cannot be used to timeout an extant read. Which is what I would expect given the available UNIX like kernel APIs. So while I have not tested this using a program written in Go I would not expect it to work and the equivalent solution definitely does not work in other languages I use regularly.

No, this does in fact work in Go. I should have thought of that. The
docs say, correctly, "SetReadDeadline sets the deadline for future
Read calls and any currently-blocked Read call."

Ian

han...@gmail.com

unread,
May 10, 2020, 9:47:37 PM5/10/20
to golang-nuts
Wouldn't something like conn.SetReadDeadline(time.Unix(1,0)) or SetReadDeadline(time.Unix(0,1)) be better than conn.SetReadDeadline(time.Now()), as it won't make an unnecessary system call to get the current time? I'm just trying to make sure I'm not missing something.

William

Andrei Tudor Călin

unread,
May 11, 2020, 1:51:27 PM5/11/20
to han...@gmail.com, golang-nuts
That would work, yes. net/http, for example, has var aLongTimeAgo = time.Unix(1, 0), which is used for cancellation.

--
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/e619b5d1-7c31-4a7c-9222-500a37ff189a%40googlegroups.com.


--
Andrei Călin
Reply all
Reply to author
Forward
0 new messages