Golang - instability with Gob over TCP

259 views Asked by At

I am having an issue with instability when using gob for TCP communication.

The main problem is that if i transfer data "to fast" i either get errors where the server terminates the connection or the package simply doesn't arrive at the server. Now, if i add a delay of 20ms between packages, everything runs as expected.

Sadly i cannot link this to playground as i am running it in three different libraries, but i have inserted the essential/culprit code.

My guess is that if i don't have a delay timer i am overwriting the stream. Any ideas?

Update: By swapping out the receiver to bufio.NewReader(c.socket) i am actually able to get an error: "gob: unknown type id or corrupted data" which somewhat like the same issue as https://github.com/golang/go/issues/1238#event-242248991

//client -> server, running as a goroutine
func (c *Client) transmitter() {
    d := 20 * time.Millisecond
    timer := time.NewTimer(d) //silly hack
    for {
        select {

        case gd := <- c.broadcast:
            <- timer.C

            Write(c.socket, gd.Data, gd.NetworkDataType)

            timer.Reset(d) //refreshing timer
        }
    }
}

//server <- client, running as a goroutine
func (c *client) receiver (s *Server){
    for {
        in, err := Read(c.socket)
        if err != nil {
            log.println(err)
        }
        //doing stuff
    }
}

func Read(conn net.Conn) (GeneralData, error)  {
    in := GeneralData{}
    if err := gob.NewDecoder(conn).Decode(&in); err != nil {
        return in, err
    }
    return in, nil
}

func Write(conn net.Conn, data interface{}, ndt NetworkDataType) {
    err := gob.NewEncoder(conn).Encode(GeneralData{ndt,data})
    if err != nil {
        log.Println("error in write: ", err)
    }
}
0

There are 0 answers