Skip to content

Commit 471b19a

Browse files
committed
supporting heartbeat in cloudevents subscribe stream
Signed-off-by: Wei Liu <liuweixa@redhat.com>
1 parent fd0d607 commit 471b19a

1 file changed

Lines changed: 38 additions & 48 deletions

File tree

  • pkg/cloudevents/generic/options/grpc/protocol

pkg/cloudevents/generic/options/grpc/protocol/protocol.go

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -130,49 +130,19 @@ func (p *Protocol) OpenInbound(ctx context.Context) error {
130130
subCtx, cancel := context.WithCancel(ctx)
131131
heartbeatCh := make(chan *pbv1.CloudEvent, 1)
132132

133-
// start to receive the events
134-
go func() {
135-
// close the heartbeat channel when stopping to receive the events
136-
defer close(heartbeatCh)
137-
138-
for {
139-
evt, err := subClient.Recv()
140-
if err != nil {
141-
select {
142-
case p.reconnectErrorChan <- fmt.Errorf("subscribe stream failed: %w", err):
143-
default:
144-
// error chan is full, do nothing
145-
}
146-
return
147-
}
148-
149-
if evt.Type == types.HeartbeatCloudEventsType {
150-
select {
151-
case heartbeatCh <- evt:
152-
case <-ctx.Done():
153-
return
154-
}
155-
continue
156-
}
157-
158-
select {
159-
case p.incoming <- evt:
160-
case <-ctx.Done():
161-
return
162-
}
163-
}
164-
}()
133+
// start to receive the events from stream
134+
go p.startEventsReceiver(subCtx, subClient, heartbeatCh)
165135

166136
// start to watch the stream heartbeat
167-
go p.startHeartbeatWatcher(ctx, heartbeatCh)
137+
go p.startHeartbeatWatcher(subCtx, heartbeatCh)
168138

169139
// Wait until external or internal context done
170140
select {
171141
case <-subCtx.Done():
172142
case <-p.closeChan:
173143
}
174144

175-
// ensure the event receiving and heartbeat checking goroutines are done
145+
// ensure the event receiver and heartbeat watcher are done
176146
cancel()
177147

178148
logger.Infof("Close grpc client connection")
@@ -198,10 +168,36 @@ func (p *Protocol) Close(ctx context.Context) error {
198168
return nil
199169
}
200170

201-
func (p *Protocol) startHeartbeatWatcher(
202-
ctx context.Context,
203-
heartbeatCh <-chan *pbv1.CloudEvent,
204-
) {
171+
func (p *Protocol) startEventsReceiver(ctx context.Context,
172+
subClient pbv1.CloudEventService_SubscribeClient, heartbeatCh chan *pbv1.CloudEvent) {
173+
for {
174+
evt, err := subClient.Recv()
175+
if err != nil {
176+
select {
177+
case p.reconnectErrorChan <- fmt.Errorf("subscribe stream failed: %w", err):
178+
default:
179+
}
180+
return
181+
}
182+
183+
if evt.Type == types.HeartbeatCloudEventsType {
184+
select {
185+
case heartbeatCh <- evt:
186+
case <-ctx.Done():
187+
return
188+
}
189+
continue
190+
}
191+
192+
select {
193+
case p.incoming <- evt:
194+
case <-ctx.Done():
195+
return
196+
}
197+
}
198+
}
199+
200+
func (p *Protocol) startHeartbeatWatcher(ctx context.Context, heartbeatCh <-chan *pbv1.CloudEvent) {
205201
logger := cecontext.LoggerFrom(ctx)
206202
// if serverHealthinessTimeout is disable, ignore the server heartbeat timeout
207203
if p.serverHealthinessTimeout == 0 {
@@ -218,18 +214,15 @@ func (p *Protocol) startHeartbeatWatcher(
218214
}
219215
}
220216

221-
// if no heartbeat was received duration the serverHealthinessTimeout,
222-
// send the timeout error to reconnectErrorChan
217+
// if no heartbeat was received duration the serverHealthinessTimeout, send the
218+
// timeout error to reconnectErrorChan
223219
timeout := p.serverHealthinessTimeout
224220
timer := time.NewTimer(timeout)
225221
defer timer.Stop()
226222

227223
for {
228224
select {
229-
case msg, ok := <-heartbeatCh:
230-
if !ok {
231-
return
232-
}
225+
case msg := <-heartbeatCh:
233226
logger.Debugf("heartbeat received %v", msg)
234227

235228
// reset timer safely
@@ -240,15 +233,12 @@ func (p *Protocol) startHeartbeatWatcher(
240233
}
241234
}
242235
timer.Reset(timeout)
243-
244236
case <-timer.C:
245-
// timeout reached: no heartbeat received
246237
select {
247-
case p.reconnectErrorChan <- fmt.Errorf("stream timeout: no message received for %v", timeout):
238+
case p.reconnectErrorChan <- fmt.Errorf("stream timeout: no heartbeat received for %v", timeout):
248239
default:
249240
}
250241
return
251-
252242
case <-ctx.Done():
253243
return
254244
}

0 commit comments

Comments
 (0)