-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgrpc.go
139 lines (113 loc) · 3.13 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
package grpcquic
import (
"context"
"crypto/tls"
"fmt"
"net"
"time"
qnet "github.com/gfanton/grpc-quic/net"
options "github.com/gfanton/grpc-quic/opts"
"github.com/gfanton/grpc-quic/transports"
quic "github.com/lucas-clemente/quic-go"
ma "github.com/multiformats/go-multiaddr"
"google.golang.org/grpc"
)
var quicConfig = &quic.Config{
// MaxReceiveStreamFlowControlWindow: 3 * (1 << 20), // 3 MB
// MaxReceiveConnectionFlowControlWindow: 4.5 * (1 << 20), // 4.5 MB
// Versions: []quic.VersionNumber{101},
// AcceptCookie: func(clientAddr net.Addr, cookie *quic.Cookie) bool {
// // TODO(#6): require source address validation when under load
// return true
// },
KeepAlive: true,
}
func newPacketConn(addr string) (net.PacketConn, error) {
// create a packet conn for outgoing connections
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
return net.ListenUDP("udp", udpAddr)
}
func newQuicDialer(tlsConf *tls.Config) func(string, time.Duration) (net.Conn, error) {
return func(target string, timeout time.Duration) (net.Conn, error) {
var err error
m, err := ma.NewMultiaddr(target)
if err != nil {
return nil, err
}
laddr, protocol, err := qnet.ParseMultiaddr(m)
if err != nil {
return nil, err
}
if protocol == ma.P_UDP {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
sess, err := quic.DialAddrContext(ctx, laddr, tlsConf, quicConfig)
if err != nil {
return nil, err
}
return qnet.NewConn(sess)
}
if protocol == ma.P_TCP {
return net.DialTimeout("tcp", laddr, timeout)
}
return nil, fmt.Errorf("Invalid protocol")
}
}
func Dial(target string, opts ...options.DialOption) (*grpc.ClientConn, error) {
cfg := options.NewClientConfig()
if err := cfg.Apply(opts...); err != nil {
return nil, err
}
creds := transports.NewCredentials(cfg.TLSConf)
dialer := newQuicDialer(cfg.TLSConf)
grpcOpts := []grpc.DialOption{
grpc.WithDialer(dialer),
grpc.WithTransportCredentials(creds),
}
grpcOpts = append(grpcOpts, cfg.GrpcDialOptions...)
return grpc.Dial(target, grpcOpts...)
}
func newListener(laddr string, tlsConf *tls.Config) (net.Listener, error) {
m, err := ma.NewMultiaddr(laddr)
if err != nil {
return nil, err
}
laddr, protocol, err := qnet.ParseMultiaddr(m)
if err != nil {
return nil, err
}
if protocol == ma.P_UDP {
pconn, err := newPacketConn(laddr)
if err != nil {
return nil, err
}
ql, err := quic.Listen(pconn, tlsConf, quicConfig)
if err != nil {
return nil, err
}
return qnet.Listen(ql), nil
}
if protocol == ma.P_TCP {
l, err := net.Listen("tcp", laddr)
if err != nil {
return nil, err
}
return l, nil
}
return nil, fmt.Errorf("Invalid protocol `%s`", m)
}
func NewServer(laddr string, opts ...options.ServerOption) (*grpc.Server, net.Listener, error) {
cfg := options.NewServerConfig()
if err := cfg.Apply(opts...); err != nil {
return nil, nil, err
}
creds := transports.NewCredentials(cfg.TLSConf)
l, err := newListener(laddr, cfg.TLSConf)
if err != nil {
return nil, nil, err
}
return grpc.NewServer(grpc.Creds(creds)), l, err
}