forked from open-cluster-management-io/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransport.go
More file actions
312 lines (256 loc) · 8.32 KB
/
Copy pathtransport.go
File metadata and controls
312 lines (256 loc) · 8.32 KB
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
package grpc
import (
"context"
"fmt"
"sync"
"time"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/binding"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
"open-cluster-management.io/sdk-go/pkg/cloudevents/constants"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options"
grpcoptions "open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options/grpc"
pbv1 "open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options/grpc/protobuf/v1"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options/grpc/protocol"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types"
"open-cluster-management.io/sdk-go/pkg/cloudevents/server/grpc/heartbeat"
)
type subscriptionRequestGetter func() *pbv1.SubscriptionRequest
type grpcTransport struct {
opts *grpcoptions.GRPCOptions
client pbv1.CloudEventServiceClient
subClient pbv1.CloudEventService_SubscribeClient
subID string
subscribed bool
mu sync.RWMutex
closeChan chan struct{}
errorChan chan error
getSubscriptionRequest subscriptionRequestGetter
}
func newTransport(grpcOptions *grpcoptions.GRPCOptions, getter subscriptionRequestGetter) *grpcTransport {
return &grpcTransport{
opts: grpcOptions,
errorChan: make(chan error),
getSubscriptionRequest: getter,
}
}
func (t *grpcTransport) Connect(ctx context.Context) error {
t.mu.Lock()
defer t.mu.Unlock()
logger := klog.FromContext(ctx)
conn, err := t.opts.Dialer.Dial()
if err != nil {
return err
}
t.client = pbv1.NewCloudEventServiceClient(conn)
// Initialize closeChan to support reconnect cycles
t.closeChan = make(chan struct{})
// Start a goroutine to monitor the gRPC connection state changes
go t.monitorConnectionState(ctx, conn)
logger.Info("grpc is connected", "grpcURL", t.opts.Dialer.URL)
return nil
}
func (t *grpcTransport) Send(ctx context.Context, evt cloudevents.Event) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.client == nil {
return fmt.Errorf("transport not connected")
}
pbEvt := &pbv1.CloudEvent{}
if err := protocol.WritePBMessage(ctx, (*binding.EventMessage)(&evt), pbEvt); err != nil {
return err
}
if _, err := t.client.Publish(ctx, &pbv1.PublishRequest{Event: pbEvt}); err != nil {
return err
}
return nil
}
func (t *grpcTransport) Subscribe(ctx context.Context) error {
logger := klog.FromContext(ctx)
t.mu.Lock()
defer t.mu.Unlock()
if t.client == nil {
return fmt.Errorf("transport not connected")
}
if t.subscribed {
return fmt.Errorf("transport has already subscribed")
}
subOption := t.getSubscriptionRequest()
subClient, err := t.client.Subscribe(ctx, subOption)
if err != nil {
return err
}
// Wait for server to send subscription-id
header, err := subClient.Header()
if err != nil {
return err
}
values := header.Get(constants.GRPCSubscriptionIDKey)
if len(values) != 1 {
// Header() succeeded but no subscription-id was sent (header is nil or empty).
// This typically means the server rejected the subscription before sending headers
// (e.g., authorization failure). The actual error is only available via Recv().
// Call Recv() to get the real error from the server.
recvErrCh := make(chan error, 1)
go func() {
_, err := subClient.Recv()
recvErrCh <- err
}()
select {
case recvErr := <-recvErrCh:
if recvErr != nil {
return recvErr
}
case <-ctx.Done():
return ctx.Err()
case <-time.After(5 * time.Second):
_ = subClient.CloseSend()
return fmt.Errorf("no subscription-id in header (%v): recv timeout", header)
}
// If Recv() didn't return an error, this is a server-side configuration issue
return fmt.Errorf("no subscription-id in header (%v)", header)
}
t.subID = values[0]
t.subClient = subClient
t.subscribed = true
logger.Info("subscribed to grpc server", "subID", t.subID, "subOption", subOption)
return nil
}
// Receive starts receiving events and invokes the provided handler for each event.
// This is a BLOCKING call that runs an event loop until the context is cancelled.
func (t *grpcTransport) Receive(ctx context.Context, handleFn options.ReceiveHandlerFn) error {
t.mu.Lock()
if t.subClient == nil {
t.mu.Unlock()
return fmt.Errorf("transport not subscribed")
}
t.mu.Unlock()
logger := klog.FromContext(ctx).WithValues("subID", t.subID)
subOption := t.getSubscriptionRequest()
subCtx, cancel := context.WithCancel(ctx)
subCtx = klog.NewContext(subCtx, logger)
healthChecker := heartbeat.NewHealthChecker(t.opts.ServerHealthinessTimeout, t.errorChan)
// start to receive the events from stream
logger.Info("receiving events", "subOption", subOption)
go t.startEventsReceiver(subCtx, healthChecker.Input(), handleFn)
// start to watch the stream heartbeat
go healthChecker.Start(subCtx)
// wait until external or internal context done
select {
case <-ctx.Done():
case <-t.getCloseChan():
}
// ensure the event receiver and heartbeat watcher are done
cancel()
logger.Info("stop receiving events", "subID", t.subID, "subOption", subOption)
return nil
}
func (t *grpcTransport) ErrorChan() <-chan error {
return t.errorChan
}
func (t *grpcTransport) Close(ctx context.Context) error {
t.mu.Lock()
defer t.mu.Unlock()
klog.FromContext(ctx).Info("close grpc transport")
// Guard against double-close panic and nil channel
if t.closeChan != nil {
select {
case <-t.closeChan:
// Already closed
default:
close(t.closeChan)
}
}
t.subscribed = false
return t.opts.Dialer.Close()
}
func (t *grpcTransport) startEventsReceiver(ctx context.Context,
heartbeatCh chan *pbv1.CloudEvent,
handleFn options.ReceiveHandlerFn) {
logger := klog.FromContext(ctx)
for {
pbEvt, err := t.subClient.Recv()
if err != nil {
st, ok := status.FromError(err)
if ok && st.Code() == codes.Canceled {
// canceled by transport, return directly.
return
}
select {
case t.errorChan <- fmt.Errorf("subscribe (subID=%s) stream failed: %w", t.subID, err):
default:
logger.Error(err, "subscribe stream failed", "subID", t.subID)
}
return
}
if pbEvt.Type == types.HeartbeatCloudEventsType {
select {
case heartbeatCh <- pbEvt:
case <-ctx.Done():
return
}
continue
}
evt, err := binding.ToEvent(ctx, protocol.NewMessage(pbEvt))
if err != nil {
logger.Error(err, "invalid event")
continue
}
handleFn(ctx, *evt)
select {
case <-ctx.Done():
return
case <-t.getCloseChan():
return
default:
// Continue receiving more events
}
}
}
func (t *grpcTransport) getCloseChan() chan struct{} {
t.mu.RLock()
defer t.mu.RUnlock()
return t.closeChan
}
// monitorConnectionState monitors the gRPC connection state changes and reports errors
// when the connection becomes disconnected.
func (t *grpcTransport) monitorConnectionState(ctx context.Context, conn *grpc.ClientConn) {
logger := klog.FromContext(ctx)
state := conn.GetState()
for {
if !conn.WaitForStateChange(ctx, state) {
// the ctx is closed, stop this watch
logger.Info("stop watching grpc connection state")
return
}
newState := conn.GetState()
// If any failure in any of the steps needed to establish connection, or any failure
// encountered while expecting successful communication on established channel, the
// grpc client connection state will be TransientFailure.
// When client certificate is expired, client will proactively close the connection,
// which will result in connection state changed from Ready to Shutdown.
// When server is closed, client will NOT close or reestablish the connection proactively,
// it will only change the connection state from Ready to Idle.
if newState == connectivity.TransientFailure ||
newState == connectivity.Shutdown ||
newState == connectivity.Idle {
select {
case t.errorChan <- fmt.Errorf("grpc connection is disconnected (state=%s)", newState):
default:
logger.Info("no error channel available to report grpc disconnected", "state", newState)
}
if newState != connectivity.Shutdown {
// don't close the connection if it's already shutdown
if err := conn.Close(); err != nil {
logger.Error(err, "failed to close grpc connection")
}
}
return // exit the goroutine as the error handler function will handle the reconnection.
}
state = newState
}
}