Blocking reads from buffered reader (Bufio)

2,665 views
Skip to first unread message

Jirka Daněk

unread,
Jul 4, 2012, 4:43:52 PM7/4/12
to golan...@googlegroups.com
Dear Gophers,

i am communicating with a server using net.Conn, which I get from net.Dial. To make my job easier, I wrapped it with bufio.NewReader(), so I can use convenience functions like ReadBytes.

My problem is that all functions I get with buffered reader are non-blocking. That becomes a problem when the server has nothing to say for a while and I keep getting EOFs from reads. I assume, once I call bufio.NewReader at an unbuffered reader, there is no way back. I cannot return to using the unbuffered reader directly.

So I want to ask: Is there a way to get a blocking read with bufio.Reader? Or function like SleepUntilDataAreAvailable? Otherwise, I would probably have to stick with unbuffered reader.

I use buffered reader mostly for the few convenience functions it provides. Is there a significant performance benefit from using it on a network socket as well? I kind of doubt it, because I think there are few buffers already in the OS and so on, so adding one more in my app does not help that much.

Thanks for your help

Rémy Oudompheng

unread,
Jul 4, 2012, 4:53:14 PM7/4/12
to Jirka Daněk, golan...@googlegroups.com
On Wed 04 July 2012 at 13:43 -0700, Jirka Daněk wrote:
> Dear Gophers,
>
> i am communicating with a server using *net.Conn*, which I get from
> net.Dial. To make my job easier, I wrapped it with *bufio.NewReader*(), so
> I can use convenience functions like *ReadBytes*.
>
> My problem is that all functions I get with buffered reader are
> non-blocking. That becomes a problem when the server has nothing to say for
> a while and I keep getting EOFs from reads. I assume, once I call *
> bufio.NewReader* at an unbuffered reader, there is no way back. I cannot
> return to using the unbuffered reader directly.

Buffered readers have blocking behaviour. What is your problem about?
Maybe you should show some code with details about what you see.

Rémy.

Message has been deleted

Jesse McNelis

unread,
Jul 4, 2012, 6:41:46 PM7/4/12
to Jirka Daněk, golan...@googlegroups.com
On Thu, Jul 5, 2012 at 6:43 AM, Jirka Daněk <jur...@gmail.com> wrote:
> non-blocking. That becomes a problem when the server has nothing to say for
> a while and I keep getting EOFs from reads.

If you're getting io.EOF returned from reading a network connection
then the connection has been closed.


--
=====================
http://jessta.id.au

Jirka Daněk

unread,
Jul 4, 2012, 7:30:29 PM7/4/12
to golan...@googlegroups.com, Jirka Daněk

On Wednesday, July 4, 2012 10:53:14 PM UTC+2, Rémy Oudompheng wrote:
On Wed 04 July 2012 at 13:43 -0700, Jirka Daněk wrote: 
> Dear Gophers, 

> i am communicating with a server using *net.Conn*, which I get from 
> net.Dial. To make my job easier, I wrapped it with *bufio.NewReader*(), so 
> I can use convenience functions like *ReadBytes*. 


> My problem is that all functions I get with buffered reader are 
> non-blocking. That becomes a problem when the server has nothing to say for 
> a while and I keep getting EOFs from reads. I assume, once I call * 
> bufio.NewReader* at an unbuffered reader, there is no way back. I cannot 

> return to using the unbuffered reader directly. 

Buffered readers have blocking behaviour. What is your problem about? 
Maybe you should show some code with details about what you see. 

Rémy.

I tried to write an example (https://gist.github.com/3050053), which showed that buffered reads really block. 

On Thursday, July 5, 2012 12:41:46 AM UTC+2, Jesse McNelis wrote:
On Thu, Jul 5, 2012 at 6:43 AM, Jirka Daněk <***@gmail.com> wrote:
> non-blocking. That becomes a problem when the server has nothing to say for
> a while and I keep getting EOFs from reads.

If you're getting io.EOF returned from reading a network connection
then the connection has been closed. 

You are right. I forget to send a required parameter, which made the server close connection without warning.

When I was reading documentation, I was not sure what EOF means in case of network sockets, I thought it signalizes that data aren't momentarily ready.

Thanks for your help.
Reply all
Reply to author
Forward
0 new messages