forked from src-d/lookout-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
147 lines (125 loc) · 3.86 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 pb
import (
"fmt"
"net"
"net/url"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// maxMessageSize overrides default grpc max. message size to send/receive to/from clients
var maxMessageSize = 100 * 1024 * 1024 // 100MB
//TODO: https://github.com/grpc/grpc-go/issues/1911
// ToNetListenerAddress converts a gRPC URL to a network+address consumable by
// net.Listen. For example:
// ipv4://127.0.0.1:8080 -> (tcp4, 127.0.0.1:8080)
func ToNetListenerAddress(target string) (network, address string, err error) {
u, err := url.Parse(target)
if err != nil {
return
}
if u.Scheme == "dns" {
err = fmt.Errorf("dns:// not supported")
return
}
if u.Scheme == "unix" {
network = "unix"
address = u.Path
return
}
address = u.Host
switch u.Scheme {
case "ipv4":
network = "tcp4"
case "ipv6":
network = "tcp6"
default:
err = fmt.Errorf("scheme not supported: %s", u.Scheme)
}
return
}
// ToGoGrpcAddress converts a standard gRPC target name to a
// one that is supported by grpc-go.
func ToGoGrpcAddress(address string) (string, error) {
n, a, err := ToNetListenerAddress(address)
if err != nil {
return "", err
}
if n == "unix" {
return fmt.Sprintf("unix:%s", a), nil
}
return a, nil
}
// Listen is equivalent to standard net.Listen, but taking gRPC URL as input.
func Listen(address string) (net.Listener, error) {
n, a, err := ToNetListenerAddress(address)
if err != nil {
return nil, err
}
return net.Listen(n, a)
}
// NewServer creates new grpc.Server with custom message size
func NewServer(opts ...grpc.ServerOption) *grpc.Server {
return NewServerWithInterceptors(nil, nil, opts...)
}
// NewServerWithInterceptors creates new grpc.Server with custom message size and default interceptors.
// The provided interceptors be executed after the default ones.
func NewServerWithInterceptors(
streamInterceptors []grpc.StreamServerInterceptor,
unaryInterceptors []grpc.UnaryServerInterceptor,
opts ...grpc.ServerOption,
) *grpc.Server {
streamInterceptors = append(
[]grpc.StreamServerInterceptor{CtxlogStreamServerInterceptor},
streamInterceptors...)
unaryInterceptors = append(
[]grpc.UnaryServerInterceptor{CtxlogUnaryServerInterceptor},
unaryInterceptors...)
opts = append(opts,
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
streamInterceptors...,
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
unaryInterceptors...,
)),
grpc.MaxRecvMsgSize(maxMessageSize),
grpc.MaxSendMsgSize(maxMessageSize),
)
return grpc.NewServer(opts...)
}
// DialContext creates a client connection to the given target with custom message size and default interceptors
func DialContext(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
return DialContextWithInterceptors(ctx, target, nil, nil, opts...)
}
// DialContextWithInterceptors creates a client connection to the given target with custom message size and default interceptors.
// The provided interceptors will be executed before the default ones.
func DialContextWithInterceptors(
ctx context.Context,
target string,
streamInterceptors []grpc.StreamClientInterceptor,
unaryInterceptors []grpc.UnaryClientInterceptor,
opts ...grpc.DialOption,
) (*grpc.ClientConn, error) {
streamInterceptors = append(
streamInterceptors,
CtxlogStreamClientInterceptor,
)
unaryInterceptors = append(
unaryInterceptors,
CtxlogUnaryClientInterceptor,
)
opts = append(opts,
grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(
streamInterceptors...,
)),
grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(
unaryInterceptors...,
)),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(maxMessageSize),
grpc.MaxCallSendMsgSize(maxMessageSize),
),
grpc.WithInsecure(),
)
return grpc.DialContext(ctx, target, opts...)
}