gingrpcnetwork

package module
v0.0.0-...-d2988f7 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

README

gin-grpc-network

A network library that can handle both http and grpc requests

中文文档

Example

Complete example

  • Write a Handler to handle grpc and http requests:
func setupHandlers() {
	// grpc 以pkg service method来区别请求
	network.HandleProto("webbff", "webbff", "login", &webbff.WebBFF_ServiceDesc, gingrpc.Handler{
		Proto: &webbff.LoginReq{},
		HandleProto: func(ctx context.Context, req interface{}) (interface{}, error) {
			ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
			defer cancel()

			l := ctxzap.Extract(ctx)
			md, ok := metadata.FromIncomingContext(ctx)
			if !ok {
				return nil, status.Error(codes.Internal, "")
			}

			ct := md.Get("content-type")
			if len(ct) == 0 {
				return nil, status.Error(codes.NotFound, "content-type lost")
			}

			l.Info("get Content-Type", zap.String("Content-Type", ct[0]))

			reqProto := req.(*webbff.LoginReq)
			username := reqProto.GetName()
			password := reqProto.GetPassword()

			l.Info("user try to login", zap.String("name", username), zap.String("password", password))

			return &webbff.LoginResp{Token: "xxxxxxxx"}, nil
		},
	})
}

http performance

goos: windows
goarch: amd64
pkg: github.com/dan-and-dna/gin-grpc
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkGinGrpc
BenchmarkGinGrpc-12      3528080              1675 ns/op            1496 B/op           13 allocs/op
PASS

grpc performance

The middleware is used, so there is no additional overhead, just grpc-go

goos: windows
goarch: amd64
pkg: easyman
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkGrpc
BenchmarkGrpc-12          112734             10224 ns/op            1702 B/op           36 allocs/op
PASS

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Poll

func Poll()

Types

This section is empty.

Directories

Path Synopsis
modules

Jump to

Keyboard shortcuts

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