grpcquic

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 7 Imported by: 0

README

gRPC over QUIC

The Go language implementation of gRPC over QUIC.

Largely based on 'https://github.com/sssgun/grpc-quic' updated for quic-go

Installation

go get github.com/coremedic/grpc-quic

Usage

Import module
import grpcquic "github.com/coremedic/grpc-quic"
Server
func main() {
	// Generate TLS config for server
	log.Println("Generating TLS config...")
	tlsConfig, err := generateTLSConfig()
	if err != nil {
		log.Fatal(err)
	}

	// Start gRPC QUIC listener and service
	log.Println("Starting QUIC gRPC Server...")
	quicListener, err := quic.ListenAddr("127.0.0.1:1848", tlsConfig, nil)
	if err != nil {
		log.Fatal(err)
	}

	grpcQuicListener := grpcquic.Listen(*quicListener)

	grpcServer := grpc.NewServer()
	pb.RegisterExampleServiceServer(grpcServer, &grpcServiceServer{})
	log.Printf("gRPC-QUIC: listening at %v\n", grpcQuicListener.Addr())

	// Accept incoming gRPC requests
	if err := grpcServer.Serve(grpcQuicListener); err != nil {
		log.Fatal(err)
	}
}
Client
func main() {
	// Create TLS config
	tlsConfig := &tls.Config{
		InsecureSkipVerify: true,
		NextProtos:         []string{"grpc-quic-example"},
	}
	creds := grpcquic.NewCredentials(tlsConfig)

	// Connect to gRPC Service Server
	dialer := grpcquic.NewQuicDialer(tlsConfig)
	grpcOpts := []grpc.DialOption{
		grpc.WithContextDialer(dialer),
		grpc.WithTransportCredentials(creds),
	}
	conn, err := grpc.Dial("127.0.0.1:1848", grpcOpts...)
	if err != nil {
		log.Fatal(err)
	}

	// Close connection at end of function
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			log.Fatal(err)
		}
	}(conn)

	// Create gRPC client
	grpcClient := pb.NewExampleServiceClient(conn)

	// Send gRPC request
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	req, err := grpcClient.Example(ctx, &pb.ExampleRequest{Msg: "Ayooooo"})
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("[gRPC]: %v\n", req.GetMsg())
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidConnType = fmt.Errorf("connection must be of type QuicConn")

ErrInvalidConnType is returned when a connection is not of type QuicConn.

Functions

func Listen

func Listen(ql quic.Listener) net.Listener

func NewCredentials

func NewCredentials(tlsConfig *tls.Config) credentials.TransportCredentials

Create new credentials object from tls config

func NewQuicConn

func NewQuicConn(connection quic.Connection) (net.Conn, error)

NewQuicConn creates a new QuicConn with an open QUIC stream

func NewQuicDialer

func NewQuicDialer(tlsConfig *tls.Config) func(context.Context, string) (net.Conn, error)

NewQuicDialer returns a new Quic Dialer function

Types

type Credentials

type Credentials struct {
	// contains filtered or unexported fields
}

Credentials for gRPC over QUIC https://pkg.go.dev/google.golang.org/grpc@v1.42.0/credentials#TransportCredentials

func (*Credentials) ClientHandshake

func (creds *Credentials) ClientHandshake(ctx context.Context, authority string, conn net.Conn) (net.Conn, credentials.AuthInfo, error)

QUIC gRPC implementation of ClientHandshake

ClientHandshake handles the client-side authentication handshake for the specified authentication protocol. ClientHandshake returns an authenicated connection and relevant authentication information.

Read more here: https://pkg.go.dev/google.golang.org/grpc@v1.42.0/credentials#TransportCredentials

func (*Credentials) Clone

Clone makes a copy of Credentials

func (*Credentials) Info

func (creds *Credentials) Info() credentials.ProtocolInfo

Info provides ProtocolInfo of Credentials

func (*Credentials) OverrideServerName deprecated

func (creds *Credentials) OverrideServerName(name string) error

QUIC gRPC facade for OverrideServerName

Deprecated: use grpc.WithAuthority instead.

func (*Credentials) ServerHandshake

func (creds *Credentials) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error)

QUIC gRPC implementation of ServerHandshake

ServerHandshake handles the server-side authentication handshake for the specified authentication protocol. ServerHandshake returns an authenicated connection and relevant authentication information.

Read more here: https://pkg.go.dev/google.golang.org/grpc@v1.42.0/credentials#TransportCredentials

type QuicConn

type QuicConn struct {
	// contains filtered or unexported fields
}

QuicConn implements net.Conn using a QUIC connection and stream

func (*QuicConn) Close

func (qc *QuicConn) Close() error

Close closes both the QUIC stream and connection

func (*QuicConn) LocalAddr

func (qc *QuicConn) LocalAddr() net.Addr

LocalAddr returns the local network address of the QUIC connection

func (*QuicConn) Read

func (qc *QuicConn) Read(b []byte) (int, error)

Read reads data from the QUIC stream. Returns the number of bytes read and any error encountered

func (*QuicConn) RemoteAddr

func (qc *QuicConn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address of the QUIC connection

func (*QuicConn) SetDeadline

func (qc *QuicConn) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines for the QUIC stream

func (*QuicConn) SetReadDeadline

func (qc *QuicConn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for future Read calls on the QUIC stream

func (*QuicConn) SetWriteDeadline

func (qc *QuicConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for future Write calls on the QUIC stream

func (*QuicConn) Write

func (qc *QuicConn) Write(b []byte) (int, error)

Write writes data to the QUIC stream. Returns the number of bytes written and any error encountered

type QuicListener

type QuicListener struct {
	// contains filtered or unexported fields
}

func (*QuicListener) Accept

func (ql *QuicListener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener

func (*QuicListener) Addr

func (ql *QuicListener) Addr() net.Addr

Addr returns the listeners network address

func (*QuicListener) Close

func (ql *QuicListener) Close() error

Close closes the listener

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL