On Wednesday, 1 June 2016 21:36:12 UTC+3, Shawn Milochik wrote:
I've spent a couple of days trying to solve a problem. Now that I've
solved it, I'm wondering if anyone here might be able to explain what was

Short version:

- b := bytes.Buffer{}- decoder := gob.NewDecoder(&b) for {

- b.Reset()

+ var b bytes.Buffer+ decoder := gob.NewDecoder(&b)

The old version, declaring the buffer and decoder outside the loop, actually worked fine -- I was able to bring up dozens of peers and they were able to communicate.

The problem occurred when I added additional servers to the cluster after the others had already been communicating. Then I'd get "gob: unknown type id or corrupted data" for every message the newly-added servers received (the "old" servers had no errors). Declaring the buffer and decoder inside the loop solves the problem.

More details:

- This is a clustered chat server
- The problem occurred even with a very simple gob (a struct containing only one string)
- The problem occurred both in Docker containers and Digital Ocean droplets, so I don't think it's a networking issue
- I'm using Go 1.6.2 on 64-bit Ubuntu
- I made a simplified version of my code and was able to replicate the problem
- Servers are communicating with one another using the gorilla websocket library
- The problem happens regardless of whether or not the servers all start at once -- the trigger is if "new" server A connects to existing server B, and server B has already been communicating with other servers, then server A gets "corrupted" messages -- despite the fact that literally the same bytes are being sent by server B to the other servers server B has been talking to and they're fine
- Checking the length of the buffer on the sender and the length of the raw bytes from the receiver shows that the size matches at both ends

Thanks for looking! I'd really like to understand what was going on -- especially if the "fix" could be a performance issue.
Read https://blog.golang.org/gobs-of-data
and https://golang.org/pkg/encoding/gob/.

tl;dr; gob data stream contains the type definitions. When you reset the
buffer, but not the decoder you could be confusing the decoder about what
things have been received and hence read some things differently.

tl;dr; one gob.Encoder must always be paired with one gob.Decoder.

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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 6 | next ›
Discussion Overview
groupgolang-nuts @
postedJun 1, '16 at 6:36p
activeJun 1, '16 at 7:47p

3 users in discussion

Shawn Milochik: 3 posts Egon: 2 posts Voidlogic: 1 post



site design / logo © 2021 Grokbase