Is data from udp socket buffered until it is read for processing

54 Views Asked by At

I need to tweak an existing code which uses sockets to listen to UDP multicast traffic and process it. The exact system setup is hard to test locally since I can receive that UDP multicast traffic on a specific server. So I want to understand what is going to happen.

First I make a connection:

c, err := net.ListenPacket("udp4", vsAddress[0]+":"+vsAddress[1])
// ...
packetConnIpv4 = ipv4.NewPacketConn(c)

Then in a loop I read from it and pass the message to an unbuffered channel:

    for {
        b := make([]byte, maxDatagramSize) // maxDatagramSize is 2048
        var n int
        n, _, _, err = packetConnIpv4.ReadFrom(b)
        if err != nil {
            return
        }
        msgBytes = new(bytes.Buffer)
        msgBytes.Reset()
        msgBytes.Write(b[:n])
        if err != nil {
            return
        }
        
        msgIn <- msgBytes // unbuffered channel
    }

Then in another goroutine I read from the channel msgIn and process the message.

So now the questions:

  1. What will happen with the data arriving from the network if the processing of msgBytes takes long time (no reads from the msgIn so it blocks on write)? This long processing IS an option in my case.
  2. If the data is going to be buffered then what is the size of that buffer?
  3. Or is the data is going to be dropped?

I personally need the data to buffered as much as possible. I need it be buffered and stored in an order that it came from the network so that later I come back and grab another portion and handle it.

0

There are 0 best solutions below