I am running into an issue with using json.Decode on a net.Conn that I was wondering if someone had a solution for.
I am trying to use json.Decode to decode json objects from a net.Conn stream (specifically a unix socket). However, in the spirit of being able to cancel the goroutine that is doing the decoding, I would like it not to block forever. Thus, I am setting the read timeout of the net.Conn before each read. However, I have run into an issue. If json.Decode gets any non io.EOF error (or in some cases even io.EOF), the decoder will never work again (always returns that error when calling the Decode method and no way to clear it). I tried to make an io.Reader that wrapped the net.Conn and looked for the timeout error, and instead returned a nil error with zero bytes and no error, but Decode will spin forever if the io.Reader it is reading from returns 0 bytes read but no error.
So I think I am stuck... Unless there is another way to handle this (other than creating a new decoder after every timeout which seems like overkill), I will probably implement my own decoder.
Here is an example of what I mean:
http://play.golang.org/p/lHI6cb8F3yNOTE: The example in the link above won't run but it shows the gist of what I am trying to do.
Any help would be greatly appreciated, but I am ok if someone says I just have to implement my own decoder (or modify the standard one).
Thanks!
Parker