-
Notifications
You must be signed in to change notification settings - Fork 3
/
connect.go
732 lines (612 loc) · 20.9 KB
/
connect.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
package inngestgo
import (
"context"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"github.com/coder/websocket"
"github.com/inngest/inngest/pkg/connect/types"
"github.com/inngest/inngest/pkg/connect/wsproto"
"github.com/inngest/inngest/pkg/enums"
"github.com/inngest/inngest/pkg/publicerr"
"github.com/inngest/inngest/pkg/syscode"
connectproto "github.com/inngest/inngest/proto/gen/connect/v1"
sdkerrors "github.com/inngest/inngestgo/errors"
"github.com/pbnjay/memory"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
"io"
"net/url"
"runtime"
"sync"
"github.com/inngest/inngestgo/internal/sdkrequest"
"github.com/oklog/ulid/v2"
"os"
"strings"
"time"
)
type workerPoolMsg struct {
msg *connectproto.ConnectMessage
ws *websocket.Conn
}
type connectHandler struct {
h *handler
connectionId ulid.ULID
messageBuffer []*connectproto.ConnectMessage
messageBufferLock sync.Mutex
inProgress sync.WaitGroup
workerPoolMsgs chan workerPoolMsg
}
// authContext is wrapper for information related to authentication
type authContext struct {
signingKey string
fallback bool
}
func (h *connectHandler) connectURLs() []string {
if h.h.isDev() {
return []string{fmt.Sprintf("%s/connect", strings.Replace(DevServerURL(), "http", "ws", 1))}
}
if len(h.h.ConnectURLs) > 0 {
return h.h.ConnectURLs
}
return nil
}
func (h *connectHandler) connectToGateway(ctx context.Context) (*websocket.Conn, error) {
hosts := h.connectURLs()
if len(hosts) == 0 {
return nil, fmt.Errorf("no connect URLs provided")
}
for _, gatewayHost := range hosts {
h.h.Logger.Debug("attempting to connect", "host", gatewayHost)
// Establish WebSocket connection to one of the gateways
ws, _, err := websocket.Dial(ctx, gatewayHost, &websocket.DialOptions{
Subprotocols: []string{
types.GatewaySubProtocol,
},
})
if err != nil {
// try next connection
continue
}
return ws, nil
}
return nil, fmt.Errorf("could not establish outbound connection: no available gateway host")
}
func (h *handler) Connect(ctx context.Context) error {
h.useConnect = true
concurrency := h.HandlerOpts.GetWorkerConcurrency()
// This determines how many messages can be processed by each worker at once.
ch := connectHandler{
h: h,
// Should this use the same buffer size as the worker pool?
workerPoolMsgs: make(chan workerPoolMsg, concurrency),
}
for i := 0; i < concurrency; i++ {
go ch.workerPool(ctx)
}
return ch.Connect(ctx)
}
func (h *connectHandler) instanceId() string {
if h.h.InstanceId != nil {
return *h.h.InstanceId
}
hostname, _ := os.Hostname()
if hostname != "" {
return hostname
}
// TODO Is there any stable identifier that can be used as a fallback?
return "<missing-instance-id>"
}
func (h *connectHandler) workerPool(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case msg := <-h.workerPoolMsgs:
h.processExecutorRequest(msg.ws, msg.msg)
}
}
}
func (h *connectHandler) Connect(ctx context.Context) error {
signingKey := h.h.GetSigningKey()
if signingKey == "" {
return fmt.Errorf("must provide signing key")
}
auth := authContext{signingKey: signingKey}
numCpuCores := runtime.NumCPU()
totalMem := memory.TotalMemory()
connectPlaceholder := url.URL{
Scheme: "ws",
Host: "connect",
}
fns, err := createFunctionConfigs(h.h.appName, h.h.funcs, connectPlaceholder, true)
if err != nil {
return fmt.Errorf("error creating function configs: %w", err)
}
marshaledFns, err := json.Marshal(fns)
if err != nil {
return fmt.Errorf("failed to serialize connect config: %w", err)
}
marshaledCapabilities, err := json.Marshal(capabilities)
if err != nil {
return fmt.Errorf("failed to serialize connect config: %w", err)
}
var attempts int
for {
attempts++
if attempts == 5 {
return fmt.Errorf("could not establish connection after 5 attempts")
}
shouldReconnect, err := h.connect(ctx, connectionEstablishData{
signingKey: auth.signingKey,
numCpuCores: int32(numCpuCores),
totalMem: int64(totalMem),
marshaledFns: marshaledFns,
marshaledCapabilities: marshaledCapabilities,
})
h.h.Logger.Error("connect failed", "err", err, "reconnect", shouldReconnect)
if !shouldReconnect {
return err
}
closeErr := websocket.CloseError{}
if errors.As(err, &closeErr) {
switch closeErr.Reason {
// If auth failed, retry with fallback key
case syscode.CodeConnectAuthFailed:
if auth.fallback {
return fmt.Errorf("failed to authenticate with fallback key, exiting")
}
signingKeyFallback := h.h.GetSigningKeyFallback()
if signingKeyFallback != "" {
auth = authContext{signingKey: signingKeyFallback, fallback: true}
}
continue
// Retry on the following error codes
case syscode.CodeConnectGatewayClosing, syscode.CodeConnectInternal, syscode.CodeConnectWorkerHelloTimeout:
continue
default:
// If we received a reason that's non-retriable, stop here.
return fmt.Errorf("connect failed with error code %q", closeErr.Reason)
}
}
}
}
type connectionEstablishData struct {
signingKey string
numCpuCores int32
totalMem int64
marshaledFns []byte
marshaledCapabilities []byte
manualReadinessAck bool
}
func (h *connectHandler) prepareConnection(ctx context.Context, data connectionEstablishData) (*websocket.Conn, bool, error) {
connectTimeout, cancelConnectTimeout := context.WithTimeout(ctx, 10*time.Second)
defer cancelConnectTimeout()
ws, err := h.connectToGateway(connectTimeout)
if err != nil {
return nil, false, fmt.Errorf("could not connect: %w", err)
}
// Connection ID is unique per connection, reconnections should get a new ID
h.connectionId = ulid.MustNew(ulid.Now(), rand.Reader)
h.h.Logger.Debug("websocket connection established")
// Wait for gateway hello message
{
initialMessageTimeout, cancelInitialTimeout := context.WithTimeout(ctx, 5*time.Second)
defer cancelInitialTimeout()
var helloMessage connectproto.ConnectMessage
err = wsproto.Read(initialMessageTimeout, ws, &helloMessage)
if err != nil {
return nil, true, fmt.Errorf("did not receive gateway hello message: %w", err)
}
if helloMessage.Kind != connectproto.GatewayMessageType_GATEWAY_HELLO {
return nil, true, fmt.Errorf("expected gateway hello message, got %s", helloMessage.Kind)
}
h.h.Logger.Debug("received gateway hello message")
}
// Send connect message
{
hashedKey, err := hashedSigningKey([]byte(data.signingKey))
if err != nil {
return nil, false, fmt.Errorf("could not hash signing key: %w", err)
}
apiOrigin := h.h.GetAPIBaseURL()
if h.h.isDev() {
apiOrigin = DevServerURL()
}
data, err := proto.Marshal(&connectproto.WorkerConnectRequestData{
SessionId: &connectproto.SessionIdentifier{
BuildId: h.h.BuildId,
InstanceId: h.instanceId(),
ConnectionId: h.connectionId.String(),
},
AuthData: &connectproto.AuthData{
HashedSigningKey: hashedKey,
},
AppName: h.h.appName,
Config: &connectproto.ConfigDetails{
Capabilities: data.marshaledCapabilities,
Functions: data.marshaledFns,
ApiOrigin: apiOrigin,
},
SystemAttributes: &connectproto.SystemAttributes{
CpuCores: data.numCpuCores,
MemBytes: data.totalMem,
Os: runtime.GOOS,
},
Environment: h.h.Env,
Platform: Ptr(platform()),
SdkVersion: SDKVersion,
SdkLanguage: SDKLanguage,
WorkerManualReadinessAck: data.manualReadinessAck,
})
if err != nil {
return nil, false, fmt.Errorf("could not serialize sdk connect message: %w", err)
}
err = wsproto.Write(ctx, ws, &connectproto.ConnectMessage{
Kind: connectproto.GatewayMessageType_WORKER_CONNECT,
Payload: data,
})
if err != nil {
return nil, true, fmt.Errorf("could not send initial message")
}
}
// Wait for gateway ready message
{
connectionReadyTimeout, cancelConnectionReadyTimeout := context.WithTimeout(ctx, 20*time.Second)
defer cancelConnectionReadyTimeout()
var connectionReadyMsg connectproto.ConnectMessage
err = wsproto.Read(connectionReadyTimeout, ws, &connectionReadyMsg)
if err != nil {
return nil, true, fmt.Errorf("did not receive gateway connection ready message: %w", err)
}
if connectionReadyMsg.Kind != connectproto.GatewayMessageType_GATEWAY_CONNECTION_READY {
return nil, true, fmt.Errorf("expected gateway connection ready message, got %s", connectionReadyMsg.Kind)
}
h.h.Logger.Debug("received gateway connection ready message")
}
return ws, false, nil
}
func (h *connectHandler) sendBufferedMessages(ws *websocket.Conn) error {
processed := 0
for _, msg := range h.messageBuffer {
// always send the message, even if the context is canceled
err := wsproto.Write(context.Background(), ws, msg)
if err != nil {
// Only send buffered messages once
h.messageBuffer = h.messageBuffer[processed:]
h.h.Logger.Error("failed to send buffered message", "err", err)
return fmt.Errorf("could not send buffered message: %w", err)
}
h.h.Logger.Debug("sent buffered message", "msg", msg)
processed++
}
h.messageBuffer = nil
return nil
}
func (h *connectHandler) connect(ctx context.Context, data connectionEstablishData) (reconnect bool, err error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ws, reconnect, err := h.prepareConnection(ctx, data)
if err != nil {
return reconnect, fmt.Errorf("could not establish connection: %w", err)
}
defer func() {
// TODO Do we need to include a reason here? If we only use this for unexpected disconnects, probably not
_ = ws.CloseNow()
}()
// Send buffered but unsent messages if connection was re-established
if len(h.messageBuffer) > 0 {
h.h.Logger.Debug("sending buffered messages", "count", len(h.messageBuffer))
err = h.sendBufferedMessages(ws)
if err != nil {
return true, fmt.Errorf("could not send buffered messages: %w", err)
}
}
eg := errgroup.Group{}
eg.Go(func() error {
for {
if ctx.Err() != nil {
return ctx.Err()
}
var msg connectproto.ConnectMessage
err = wsproto.Read(ctx, ws, &msg)
if err != nil {
h.h.Logger.Error("failed to read message", "err", err)
// The connection may still be active, but for some reason we couldn't read the message
return err
}
h.h.Logger.Debug("received gateway request", "msg", &msg)
switch msg.Kind {
case connectproto.GatewayMessageType_GATEWAY_EXECUTOR_REQUEST:
// Handle invoke in a non-blocking way to allow for other messages to be processed
h.inProgress.Add(1)
h.workerPoolMsgs <- workerPoolMsg{
msg: &msg,
ws: ws,
}
default:
h.h.Logger.Error("got unknown gateway request", "err", err)
continue
}
}
})
h.h.Logger.Debug("waiting for read loop to end")
// If read loop ends, this can be for two reasons
// - Connection loss (io.EOF), read loop terminated intentionally (CloseError), other error (unexpected)
// - Worker shutdown, parent context got canceled
if err := eg.Wait(); err != nil {
h.h.Logger.Debug("read loop ended with error", "err", err)
// In case the gateway intentionally closed the connection, we'll receive a close error
cerr := websocket.CloseError{}
if errors.As(err, &cerr) {
h.h.Logger.Error("connection closed with reason", "reason", cerr.Reason)
// Reconnect!
return true, fmt.Errorf("connection closed with reason %q: %w", cerr.Reason, cerr)
}
// connection closed without reason
if errors.Is(err, io.EOF) {
h.h.Logger.Error("failed to read message from gateway, lost connection unexpectedly", "err", err)
return true, fmt.Errorf("connection closed unexpectedly: %w", cerr)
}
// If this is not a worker shutdown, we should reconnect
if ctx.Err() == nil {
return true, fmt.Errorf("connection closed unexpectedly: %w", ctx.Err())
}
}
// Perform graceful shutdown routine
// Signal gateway that we won't process additional messages!
{
h.h.Logger.Debug("sending worker pause message")
err := wsproto.Write(context.Background(), ws, &connectproto.ConnectMessage{
Kind: connectproto.GatewayMessageType_WORKER_PAUSE,
})
if err != nil {
// We should not exit here, as we're already in the shutdown routine
h.h.Logger.Error("failed to serialize worker pause msg", "err", err)
}
}
h.h.Logger.Debug("waiting for in-progress requests to finish")
// Wait until all in-progress requests are completed
h.inProgress.Wait()
// Send out buffered messages, using new connection if necessary!
h.messageBufferLock.Lock()
defer h.messageBufferLock.Unlock()
if len(h.messageBuffer) > 0 {
attempts := 0
for {
attempts++
if attempts == 3 {
h.h.Logger.Error("could not establish connection after 3 attempts")
break
}
reconnect, err = h.withTemporaryConnection(data, func(ws *websocket.Conn) error {
// Send buffered messages
err := h.sendBufferedMessages(ws)
if err != nil {
return fmt.Errorf("could not send buffered messages: %w", err)
}
return nil
})
if err != nil {
if !reconnect {
h.h.Logger.Error("could not establish connection for sending buffered messages", "err", err)
break
}
continue
}
}
}
// Attempt to shut down connection if not already done
_ = ws.Close(websocket.StatusNormalClosure, connectproto.WorkerDisconnectReason_WORKER_SHUTDOWN.String())
return false, nil
}
func (h *connectHandler) withTemporaryConnection(data connectionEstablishData, handler func(ws *websocket.Conn) error) (bool, error) {
// Prevent this connection from receiving work
data.manualReadinessAck = true
ws, reconnect, err := h.prepareConnection(context.Background(), data)
if err != nil {
return reconnect, fmt.Errorf("could not establish temporary connection: %w", err)
}
defer func() {
_ = ws.Close(websocket.StatusNormalClosure, connectproto.WorkerDisconnectReason_WORKER_SHUTDOWN.String())
}()
err = handler(ws)
if err != nil {
return false, err
}
return false, nil
}
func (h *connectHandler) processExecutorRequest(ws *websocket.Conn, msg *connectproto.ConnectMessage) {
defer h.inProgress.Done()
// Always make sure the invoke finishes properly
processCtx := context.Background()
err := h.handleInvokeMessage(processCtx, ws, msg)
// When we encounter an error, we cannot retry the connection from inside the goroutine.
// If we're dealing with connection loss, the next read loop will fail with the same error
// and handle the reconnection.
if err != nil {
cerr := websocket.CloseError{}
if errors.As(err, &cerr) {
h.h.Logger.Error("gateway connection closed with reason", "reason", cerr.Reason)
return
}
if errors.Is(err, io.EOF) {
h.h.Logger.Error("gateway connection closed unexpectedly", "err", err)
return
}
// TODO If error is not connection-related, should we retry? Send the buffered message?
}
}
func (h *connectHandler) handleInvokeMessage(ctx context.Context, ws *websocket.Conn, msg *connectproto.ConnectMessage) error {
resp, err := h.connectInvoke(ctx, ws, msg)
if err != nil {
h.h.Logger.Error("failed to handle sdk request", "err", err)
// TODO Should we drop the connection? Continue receiving messages?
return fmt.Errorf("could not handle sdk request: %w", err)
}
data, err := proto.Marshal(resp)
if err != nil {
h.h.Logger.Error("failed to serialize sdk response", "err", err)
// TODO This should never happen; Signal that we should retry
return fmt.Errorf("could not serialize sdk response: %w", err)
}
responseMessage := &connectproto.ConnectMessage{
Kind: connectproto.GatewayMessageType_WORKER_REPLY,
Payload: data,
}
err = wsproto.Write(ctx, ws, responseMessage)
if err != nil {
h.h.Logger.Error("failed to send sdk response", "err", err)
// Buffer message to retry
h.messageBufferLock.Lock()
h.messageBuffer = append(h.messageBuffer, responseMessage)
h.messageBufferLock.Unlock()
return fmt.Errorf("could not send sdk response: %w", err)
}
return nil
}
// connectInvoke is the counterpart to invoke for connect
func (h *connectHandler) connectInvoke(ctx context.Context, ws *websocket.Conn, msg *connectproto.ConnectMessage) (*connectproto.SDKResponse, error) {
body := connectproto.GatewayExecutorRequestData{}
if err := proto.Unmarshal(msg.Payload, &body); err != nil {
// TODO Should we send this back to the gateway?
h.h.Logger.Error("error decoding gateway request data", "error", err)
return nil, fmt.Errorf("invalid gateway message data: %w", err)
}
// Note: This still uses JSON
// TODO Replace with Protobuf
var request sdkrequest.Request
if err := json.Unmarshal(body.RequestPayload, &request); err != nil {
// TODO Should we send this back to the gateway? Previously this was a status code 400 public error with "malformed input"
h.h.Logger.Error("error decoding sdk request", "error", err)
return nil, fmt.Errorf("invalid SDK request payload: %w", err)
}
ackPayload, err := proto.Marshal(&connectproto.WorkerRequestAckData{
RequestId: body.RequestId,
AppId: body.AppId,
FunctionSlug: body.FunctionSlug,
StepId: body.StepId,
})
if err != nil {
h.h.Logger.Error("error marshaling request ack", "error", err)
return nil, publicerr.Error{
Message: "malformed input",
Status: 400,
}
}
// Ack message
if err := wsproto.Write(ctx, ws, &connectproto.ConnectMessage{
Kind: connectproto.GatewayMessageType_WORKER_REQUEST_ACK,
Payload: ackPayload,
}); err != nil {
h.h.Logger.Error("error sending request ack", "error", err)
return nil, publicerr.Error{
Message: "failed to ack worker request",
Status: 400,
}
}
// TODO Should we wait for a gateway response before starting to process? What if the gateway fails acking and we start too early?
// This should not happen but could lead to double processing of the same message
if request.UseAPI {
// TODO: implement this
// retrieve data from API
// request.Steps =
// request.Events =
_ = 0 // no-op to avoid linter error
}
h.h.l.RLock()
var fn ServableFunction
for _, f := range h.h.funcs {
if f.Slug() == body.FunctionSlug {
fn = f
break
}
}
h.h.l.RUnlock()
if fn == nil {
// XXX: This is a 500 within the JS SDK. We should probably change
// the JS SDK's status code to 410. 404 indicates that the overall
// API for serving Inngest isn't found.
return nil, publicerr.Error{
Message: fmt.Sprintf("function not found: %s", body.FunctionSlug),
Status: 410,
}
}
var stepId *string
if body.StepId != nil && *body.StepId != "step" {
stepId = body.StepId
}
resp, ops, err := invoke(ctx, fn, &request, stepId)
// NOTE: When triggering step errors, we should have an OpcodeStepError
// within ops alongside an error. We can safely ignore that error, as it's
// only used for checking whether the step used a NoRetryError or RetryAtError
//
// For that reason, we check those values first.
noRetry := sdkerrors.IsNoRetryError(err)
retryAt := sdkerrors.GetRetryAtTime(err)
if len(ops) == 1 && ops[0].Op == enums.OpcodeStepError {
// Now we've handled error types we can ignore step
// errors safely.
err = nil
}
// Now that we've handled the OpcodeStepError, if we *still* ahve
// a StepError kind returned from a function we must have an unhandled
// step error. This is a NonRetryableError, as the most likely code is:
//
// _, err := step.Run(ctx, func() (any, error) { return fmt.Errorf("") })
// if err != nil {
// return err
// }
if sdkerrors.IsStepError(err) {
err = fmt.Errorf("Unhandled step error: %s", err)
noRetry = true
}
// These may be added even for 2xx codes with step errors.
var retryAfterVal *string
if retryAt != nil {
retryAfterVal = StrPtr(retryAt.Format(time.RFC3339))
}
if err != nil {
h.h.Logger.Error("error calling function", "error", err)
return &connectproto.SDKResponse{
RequestId: body.RequestId,
Status: connectproto.SDKResponseStatus_ERROR,
Body: []byte(fmt.Sprintf("error calling function: %s", err.Error())),
NoRetry: noRetry,
RetryAfter: retryAfterVal,
}, nil
}
if len(ops) > 0 {
// Note: This still uses JSON
// TODO Replace with Protobuf
serializedOps, err := json.Marshal(ops)
if err != nil {
return nil, fmt.Errorf("could not serialize ops: %w", err)
}
// Return the function opcode returned here so that we can re-invoke this
// function and manage state appropriately. Any opcode here takes precedence
// over function return values as the function has not yet finished.
return &connectproto.SDKResponse{
RequestId: body.RequestId,
Status: connectproto.SDKResponseStatus_NOT_COMPLETED,
Body: serializedOps,
NoRetry: noRetry,
RetryAfter: retryAfterVal,
}, nil
}
// Note: This still uses JSON
// TODO Replace with Protobuf
serializedResp, err := json.Marshal(resp)
if err != nil {
return nil, fmt.Errorf("could not serialize resp: %w", err)
}
// Return the function response.
return &connectproto.SDKResponse{
RequestId: body.RequestId,
Status: connectproto.SDKResponseStatus_DONE,
Body: serializedResp,
NoRetry: noRetry,
RetryAfter: retryAfterVal,
}, nil
}