Skip to content

Commit 8efaf82

Browse files
committed
supporting heartbeat in cloudevents subscribe stream
Signed-off-by: Wei Liu <liuweixa@redhat.com>
1 parent 9d57d8a commit 8efaf82

5 files changed

Lines changed: 17 additions & 14 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (o *grpcAgentOptions) Protocol(ctx context.Context, dataType types.CloudEve
4747
}
4848

4949
if o.ServerHealthinessTimeout != nil {
50-
opts = append(opts, protocol.WithServerHealthinessTimeout(*o.ServerHealthinessTimeout))
50+
opts = append(opts, protocol.WithServerHealthinessTimeout(o.ServerHealthinessTimeout))
5151
}
5252

5353
receiver, err := o.GetCloudEventsProtocol(

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,21 @@ func WithSubscribeOption(subscribeOpt *SubscribeOption) Option {
2929
func WithReconnectErrorChan(errorChan chan error) Option {
3030
return func(p *Protocol) error {
3131
if errorChan == nil {
32-
return fmt.Errorf("the errorChan must not be nil")
32+
return fmt.Errorf("the error channel must not be nil")
3333
}
3434
p.reconnectErrorChan = errorChan
3535
return nil
3636
}
3737
}
3838

39-
func WithServerHealthinessTimeout(timeout time.Duration) Option {
39+
func WithServerHealthinessTimeout(timeout *time.Duration) Option {
4040
return func(p *Protocol) error {
41-
if timeout <= 0 {
42-
p.serverHealthinessTimeout = 20 * time.Second // by default, using 20s
41+
if timeout != nil {
42+
if *timeout <= 0 {
43+
return fmt.Errorf("the server healthiness timeout must be greater than 0")
44+
}
45+
p.serverHealthinessTimeout = timeout
4346
}
44-
p.serverHealthinessTimeout = timeout
4547
return nil
4648
}
4749
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"google.golang.org/grpc/credentials/insecure"
10+
"k8s.io/utils/ptr"
1011

1112
"google.golang.org/grpc"
1213
"google.golang.org/grpc/test/bufconn"
@@ -86,7 +87,7 @@ func TestProtocol_Success(t *testing.T) {
8687
DataType: "io.open-cluster-management.test",
8788
}),
8889
WithReconnectErrorChan(reconnectErrorChan),
89-
WithServerHealthinessTimeout(5*time.Second),
90+
WithServerHealthinessTimeout(ptr.To(5*time.Second)),
9091
)
9192
if err != nil {
9293
t.Fatal(err)
@@ -123,7 +124,7 @@ func TestProtocol_Timeout(t *testing.T) {
123124
DataType: "io.open-cluster-management.test",
124125
}),
125126
WithReconnectErrorChan(reconnectErrorChan),
126-
WithServerHealthinessTimeout(2*time.Second),
127+
WithServerHealthinessTimeout(ptr.To(5*time.Second)),
127128
)
128129
if err != nil {
129130
t.Fatal(err)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ type Protocol struct {
3535
reconnectErrorChan chan error
3636

3737
// serverHealthinessTimeout is the max duration that client will reconnect if no server healthiness
38-
// status is received in this duration, if it is 0, this will be disabled.
39-
serverHealthinessTimeout time.Duration
38+
// status is received in this duration.
39+
serverHealthinessTimeout *time.Duration
4040
}
4141

4242
var (
@@ -199,8 +199,8 @@ func (p *Protocol) startEventsReceiver(ctx context.Context,
199199

200200
func (p *Protocol) startHeartbeatWatcher(ctx context.Context, heartbeatCh <-chan *pbv1.CloudEvent) {
201201
logger := cecontext.LoggerFrom(ctx)
202-
// if serverHealthinessTimeout is disable, ignore the server heartbeat timeout
203-
if p.serverHealthinessTimeout == 0 {
202+
// if serverHealthinessTimeout is not set, ignore the heartbeat timeout check
203+
if p.serverHealthinessTimeout == nil {
204204
for {
205205
select {
206206
case msg := <-heartbeatCh:
@@ -213,7 +213,7 @@ func (p *Protocol) startHeartbeatWatcher(ctx context.Context, heartbeatCh <-chan
213213

214214
// if no heartbeat was received duration the serverHealthinessTimeout, send the
215215
// timeout error to reconnectErrorChan
216-
timeout := p.serverHealthinessTimeout
216+
timeout := *p.serverHealthinessTimeout
217217
timer := time.NewTimer(timeout)
218218
defer timer.Stop()
219219

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (o *gRPCSourceOptions) Protocol(ctx context.Context, dataType types.CloudEv
4242
}
4343

4444
if o.ServerHealthinessTimeout != nil {
45-
opts = append(opts, protocol.WithServerHealthinessTimeout(*o.ServerHealthinessTimeout))
45+
opts = append(opts, protocol.WithServerHealthinessTimeout(o.ServerHealthinessTimeout))
4646
}
4747

4848
receiver, err := o.GetCloudEventsProtocol(

0 commit comments

Comments
 (0)