generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
rpc.go
357 lines (317 loc) · 9.96 KB
/
rpc.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package rpc
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"os"
"strings"
"syscall"
"time"
"connectrpc.com/connect"
"github.com/jpillora/backoff"
"golang.org/x/net/http2"
"github.com/block/ftl/internal/authn"
"github.com/block/ftl/internal/log"
)
// PingResponse is a constraint that is used to enforce that a pointer to the [Pingable] response message has a
// GetNotReady() method.
type PingResponse[T any] interface {
*T
GetNotReady() string
}
// Pingable is an interface that is used to indicate that a client can be pinged.
type Pingable[Req any, Resp any, RespPtr PingResponse[Resp]] interface {
Ping(ctx context.Context, req *connect.Request[Req]) (*connect.Response[Resp], error)
}
// InitialiseClients initialises global HTTP clients used by the RPC system.
//
// "authenticators" are authenticator executables to use for each endpoint. The key is the URL of the endpoint, the
// value is the path to the authenticator executable.
//
// "allowInsecure" skips certificate verification, making TLS susceptible to machine-in-the-middle attacks.
func InitialiseClients(authenticators map[string]string, allowInsecure bool) {
// We can't have a client-wide timeout because it also applies to
// streaming RPCs, timing them out.
h2cClient = &http.Client{
Transport: authn.Transport(&http2.Transport{
AllowHTTP: true,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: allowInsecure, // #nosec G402
},
DialTLSContext: func(ctx context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
conn, err := dialer.Dial(network, addr)
return conn, err
},
}, authenticators),
}
tlsClient = &http.Client{
Transport: authn.Transport(&http2.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: allowInsecure, // #nosec G402
},
DialTLSContext: func(ctx context.Context, network, addr string, config *tls.Config) (net.Conn, error) {
tlsDialer := tls.Dialer{Config: config, NetDialer: dialer}
conn, err := tlsDialer.DialContext(ctx, network, addr)
return conn, err
},
}, authenticators),
}
// Use a separate client for HTTP/1.1 with TLS.
http1TLSClient = &http.Client{
Transport: authn.Transport(&http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: allowInsecure, // #nosec G402
},
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
logger := log.FromContext(ctx)
logger.Debugf("HTTP/1.1 connecting to %s %s", network, addr)
tlsDialer := tls.Dialer{NetDialer: dialer}
conn, err := tlsDialer.DialContext(ctx, network, addr)
return conn, fmt.Errorf("HTTP/1.1 TLS dial failed: %w", err)
},
}, authenticators),
}
}
func init() {
InitialiseClients(map[string]string{}, false)
}
var (
dialer = &net.Dialer{
Timeout: time.Second * 10,
}
h2cClient *http.Client
tlsClient *http.Client
// Temporary client for HTTP/1.1 with TLS to help with debugging.
http1TLSClient *http.Client
)
// GetHTTPClient returns a HTTP client usable for the given URL.
func GetHTTPClient(url string) *http.Client {
if h2cClient == nil {
panic("rpc.InitialiseClients() must be called before GetHTTPClient()")
}
// TEMP_GRPC_HTTP1_ONLY set to non blank will use http1TLSClient
if os.Getenv("TEMP_GRPC_HTTP1_ONLY") != "" {
return http1TLSClient
}
if strings.HasPrefix(url, "http://") {
return h2cClient
}
return tlsClient
}
// ClientFactory is a function that creates a new client and is typically one of
// the New*Client functions generated by protoc-gen-connect-go.
type ClientFactory[Client Pingable[Req, Resp, RespPtr], Req any, Resp any, RespPtr PingResponse[Resp]] func(httpClient connect.HTTPClient, baseURL string, opts ...connect.ClientOption) Client
func Dial[Client Pingable[Req, Resp, RespPtr], Req any, Resp any, RespPtr PingResponse[Resp]](factory ClientFactory[Client, Req, Resp, RespPtr], baseURL string, errorLevel log.Level, opts ...connect.ClientOption) Client {
client := GetHTTPClient(baseURL)
opts = append(opts, DefaultClientOptions(errorLevel)...)
return factory(client, baseURL, opts...)
}
// ContextValuesMiddleware injects values from a Context into the request Context.
func ContextValuesMiddleware(ctx context.Context, handler http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(mergedContext{values: ctx, Context: r.Context()})
handler.ServeHTTP(w, r)
}
}
var _ context.Context = (*mergedContext)(nil)
type mergedContext struct {
values context.Context
context.Context
}
func (m mergedContext) Value(key any) any {
if value := m.Context.Value(key); value != nil {
return value
}
return m.values.Value(key)
}
type noopLogSync struct{}
var _ log.Sink = noopLogSync{}
func (noopLogSync) Log(entry log.Entry) error { return nil }
// Wait for a client to become available.
//
// This will repeatedly call Ping() according to the retry policy until the client is
// ready or the deadline is reached.
//
// If "ctx" is cancelled this will return ctx.Err()
//
// Usually rpc errors are logged, but this function will silence ping call errors, and
// returns the last error if the deadline is reached.
func Wait[Req any, Resp any, RespPtr PingResponse[Resp]](ctx context.Context, retry backoff.Backoff, deadline time.Duration, client Pingable[Req, Resp, RespPtr]) error {
errChan := make(chan error)
ctx, cancel := context.WithTimeout(ctx, deadline)
defer cancel()
go func() {
logger := log.FromContext(ctx)
// create a context logger with a new one that does not log debug messages (which include each ping call failures)
silencedCtx := log.ContextWithLogger(ctx, log.New(log.Error, noopLogSync{}))
start := time.Now()
// keep track of the last ping error
var err error
for {
select {
case <-ctx.Done():
if err != nil && errors.Is(ctx.Err(), context.DeadlineExceeded) {
errChan <- err
} else {
errChan <- ctx.Err()
}
return
default:
}
var req *Req
var resp *connect.Response[Resp]
resp, err = client.Ping(silencedCtx, connect.NewRequest(req))
if err == nil {
if (RespPtr)(resp.Msg).GetNotReady() == "" {
logger.Debugf("Ping succeeded in %.2fs", time.Since(start).Seconds())
errChan <- nil
return
}
err = fmt.Errorf("service is not ready: %s", (RespPtr)(resp.Msg).GetNotReady())
}
delay := retry.Duration()
logger.Tracef("Ping failed waiting %s for client: %+v", delay, err)
time.Sleep(delay)
}
}()
err := <-errChan
if err != nil {
return err
}
return nil
}
// RetryStreamingClientStream will repeatedly call handler with the stream
// returned by "rpc" until handler returns an error or the context is cancelled.
//
// If the stream errors, it will be closed and a new call will be issued.
func RetryStreamingClientStream[Req any, Resp any](
ctx context.Context,
retry backoff.Backoff,
rpc func(context.Context) *connect.ClientStreamForClient[Req, Resp],
handler func(ctx context.Context, send func(*Req) error) error,
) {
logLevel := log.Debug
errored := false
logger := log.FromContext(ctx)
for {
stream := rpc(ctx)
var err error
for {
err = handler(ctx, stream.Send)
if err != nil {
break
}
if errored {
logger.Debugf("Client stream recovered")
errored = false
}
select {
case <-ctx.Done():
return
default:
}
retry.Reset()
logLevel = log.Warn
}
// We've hit an error.
_, closeErr := stream.CloseAndReceive()
if closeErr != nil {
logger.Logf(log.Debug, "Failed to close stream: %s", closeErr)
}
errored = true
delay := retry.Duration()
if !errors.Is(err, context.Canceled) {
logger.Logf(logLevel, "Stream handler failed retrying in %s: %s", delay, err)
}
select {
case <-ctx.Done():
return
case <-time.After(delay):
}
}
}
// AlwaysRetry instructs RetryStreamingServerStream to always retry the errors it encounters when
// supplied as the errorRetryCallback argument
func AlwaysRetry() func(error) bool {
return func(err error) bool { return true }
}
// RetryStreamingServerStream will repeatedly call handler with responses from
// the stream returned by "rpc" until either the context is cancelled or the
// errorRetryCallback returns false.
func RetryStreamingServerStream[Req, Resp any](
ctx context.Context,
name string,
retry backoff.Backoff,
req *Req,
rpc func(context.Context, *connect.Request[Req]) (*connect.ServerStreamForClient[Resp], error),
handler func(ctx context.Context, resp *Resp) error,
errorRetryCallback func(err error) bool,
) {
logLevel := log.Trace
errored := false
logger := log.FromContext(ctx)
for {
stream, err := rpc(ctx, connect.NewRequest(req))
if err == nil {
for {
if stream.Receive() {
resp := stream.Msg()
err = handler(ctx, resp)
if err != nil {
break
}
if errored {
logger.Tracef("Server stream recovered")
errored = false
}
select {
case <-ctx.Done():
err := stream.Close()
if err != nil {
logger.Debugf("Failed to close stream: %s", err)
}
return
default:
}
retry.Reset()
logLevel = log.Warn
} else {
// Stream terminated; check if this was caused by an error
err = stream.Err()
logLevel = logLevelForError(err)
break
}
}
err := stream.Close()
if err != nil {
logger.Debugf("Failed to close stream: %s", err)
}
}
errored = true
delay := retry.Duration()
if err != nil && !errors.Is(err, context.Canceled) {
if errorRetryCallback != nil && !errorRetryCallback(err) {
logger.Errorf(err, "Stream handler encountered a non-retryable error")
return
}
logger.Logf(logLevel, "Stream handler failed for %s, retrying in %s: %s", name, delay, err)
} else if err == nil {
logger.Debugf("Stream finished, retrying in %s", delay)
}
select {
case <-ctx.Done():
return
case <-time.After(delay):
}
}
}
// logLevelForError indicates the log.Level to use for the specified error
func logLevelForError(err error) log.Level {
if err != nil && errors.Is(err, syscall.ECONNREFUSED) {
return log.Trace
}
return log.Warn
}