Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dan-and-dna/gin-grpc-network
A network library that can handle requests for both http and grpc protocols
https://github.com/dan-and-dna/gin-grpc-network
gin grpc http network protocol
Last synced: about 3 hours ago
JSON representation
A network library that can handle requests for both http and grpc protocols
- Host: GitHub
- URL: https://github.com/dan-and-dna/gin-grpc-network
- Owner: DAN-AND-DNA
- License: bsd-3-clause
- Created: 2022-11-12T10:31:22.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2022-11-17T14:45:33.000Z (about 2 years ago)
- Last Synced: 2024-11-14T14:46:31.364Z (5 days ago)
- Topics: gin, grpc, http, network, protocol
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gin-grpc-network
A network library that can handle both http and grpc requests## [中文文档](./README_cn.md)
# Example
[Complete example](https://github.com/DAN-AND-DNA/easyman)- Write a Handler to handle grpc and http requests:
```golang
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
```c++
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
```c++
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
```