-
Notifications
You must be signed in to change notification settings - Fork 10
/
grpc.go
78 lines (64 loc) · 2.06 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
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/encoding/gzip"
)
func getSecurityOptions(jctx *JCtx) (grpc.DialOption, error) {
var bs []byte
var err error
if jctx.config.TLS.CA == "" {
return grpc.WithInsecure(), nil
}
certificate, _ := tls.LoadX509KeyPair(jctx.config.TLS.ClientCrt, jctx.config.TLS.ClientKey)
certPool := x509.NewCertPool()
if bs, err = ioutil.ReadFile(jctx.config.TLS.CA); err != nil {
return nil, fmt.Errorf("[%s] failed to read ca cert: %s", jctx.config.Host, err)
}
if ok := certPool.AppendCertsFromPEM(bs); !ok {
return nil, fmt.Errorf("[%s] failed to append certs", jctx.config.Host)
}
transportCreds := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{certificate},
ServerName: jctx.config.TLS.ServerName,
RootCAs: certPool,
})
return grpc.WithTransportCredentials(transportCreds), nil
}
func getGPRCDialOptions(jctx *JCtx, vendor *vendor) ([]grpc.DialOption, error) {
var opts []grpc.DialOption
if securityOpt, err := getSecurityOptions(jctx); err == nil {
opts = append(opts, securityOpt)
} else {
return nil, err
}
if *statsHandler {
opts = append(opts, grpc.WithStatsHandler(&statshandler{jctx: jctx}))
if isCsvStatsEnabled(jctx) {
jctx.config.InternalJtimon.csvLogger.Printf(fmt.Sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s\n",
"sensor-path", "sequence-number", "component-id", "sub-component-id", "packet-size", "p-ts", "e-ts", "re-stream-creation-ts", "re-payload-get-ts"))
}
}
switch *compression {
case "gzip":
compressionOpts := grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name))
opts = append(opts, compressionOpts)
jLog(jctx, "compression = gzip")
default:
jLog(jctx, "compression = none")
}
ws := jctx.config.GRPC.WS
opts = append(opts, grpc.WithInitialWindowSize(ws))
opts = append(opts, grpc.WithInitialConnWindowSize(ws))
if vendor.dialExt != nil {
opt := vendor.dialExt(jctx)
if opt != nil {
opts = append(opts, opt)
}
}
return opts, nil
}