Skip to content

Commit

Permalink
rename Connection.{Send,Receive}Message to {Send,Receive}Datagram (#4116
Browse files Browse the repository at this point in the history
)

This is more consistent with both the RFC and the rest of the API. For
example, the option in the Config is already name EnableDatagrams, and
the property in the ConnectionState is named SupportsDatagrams.
  • Loading branch information
marten-seemann committed Oct 25, 2023
1 parent 7884f87 commit 1c631cf
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 71 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -159,18 +159,18 @@ On the receiver side, this is surfaced as a `quic.ApplicationError`.

Unreliable datagrams are a QUIC extension ([RFC 9221](https://datatracker.ietf.org/doc/html/rfc9221)) that is negotiated during the handshake. Support can be enabled by setting the `quic.Config.EnableDatagram` flag. Note that this doesn't guarantee that the peer also supports datagrams. Whether or not the feature negotiation succeeded can be learned from the `quic.ConnectionState.SupportsDatagrams` obtained from `quic.Connection.ConnectionState()`.

QUIC DATAGRAMs are a new QUIC frame type sent in QUIC 1-RTT packets (i.e. after completion of the handshake). Therefore, they're end-to-end encrypted and congestion-controlled. However, if a DATAGRAM frame is deemed lost by QUIC's loss detection mechanism, they are not automatically retransmitted.
QUIC DATAGRAMs are a new QUIC frame type sent in QUIC 1-RTT packets (i.e. after completion of the handshake). Therefore, they're end-to-end encrypted and congestion-controlled. However, if a DATAGRAM frame is deemed lost by QUIC's loss detection mechanism, they are not retransmitted.

Datagrams are sent using the `SendMessage` method on the `quic.Connection`:
Datagrams are sent using the `SendDatagram` method on the `quic.Connection`:

```go
conn.SendMessage([]byte("foobar"))
conn.SendDatagram([]byte("foobar"))
```

And received using `ReceiveMessage`:
And received using `ReceiveDatagram`:

```go
msg, err := conn.ReceiveMessage()
msg, err := conn.ReceiveDatagram()
```

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. There are also some restrictions regarding the maximum message size (see #3599).
Expand Down
4 changes: 2 additions & 2 deletions connection.go
Expand Up @@ -2343,7 +2343,7 @@ func (s *connection) onStreamCompleted(id protocol.StreamID) {
}
}

func (s *connection) SendMessage(p []byte) error {
func (s *connection) SendDatagram(p []byte) error {
if !s.supportsDatagrams() {
return errors.New("datagram support disabled")
}
Expand All @@ -2357,7 +2357,7 @@ func (s *connection) SendMessage(p []byte) error {
return s.datagramQueue.AddAndWait(f)
}

func (s *connection) ReceiveMessage(ctx context.Context) ([]byte, error) {
func (s *connection) ReceiveDatagram(ctx context.Context) ([]byte, error) {
if !s.config.EnableDatagrams {
return nil, errors.New("datagram support disabled")
}
Expand Down
6 changes: 3 additions & 3 deletions integrationtests/self/datagram_test.go
Expand Up @@ -57,7 +57,7 @@ var _ = Describe("Datagram test", func() {
defer wg.Done()
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(i))
Expect(conn.SendMessage(b)).To(Succeed())
Expect(conn.SendDatagram(b)).To(Succeed())
}(i)
}
wg.Wait()
Expand Down Expand Up @@ -120,7 +120,7 @@ var _ = Describe("Datagram test", func() {
for {
// Close the connection if no message is received for 100 ms.
timer := time.AfterFunc(scaleDuration(100*time.Millisecond), func() { conn.CloseWithError(0, "") })
if _, err := conn.ReceiveMessage(context.Background()); err != nil {
if _, err := conn.ReceiveDatagram(context.Background()); err != nil {
break
}
timer.Stop()
Expand Down Expand Up @@ -170,7 +170,7 @@ var _ = Describe("Datagram test", func() {
Expect(err).ToNot(HaveOccurred())
Expect(conn.ConnectionState().SupportsDatagrams).To(BeFalse())

Expect(conn.SendMessage([]byte{0})).To(HaveOccurred())
Expect(conn.SendDatagram([]byte{0})).To(HaveOccurred())

close()
conn.CloseWithError(0, "")
Expand Down
8 changes: 4 additions & 4 deletions integrationtests/self/zero_rtt_oldgo_test.go
Expand Up @@ -830,7 +830,7 @@ var _ = Describe("0-RTT", func() {
defer close(received)
conn, err := ln.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
receivedMessage, err = conn.ReceiveMessage(context.Background())
receivedMessage, err = conn.ReceiveDatagram(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(conn.ConnectionState().Used0RTT).To(BeTrue())
}()
Expand All @@ -844,7 +844,7 @@ var _ = Describe("0-RTT", func() {
)
Expect(err).ToNot(HaveOccurred())
Expect(conn.ConnectionState().SupportsDatagrams).To(BeTrue())
Expect(conn.SendMessage(sentMessage)).To(Succeed())
Expect(conn.SendDatagram(sentMessage)).To(Succeed())
<-conn.HandshakeComplete()
<-received

Expand Down Expand Up @@ -884,7 +884,7 @@ var _ = Describe("0-RTT", func() {
defer GinkgoRecover()
conn, err := ln.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
_, err = conn.ReceiveMessage(context.Background())
_, err = conn.ReceiveDatagram(context.Background())
Expect(err.Error()).To(Equal("datagram support disabled"))
<-conn.HandshakeComplete()
Expect(conn.ConnectionState().Used0RTT).To(BeFalse())
Expand All @@ -900,7 +900,7 @@ var _ = Describe("0-RTT", func() {
Expect(err).ToNot(HaveOccurred())
// the client can temporarily send datagrams but the server doesn't process them.
Expect(conn.ConnectionState().SupportsDatagrams).To(BeTrue())
Expect(conn.SendMessage(make([]byte, 100))).To(Succeed())
Expect(conn.SendDatagram(make([]byte, 100))).To(Succeed())
<-conn.HandshakeComplete()

Expect(conn.ConnectionState().SupportsDatagrams).To(BeFalse())
Expand Down
8 changes: 4 additions & 4 deletions integrationtests/self/zero_rtt_test.go
Expand Up @@ -960,7 +960,7 @@ var _ = Describe("0-RTT", func() {
defer close(received)
conn, err := ln.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
receivedMessage, err = conn.ReceiveMessage(context.Background())
receivedMessage, err = conn.ReceiveDatagram(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(conn.ConnectionState().Used0RTT).To(BeTrue())
}()
Expand All @@ -974,7 +974,7 @@ var _ = Describe("0-RTT", func() {
)
Expect(err).ToNot(HaveOccurred())
Expect(conn.ConnectionState().SupportsDatagrams).To(BeTrue())
Expect(conn.SendMessage(sentMessage)).To(Succeed())
Expect(conn.SendDatagram(sentMessage)).To(Succeed())
<-conn.HandshakeComplete()
<-received

Expand Down Expand Up @@ -1016,7 +1016,7 @@ var _ = Describe("0-RTT", func() {
defer GinkgoRecover()
conn, err := ln.Accept(context.Background())
Expect(err).ToNot(HaveOccurred())
_, err = conn.ReceiveMessage(context.Background())
_, err = conn.ReceiveDatagram(context.Background())
Expect(err.Error()).To(Equal("datagram support disabled"))
<-conn.HandshakeComplete()
Expect(conn.ConnectionState().Used0RTT).To(BeFalse())
Expand All @@ -1032,7 +1032,7 @@ var _ = Describe("0-RTT", func() {
Expect(err).ToNot(HaveOccurred())
// the client can temporarily send datagrams but the server doesn't process them.
Expect(conn.ConnectionState().SupportsDatagrams).To(BeTrue())
Expect(conn.SendMessage(make([]byte, 100))).To(Succeed())
Expect(conn.SendDatagram(make([]byte, 100))).To(Succeed())
<-conn.HandshakeComplete()

Expect(conn.ConnectionState().SupportsDatagrams).To(BeFalse())
Expand Down
10 changes: 5 additions & 5 deletions interface.go
Expand Up @@ -187,10 +187,10 @@ type Connection interface {
// Warning: This API should not be considered stable and might change soon.
ConnectionState() ConnectionState

// SendMessage sends a message as a datagram, as specified in RFC 9221.
SendMessage([]byte) error
// ReceiveMessage gets a message received in a datagram, as specified in RFC 9221.
ReceiveMessage(context.Context) ([]byte, error)
// SendDatagram sends a message as a datagram, as specified in RFC 9221.
SendDatagram([]byte) error
// ReceiveDatagram gets a message received in a datagram, as specified in RFC 9221.
ReceiveDatagram(context.Context) ([]byte, error)
}

// An EarlyConnection is a connection that is handshaking.
Expand Down Expand Up @@ -338,7 +338,7 @@ type ConnectionState struct {
// SupportsDatagrams says if support for QUIC datagrams (RFC 9221) was negotiated.
// This requires both nodes to support and enable the datagram extensions (via Config.EnableDatagrams).
// If datagram support was negotiated, datagrams can be sent and received using the
// SendMessage and ReceiveMessage methods on the Connection.
// SendDatagram and ReceiveDatagram methods on the Connection.
SupportsDatagrams bool
// Used0RTT says if 0-RTT resumption was used.
Used0RTT bool
Expand Down
48 changes: 24 additions & 24 deletions internal/mocks/quic/early_conn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 24 additions & 24 deletions mock_quic_conn_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1c631cf

Please sign in to comment.