Giter Site home page Giter Site logo

grpc-quic's Introduction

gRPC over QUIC

The Go language implementation of gRPC over QUIC.

Prerequisites

  • [Go][]: any one of the three latest major [releases][go-releases].

Installation

With [Go module][] support (Go 1.11+), simply add the following import

import "github.com/sssgun/grpc-quic"

Usage

As a server

func echoGrpcQuicServer(certFile, keyFile string) error {
	log.Println("starting echo quic server")

	tlsConf, err := generateTLSConfig(certFile, keyFile)
	if err != nil {
		log.Printf("QuicServer: failed to generateTLSConfig. %s", err.Error())
		return err
	}

	ql, err := quic.ListenAddr(*addr, tlsConf, nil)
	if err != nil {
		log.Printf("QuicServer: failed to ListenAddr. %s", err.Error())
		return err
	}
	listener := qnet.Listen(ql)

	s := grpc.NewServer()
	pb.RegisterGreeterServer(s, &server{})
	log.Printf("QuicServer: listening at %v", listener.Addr())

	if err := s.Serve(listener); err != nil {
		log.Printf("QuicServer: failed to serve. %v", err)
		return err
	}

	log.Println("stopping echo quic server")
	return nil
}

As a client

func echoGrpcQuicClient() error {
	tlsConf := &tls.Config{
		InsecureSkipVerify: true,
		NextProtos:         []string{"quic-echo-example"},
	}

	creds := qnet.NewCredentials(tlsConf)

	dialer := qnet.NewQuicDialer(tlsConf)
	grpcOpts := []grpc.DialOption{
		grpc.WithContextDialer(dialer),
		grpc.WithTransportCredentials(creds),
	}

	conn, err := grpc.Dial(*addr, grpcOpts...)
	if err != nil {
		log.Printf("QuicClient: failed to grpc.Dial. %s", err.Error())
		return err
	}
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			log.Printf("QuicClient: failed to close - grpc.Dial. %s", err.Error())
		}
	}(conn)

	c := pb.NewGreeterClient(conn)
	name := defaultName
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
	if err != nil {
		log.Printf("QuicClient: could not greet. %v", err)
		return err
	}
	log.Printf("QuicClient: Greeting=%s", r.GetMessage())

	return nil
}

grpc-quic's People

Contributors

sssgun avatar tylorshine avatar

Stargazers

めかにゃんこ avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.