Datagrams
The Unreliable Datagram Extension
Unreliable datagrams are not part of QUIC (RFC 9000) itself, but a feature that is added by a QUIC extension (RFC 9221). As other extensions, it can be negotiated during the handshake. Receiving datagrams can be enabled by setting the quic.Config.EnableDatagrams flag. This is reflected in ConnectionState.SupportsDatagrams.Local. Whether the peer supports receiving datagrams is indicated by ConnectionState.SupportsDatagrams.Remote, obtained from Conn.ConnectionState().
QUIC DATAGRAMs are a new QUIC frame type sent in QUIC 0-RTT or 1-RTT packets. Therefore, they’re end-to-end encrypted and congestion-controlled. Lost DATAGRAM frames are not retransmitted.
Sending and Receiving Datagrams
Datagrams are sent using the SendDatagram method on the quic.Conn:
conn.SendDatagram([]byte("foobar"))And received using ReceiveDatagram:
msg, err := conn.ReceiveDatagram(context.Background())Note that this code path is currently not optimized. It works for datagrams that are sent occasionally, but it doesn’t achieve the same throughput as writing data on a stream. Please get in touch on issue #3766 if your use case relies on high datagram throughput, or if you’d like to help fix this issue.