-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
147 lines (135 loc) · 3.72 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
context "context"
"fmt"
"io"
"net"
"net/url"
"strings"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
)
func logUnaryRPC(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
var start = time.Now().UTC()
// "method" is the gRPC term, which actually means "path" in HTTP terms.
log.WithField("method", method).Trace("starting unary RPC")
var err = invoker(ctx, method, req, reply, cc, opts...)
log.WithFields(log.Fields{
"method": method,
"timeMillis": time.Now().UTC().Sub(start).Milliseconds(),
"error": err,
}).Debug("finished gRPC client request")
return err
}
func dialAddress(ctx context.Context, addr string) (*grpc.ClientConn, error) {
dialCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
var logger = grpc.UnaryClientInterceptor(grpc.UnaryClientInterceptor(logUnaryRPC))
var dialAddr string
opts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithBlock(), grpc.WithChainUnaryInterceptor(logger)}
if strings.HasPrefix(addr, "unix://") {
parsedUrl, err := url.Parse(addr)
if err != nil {
return nil, err
}
dialAddr = parsedUrl.Path
opts = append(opts, grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}))
} else {
dialAddr = addr
}
conn, err := grpc.DialContext(dialCtx, dialAddr, opts...)
if err != nil {
return nil, fmt.Errorf("failed to dial `%v`: %w", dialAddr, err)
}
log.Printf("[dialAddress] dial successful. addr = %s", addr)
go func() {
<-ctx.Done()
if cerr := conn.Close(); cerr != nil {
grpclog.Infof("Failed to close conn to %s: %v", addr, cerr)
}
}()
return conn, err
}
// / This is a bit reversed from normal operations. We're forwarding messages
// / from the local grpc server to a remote server. Sends messages received by
// / the server to the client and sends responses sent by the client to the
// / server.
func proxyStream(ctx context.Context, streamDesc string, source grpc.ServerStream, destination grpc.ClientStream, req interface{}, resp interface{}) error {
eg, ctx := errgroup.WithContext(ctx)
log.WithField("stream", streamDesc).Trace("starting streaming proxy")
var startTime = time.Now().UTC()
eg.Go(func() (_err error) {
var msgCount = 0
defer func() {
log.WithFields(log.Fields{
"stream": streamDesc,
"error": _err,
"msgCount": msgCount,
}).Trace("finished forwarding messages from client to server")
}()
for {
select {
case <-ctx.Done():
return nil
default:
// Keep going
}
err := source.RecvMsg(req)
if err == io.EOF {
return nil
} else if err != nil {
return err
}
msgCount++
err = destination.SendMsg(req)
if err == io.EOF {
return nil
} else if err != nil {
return err
}
}
})
eg.Go(func() (_err error) {
var msgCount = 0
defer func() {
log.WithFields(log.Fields{
"stream": streamDesc,
"error": _err,
"msgCount": msgCount,
}).Trace("finished forwarding messages from server to client")
}()
for {
select {
case <-ctx.Done():
return nil
default:
// Keep going
}
err := destination.RecvMsg(resp)
if err == io.EOF {
return nil
} else if err != nil {
return err
}
msgCount++
err = source.SendMsg(resp)
if err == io.EOF {
return nil
} else if err != nil {
return err
}
}
})
var err = eg.Wait()
log.WithFields(log.Fields{
"stream": streamDesc,
"error": err,
"timeMillis": time.Now().UTC().Sub(startTime).Milliseconds(),
}).Debug("finished proxying streaming RPC")
return err
}