Skip to content

Commit

Permalink
check for aliveness without blocking on receive from stream
Browse files Browse the repository at this point in the history
  • Loading branch information
ValyaB committed Sep 16, 2024
1 parent c33cc70 commit b82229a
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions internal/proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,28 @@ func (c *Client) run(ctx context.Context, stream cloudproxyv1alpha.CloudProxyAPI
defer cancel()
go c.sendKeepAlive(ctxWithCancel, stream)

go func() {
for {
c.log.Debugf("Polling stream for messages")

in, err := stream.Recv()
if err != nil {
c.log.Errorf("stream.Recv: %v", err)
}

c.log.Debugf("Handling message from castai")
go c.handleMessage(in, stream)
}
}()

for {
if ctx.Err() != nil {
return ctx.Err()
}
if !c.isAlive() {
return fmt.Errorf("last seen too old, closing stream")
}
c.log.Info("Polling stream for messages")
in, err := stream.Recv()
if err != nil {
return fmt.Errorf("stream.Recv: %w", err)
}

c.log.Info("Handling message from castai")
go c.handleMessage(in, stream)
time.Sleep(time.Duration(c.keepAliveTimeout.Load()))
}
}

Expand Down

0 comments on commit b82229a

Please sign in to comment.