
Quic is a golang library that provides a simple and efficient way to implement the QUIC protocol, which is a transport-layer protocol designed to improve the performance of the internet.
QUIC is designed to be a general-purpose transport protocol, similar to TCP and UDP, but with additional features such as connection multiplexing and header compression.
To get started with Quic in golang, you'll need to import the quic-go library and create a new instance of the quic.Config struct, which defines the configuration for the QUIC connection.
The quic.Config struct has several fields that you can customize, including the address and port to listen on, the maximum packet size, and the number of concurrent connections allowed.
Expand your knowledge: Dns over Quic
Usage and Configuration
To create a QUIC endpoint, you send and receive traffic on a network address. This can be done to either accept inbound QUIC connections or create outbound ones.
A Config structure configures a QUIC endpoint and must not be modified after it has been passed to a QUIC function. You can reuse a Config, and the quic package will not modify it.
Starting a QUIC server is similar to using the standard lib http in go; see the example server for more information.
On a similar theme: Golang Create Error
Usage

You can create an Endpoint to either accept inbound QUIC connections or create outbound ones, which send and receive traffic on a network address.
A Stream is a QUIC stream, an ordered, reliable byte stream. This is useful for sending and receiving data over a QUIC connection.
You can use quic.Dial and quic.DialAddr without explicitly initializing a quic.Transport, but you can't reuse the same UDP socket for other outgoing connections, or to listen for incoming connections.
Starting a QUIC server is very similar to the standard lib http in go, and you can use a http3.RoundTripper as a Transport in a http.Client.
DialAddrContext establishes a new QUIC connection to a server using the provided context, and DialContext establishes a new QUIC connection to a server using a net.PacketConn using the provided context.
A new 0-RTT QUIC connection to a server can be established using DialEarlyContext or DialEarly, and the same PacketConn can be used for multiple calls to Dial and Listen, QUIC connection IDs are used for demultiplexing the different connections.
Check this out: Golang Create

The host parameter is used for SNI, and the tls.Config must define an application protocol using NextProtos.
You can create a new connection to a network address using Dial, and the same PacketConn can be used for multiple calls to Dial and Listen, QUIC connection IDs are used for demultiplexing the different connections.
A new 0-RTT QUIC connection to a server can be established using DialAddrEarly, and it uses a new UDP connection and closes this connection when the QUIC connection is closed.
An EarlyConnection is a connection that is handshaking, and data sent during the handshake is encrypted using the forward secure keys.
A fresh viewpoint: Dns over Quic Server
Config
A Config structure configures a QUIC endpoint. It contains all the configuration data needed for a QUIC server or client.
You can reuse a Config, and the quic package won't modify it once it's been passed to a QUIC function.
To create a new Config, you can use the Clone method, which returns a shallow clone of the original Config. This is safe to do even if the Config is being used concurrently by a QUIC endpoint.
The Clone method returns nil if the original Config is nil.
Error Handling
Error Handling is a crucial aspect of working with QUIC in Go. A StreamErrorCode is used to indicate why a stream is being closed, according to RFC 9000, Section 20.2.
QUIC provides a way to handle errors that occur during stream operations. A StreamError is used for Stream.CancelRead and Stream.CancelWrite, and is also returned from Stream.Read and Stream.Write if the peer canceled reading or writing.
Stream errors can be triggered by the peer, and it's essential to handle these situations properly. A StreamError is returned when the peer cancels reading or writing, which can happen due to various reasons such as network issues or intentional cancellation.
Connections
Connections are a QUIC connection between two peers, which can return various types of errors, including ApplicationError, TransportError, IdleTimeoutError, HandshakeTimeoutError, StatelessResetError, and VersionNegotiationError.
A Connection is established using a net.PacketConn, which can be reused for multiple calls to Dial and Listen. QUIC connection IDs are used for demultiplexing the different connections.
A QUIC connection can be in one of three states: a Connection, an EarlyConnection, or a ConnectionState. An EarlyConnection is a connection that is handshaking, while a ConnectionState records basic details about a QUIC connection.
*DialAddr

DialAddr establishes a new QUIC connection to a server. It uses a new UDP connection and closes this connection when the QUIC connection is closed. The hostname for SNI is taken from the given address. The tls.Config.CipherSuites allows setting of TLS 1.3 cipher suites.
In other words, DialAddr is similar to Dial, but it uses a new UDP connection for each call. This is in contrast to Dial, which can reuse a UDP connection for multiple calls.
Reset
Resetting a stream can be a crucial step in managing connections, and it's essential to understand how it works. Reset aborts writes on the stream and notifies the peer that the stream was terminated abruptly.
Any blocked writes will be unblocked and return errors, which can be useful for detecting and handling issues. This can happen when the stream is reset, and it's a good idea to be prepared to handle these errors.
Reset sends the application protocol error code, which must be less than 2^62, to the peer. This error code is an important part of the reset process.
Use Close to wait for the peer's acknowledgement, as Reset does not wait for the peer to acknowledge receipt of the error. This is an important distinction to make when working with streams.
Reset does not affect reads, so you can continue reading from the stream without any issues.
For another approach, see: Golang Source Code
Connection Management
A QUIC connection between two peers is represented by a Connection object, which can return various types of errors, including ApplicationError and TransportError.
These errors can occur due to misbehaving peers or application issues.
The Connection object can also return IdleTimeoutError when a peer goes away unexpectedly.
This can happen due to a net.Error timeout error.
Additionally, HandshakeTimeoutError can occur if the cryptographic handshake takes too long, also a net.Error timeout error.
StatelessResetError is returned when a stateless reset is received.
This is also a net.Error temporary error.
In QUIC, the version of the protocol used by the peers must overlap for a connection to be established.
If there's no version overlap, a VersionNegotiationError is returned by the client.
Endpoint
An Endpoint handles QUIC traffic on a network address. It can accept inbound connections or create outbound ones.
Multiple goroutines may invoke methods on an Endpoint simultaneously, which can be a powerful feature but also requires careful synchronization.
An Endpoint can be created using the NewEndpoint function, which takes a net.PacketConn as the underlying transport. If the PacketConn is not a *net.UDPConn, the endpoint may be slower and lack access to some features of the network.
Closing an Endpoint using the Close method will abort every open connection and discard data in stream read and write buffers. It will also wait for the peers of any open connection to acknowledge the connection has been closed.
Curious to learn more? Check out: Golang Network Programming
Streams and Reading
Reading data from a stream in QUIC Golang is a straightforward process. The Read function reads data from the stream as soon as at least one byte of data is available.
If the peer closes the stream cleanly, Read returns io.EOF after returning all data sent by the peer. This is a reliable way to determine when the stream has been fully read.
It's worth noting that calling Read concurrently with other functions is not safe. This is a crucial consideration when working with streams in QUIC Golang.
Stream

A Stream is an ordered byte stream, which means that data is read and written in a specific order. This is important to keep in mind when working with streams.
Streams may be bidirectional, read-only, or write-only, and methods inappropriate for a stream's direction will return errors. For example, trying to write to a read-only stream will result in an error.
It is not safe to perform concurrent reads from or writes to a stream, but it is safe to read and write at the same time. This is an important consideration when designing applications that use streams.
Reads and writes are buffered, which means that data is stored in memory before being written to the stream. This can help improve performance and reduce the number of system calls.
A Stream is the interface implemented by QUIC streams, and it is not necessary to wrap a stream in a bufio.ReadWriter or apply additional buffering.
Flush

The Flush method is used to clear data written to a stream, but it doesn't wait for the peer to acknowledge receipt of the data.
To ensure the peer acknowledges receipt of the data, you should use the Close method instead of Flush.
Flush does not guarantee the peer has received the data, making it a non-blocking operation.
Writing and Closing
Writing to a quic stream is a bit different than what you're used to. The Write method writes data to the stream write buffer, but it's not sent immediately. Instead, it's sent when the buffer is sufficiently full.
To ensure your data is sent, you can call the Flush method. This will send any buffered data to the peer. If you want to wait for the peer to acknowledge receipt of the data, you should use the Close method instead.
Closing a quic stream can be a bit tricky. If you want to abort writes on the stream, you can use the CloseWrite method. This will send any data in the write buffer to the peer, but it won't wait for acknowledgement.
Send in v0.7.0

In v0.7.0, a SendStream was introduced. This new feature is a unidirectional Send Stream.
A SendStream is a powerful tool that allows for efficient data transfer. It's designed to be a one-way street, sending data from one end to the other without any expectation of receiving anything in return.
The addition of SendStream in v0.7.0 marks a significant improvement in handling data streams. This change enables developers to write more streamlined code that's better equipped to handle complex data transfer scenarios.
CloseWrite
CloseWrite is a function that aborts writes on a stream. Any blocked writes will be unblocked and return errors.
It sends any data in the stream write buffer to the peer. This means that any data that was being written to the stream before CloseWrite was called will still be sent to the peer.
CloseWrite does not wait for the peer to acknowledge receipt of the data. If you need to wait for the peer's acknowledgement, you should use the Close function instead.
It's worth noting that CloseWrite only aborts writes on the stream, it does not close the stream itself. This is different from Close, which closes the stream and all associated connections.
Take a look at this: Golang Func Type
Write

Writing to a stream can be a bit tricky, but understanding how it works can make a big difference.
The Write method writes data to the stream write buffer. Buffered data is only sent when the buffer is sufficiently full.
You can call the Flush method to ensure buffered data is sent, which is useful when you need to guarantee that your data is transmitted immediately.
A SendStream is a type of unidirectional stream, added in version 0.7.0.
To abort writes on a stream, use the CloseWrite method. Any blocked writes will be unblocked and return errors.
CloseWrite sends any data in the stream write buffer to the peer, but it doesn't wait for the peer to acknowledge receipt of the data.
Intriguing read: Golang Write String to File
WriteByte
Writing a single byte to the stream is a straightforward process, thanks to the WriteByte function. This function writes a single byte to the stream, making it a convenient option for sending small amounts of data.

To use WriteByte, you simply need to call the function with the byte you want to write as an argument. The byte will be written to the stream, where it can be buffered and sent to the peer when the buffer is sufficiently full.
It's worth noting that WriteByte is a simple but powerful tool for sending small amounts of data, and can be used in a variety of situations where a single byte needs to be sent. For example, you might use WriteByte to send a single flag or indicator byte to the peer.
Suggestion: Golang Use Cases
Types and Constants
In Quic Golang, there are specific types and constants that play a crucial role in the communication between the client and the server. A ClientToken is a token received by the client.
This token can be used to skip address validation on future connection attempts.
IsReadOnly
IsReadOnly is a crucial concept in understanding the behavior of streams. It determines whether a stream is read-only, which means it's a unidirectional stream created by the peer.

A read-only stream is essentially one-way, allowing data to flow in only one direction. This is opposed to a bidirectional stream that allows data to flow both ways.
The IsReadOnly function is a quick way to check if a stream is read-only, which can be useful in certain situations. For instance, you might want to avoid attempting to write to a read-only stream to prevent errors.
In the context of streams, being read-only doesn't necessarily mean it's not useful. In fact, read-only streams can be very useful for certain applications, such as streaming data from a peer.
A fresh viewpoint: Golang Os Read File
Constants
In programming, constants are values that don't change once they're set. They're like unchangeable variables.
Constants can be numbers, like 5 or 10, or strings, like "hello" or "goodbye". They can also be other types of values, like true or false.
A good example of a constant is the value of pi, which is approximately 3.14. This value never changes, regardless of the situation.

Constants are often used in formulas and calculations to ensure that the same values are used consistently. For instance, in a formula that calculates the area of a circle, the value of pi is always used, never changing.
In some programming languages, constants are declared with special keywords, like "const" or "final". This tells the computer that the value can't be changed.
Types
A ClientToken is a token received by the client. It can be used to skip address validation on future connection attempts.
There are different types of tokens in this system. A ClientToken is one of them.
A ClientToken is used to skip address validation on future connection attempts. This can be helpful in certain situations.
This type of token is received by the client. It's a crucial part of the system.
Documentation and Implementation
quic-go is an implementation of the QUIC protocol in Go, including the Unreliable Datagram Extension and Datagram Packetization Layer Path MTU Discovery. It supports HTTP/3, including QPACK.
Curious to learn more? Check out: Golang Go
quic-go implements the IETF QUIC draft-29, but support for it will eventually be dropped as it's phased out of the ecosystem. This implementation is based on RFC 9000, RFC 9001, and RFC 9002.
quic-go has a wide range of features, including support for the Unreliable Datagram Extension and Datagram Packetization Layer Path MTU Discovery. It also has a robust error handling system, with errors like Err0RTTRejected and ErrServerClosed being returned in specific situations.
Here are some of the key features of quic-go's implementation:
Projects Using Go
Projects Using Go are incredibly diverse and powerful. You can see this in the list of projects that use the quic-go library, which includes AdGuardHome, a free and open-source network-wide ads and trackers blocking DNS server.
One of the most impressive projects is algernon, a small self-contained pure-Go web server with a wide range of features, including Lua, Markdown, HTTP/2, and QUIC support. This shows how Go can be used to create complex and scalable systems.
Expand your knowledge: Go vs Golang
Another notable project is caddy, a fast and multi-platform web server with automatic HTTPS. This makes it a great choice for developers who want to create secure and high-performance web applications.
Here are some of the projects that use quic-go:
Release Policy
Our release policy is designed to keep up with the latest developments in Go. We aim to support the latest two Go releases.
This approach ensures that our implementation stays current and relevant. We don't want to fall behind in terms of compatibility and functionality.
The latest two Go releases are the ones we focus on. This way, we can provide the best possible experience for our users.
By supporting the latest Go releases, we can take advantage of new features and improvements. This benefits everyone involved.
Our goal is to stay ahead of the curve, not to lag behind. We're committed to keeping our implementation up to date.
Clone inv0.15.0
Clone in 0.15.0 is a safe operation that can be performed concurrently by multiple goroutines. This is because Clone returns a shallow clone of the Config, which means it only copies the top-level fields and not any nested or referenced values.
The Clone method is designed to work with Config objects that are being used by a QUIC endpoint, and it's safe to use even if the original Config is being modified concurrently by other goroutines.
If the original Config is nil, Clone will return nil as well. This is a simple but important detail to keep in mind when working with Config objects.
Documentation
In the context of networking, errors can occur when attempting to establish a connection. The Err0RTTRejected error is returned from Open{Uni}Stream{Sync} and Accept{Uni}Stream methods when a server rejects a 0-RTT connection attempt.
This error can be a frustrating experience, especially when working with networked applications. It's essential to understand the underlying causes of this error to resolve the issue efficiently.
The Err0RTTRejected error is a specific response from the server, indicating that it cannot establish a 0-RTT connection. This can happen due to various reasons, including server configuration or security settings.
When dealing with this error, it's crucial to verify the server's configuration and settings. This can help identify potential issues and resolve the error.
The ErrServerClosed error is returned by the Listener or EarlyListener's Accept method after a call to Close. This error occurs when the server is closed or terminated unexpectedly.
This error can be a sign of a more significant issue, such as a server crash or a network failure. In such cases, it's essential to investigate the root cause of the error to prevent future occurrences.
In summary, understanding the Err0RTTRejected and ErrServerClosed errors is vital for troubleshooting networked applications. By identifying the underlying causes of these errors, developers can resolve issues efficiently and improve the overall performance of their applications.
Pure Go Implementation
quic-go is an implementation of the QUIC protocol in Go. It includes support for HTTP/3, including QPACK.
quic-go has a wide range of features, including the Unreliable Datagram Extension and Datagram Packetization Layer Path MTU Discovery.
The project has a strong focus on implementing the latest QUIC drafts, including the IETF QUIC draft-29. However, support for this draft will eventually be dropped.
Here are some of the key features of quic-go:
quic-go is used in a variety of projects, including AdGuardHome, algernon, and caddy.
Frequently Asked Questions
Is QUIC replacing TCP?
No, QUIC is not replacing TCP, but rather operating alongside it at the transport layer. QUIC resides in the application space, making it easier to update and enhance than traditional TCP.
Featured Images: pexels.com


