-
Notifications
You must be signed in to change notification settings - Fork 11
/
options.go
179 lines (158 loc) · 5.48 KB
/
options.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright 2022 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package options
import (
"context"
"crypto/rand"
"crypto/tls"
"fmt"
"math"
"math/big"
"net"
"net/url"
"sync"
"time"
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/kelseyhightower/envconfig"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"knative.dev/pkg/logging"
)
type envStruct struct {
EnableClientHandlingTimeHistogram bool `envconfig:"ENABLE_CLIENT_HANDLING_TIME_HISTOGRAM" default:"true"`
EnableClientStreamReceiveTimeHistogram bool `envconfig:"ENABLE_CLIENT_STREAM_RECEIVE_TIME_HISTOGRAM" default:"true"`
EnableClientStreamSendTimeHistogram bool `envconfig:"ENABLE_CLIENT_STREAM_SEND_TIME_HISTOGRAM" default:"true"`
GrpcClientMaxRetry uint `envconfig:"GRPC_CLIENT_MAX_RETRY" default:"0"`
}
var (
envOnce sync.Once
env envStruct
)
// Parse these lazily, to allow clients to set their own in their main() or init().
func getEnv() *envStruct {
envOnce.Do(func() {
logger := logging.FromContext(context.Background())
if err := envconfig.Process("", &env); err != nil {
logger.Warn("Failed to process environment variables", "error", err)
}
})
return &env
}
// ListenerForTest is to support bufnet in our testing.
var ListenerForTest DialableListener
type DialableListener interface {
net.Listener
Dial() (net.Conn, error)
}
var listenersForTest sync.Map
// Register a test listener and get a provided scheme.
func RegisterListenerForTest(listener DialableListener) string {
for {
val, err := rand.Int(rand.Reader, big.NewInt(int64(math.MaxInt64)))
if err != nil {
panic(err)
}
scheme := fmt.Sprintf("test%d", val.Int64())
if _, conflicted := listenersForTest.LoadOrStore(scheme, listener); !conflicted {
return scheme
}
}
}
// Unregister a test listener.
func UnregisterTestListener(scheme string) {
listenersForTest.Delete(scheme)
}
func getTestListener(scheme string) (DialableListener, bool) {
v, ok := listenersForTest.Load(scheme)
if !ok {
return nil, ok
}
return v.(DialableListener), true
}
// These are defined as global variables, so that folks can expose them as flags
// in their entrypoints, if they choose.
var (
RecvMsgSize = 100 * 1024 * 1024 // 100MB
SendMsgSize = 100 * 1024 * 1024 // 100MB
)
func enableClientTimeHistogram() {
hopt := grpc_prometheus.WithHistogramBuckets(
[]float64{0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60, 120, 300, 600},
)
if getEnv().EnableClientHandlingTimeHistogram {
grpc_prometheus.EnableClientHandlingTimeHistogram(hopt)
}
if getEnv().EnableClientStreamReceiveTimeHistogram {
grpc_prometheus.EnableClientStreamReceiveTimeHistogram(hopt)
}
if getEnv().EnableClientStreamSendTimeHistogram {
grpc_prometheus.EnableClientStreamSendTimeHistogram(hopt)
}
}
func GRPCOptions(delegate url.URL) (string, []grpc.DialOption) {
retryOpts := []grpc_retry.CallOption{
grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100 * time.Millisecond)),
grpc_retry.WithMax(getEnv().GrpcClientMaxRetry),
}
switch delegate.Scheme {
case "http":
port := "80"
// Explicit port from the user signifies we should override the scheme-based defaults.
if delegate.Port() != "" {
port = delegate.Port()
}
enableClientTimeHistogram()
return net.JoinHostPort(delegate.Hostname(), port), []grpc.DialOption{
grpc.WithChainUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor, otelgrpc.UnaryClientInterceptor(), grpc_retry.UnaryClientInterceptor(retryOpts...)),
grpc.WithChainStreamInterceptor(grpc_prometheus.StreamClientInterceptor, otelgrpc.StreamClientInterceptor(), grpc_retry.StreamClientInterceptor(retryOpts...)),
grpc.WithBlock(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(RecvMsgSize),
grpc.MaxCallSendMsgSize(SendMsgSize),
),
}
case "https":
port := "443"
// Explicit port from the user signifies we should override the scheme-based defaults.
if delegate.Port() != "" {
port = delegate.Port()
}
enableClientTimeHistogram()
return net.JoinHostPort(delegate.Hostname(), port), []grpc.DialOption{
grpc.WithChainUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor, otelgrpc.UnaryClientInterceptor(), grpc_retry.UnaryClientInterceptor(retryOpts...)),
grpc.WithChainStreamInterceptor(grpc_prometheus.StreamClientInterceptor, otelgrpc.StreamClientInterceptor(), grpc_retry.StreamClientInterceptor(retryOpts...)),
grpc.WithBlock(),
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
MinVersion: tls.VersionTLS12,
})),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(RecvMsgSize),
grpc.MaxCallSendMsgSize(SendMsgSize),
),
}
case "bufnet": // This is to support testing, it will not pass webhook validation.
return "bufnet", []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return ListenerForTest.Dial()
}),
}
default:
listener, ok := getTestListener(delegate.Scheme)
if !ok {
panic("unreachable for valid delegates.")
}
return delegate.Scheme, []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return listener.Dial()
}),
}
}
}