Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour of golang UDP server

Tags:

networking

go

udp

I wrote a simple UDP server in go.

When I do go run udp.go it prints all packages I send to it. But when running go run udp.go > out it stops passing stdout to the out file when the client stops.

The client is simple program that sends 10k requests. So in the file I have around 50% of sent packages. When I run the client again, the out file grows again until the client script finishes.

Server code:

package main

import (
  "net"

  "fmt"
)

func main() {
  addr, _ := net.ResolveUDPAddr("udp", ":2000")
  sock, _ := net.ListenUDP("udp", addr)

  i := 0
  for {
    i++
    buf := make([]byte, 1024)
    rlen, _, err := sock.ReadFromUDP(buf)
    if err != nil {
      fmt.Println(err)
    }
    fmt.Println(string(buf[0:rlen]))
    fmt.Println(i)
    //go handlePacket(buf, rlen)
  }
}

And here is the client code:

package main

import (
  "net"

  "fmt"
)

func main() {

  num := 0
  for i := 0; i < 100; i++ {
    for j := 0; j < 100; j++ {
      num++
      con, _ := net.Dial("udp", "127.0.0.1:2000")
      fmt.Println(num)
      buf := []byte("bla bla bla I am the packet")
      _, err := con.Write(buf)
      if err != nil {
        fmt.Println(err)
      }
    }
  }
}
like image 779
Sławosz Avatar asked Oct 29 '13 12:10

Sławosz


2 Answers

As you suspected, it seems like UDP packet loss due to the nature of UDP. Because UDP is connectionless, the client doesn't care if the server is available or ready to receive data. So if the server is busy processing, it won't be available to handle the next incoming datagram. You can check with netstat -u (which should include UDP packet loss info). I ran into the same thing, in which the server (receive side) could not keep up with the packets sent.

You can try two things (the second worked for me with your example):

Call SetReadBuffer. Ensure the receive socket has enough buffering to handle everything you throw at it.

sock, _ := net.ListenUDP("udp", addr)
sock.SetReadBuffer(1048576)

Do all packet processing in a go routine. Try to increase the datagrams per second by ensuring the server isn't busy doing other work when you want it to be available to receive. i.e. Move the processing work to a go routine, so you don't hold up ReadFromUDP().

//Reintroduce your go handlePacket(buf, rlen) with a count param
func handlePacket(buf []byte, rlen int, count int)
        fmt.Println(string(buf[0:rlen]))
        fmt.Println(count)
}

...

go handlePacket(buf, rlen, i)

One final option:

Lastly, and probably not what you want, you put a sleep in your client which would slow down the rate and would also remove the problem. e.g.

buf := []byte("bla bla bla I am the packet")
time.Sleep(100 * time.Millisecond)
_, err := con.Write(buf)
like image 191
Matt Self Avatar answered Nov 07 '22 14:11

Matt Self


Try syncing stdout after the write statements.

os.Stdout.Sync() 
like image 39
Fred the Magic Wonder Dog Avatar answered Nov 07 '22 16:11

Fred the Magic Wonder Dog