-
Notifications
You must be signed in to change notification settings - Fork 3
/
grpc.go
84 lines (69 loc) · 1.8 KB
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package xhttp
import (
"bytes"
"context"
"crypto/tls"
"encoding/binary"
"io/ioutil"
"net"
"net/http"
"time"
"github.com/learninto/goutil/errors"
"golang.org/x/net/http2"
"google.golang.org/protobuf/proto"
)
type GrpcClient interface {
DoUnary(ctx context.Context, api string, req, resp proto.Message) (h2resp *http.Response, err error)
}
var plainTextH2Transport = &http2.Transport{
AllowHTTP: true,
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(network, addr)
},
}
func NewGrpcClient(timeout time.Duration) GrpcClient {
return &myClient{
cli: &http.Client{
Transport: plainTextH2Transport,
Timeout: timeout,
},
}
}
func (c *myClient) DoUnary(ctx context.Context, api string, req, resp proto.Message) (h2resp *http.Response, err error) {
rpb, err := proto.Marshal(req)
if err != nil {
return
}
// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
lpb := len(rpb)
buf := &bytes.Buffer{}
buf.Grow(lpb + 5)
bs := make([]byte, 4)
binary.BigEndian.PutUint32(bs, uint32(lpb))
buf.WriteByte(0) // 不压缩
buf.Write(bs) // 写入长度
buf.Write(rpb) // 写入消息内容
h2req, err := http.NewRequest("POST", api, buf)
if err != nil {
return
}
h2req = h2req.WithContext(ctx)
h2req.Header.Set("trailers", "TE")
h2req.Header.Set("content-type", "application/grpc+proto")
h2resp, err = c.Do(ctx, h2req)
if err != nil {
return
}
defer h2resp.Body.Close()
pb, err := ioutil.ReadAll(h2resp.Body)
if err != nil {
return
}
if status := h2resp.Trailer.Get("grpc-status"); status != "0" {
err = errors.Errorf("grpc status: %s grpc message: %s", status, h2resp.Trailer.Get("grpc-message"))
return
}
// 因为是 Unary,可以直接跳过前五个字节
err = proto.Unmarshal(pb[5:], resp)
return
}