Skip to content

Commit

Permalink
Fix exiting when context is cancelled
Browse files Browse the repository at this point in the history
Currently this just panics because it doesn't actually exit and tries
to send an event to the closed channel.

Note: I'm still unsure this actually closes the underlying connection,
but any dupe connection should close it anyway.
  • Loading branch information
Fizzadar committed Aug 9, 2024
1 parent f3a2df9 commit 0c664ad
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
"net/http"
"time"

pb "github.com/beeper/push-receiver/pb/mcs"
"github.com/pkg/errors"

pb "github.com/beeper/push-receiver/pb/mcs"
)

// httpClient defines the minimal interface needed for an http.Client to be implemented.
Expand Down Expand Up @@ -135,38 +136,25 @@ func (c *MCSClient) tryToConnect(ctx context.Context) error {
// start heartbeat
go c.heartbeat.start(ctx, mcs)

select {
case err := <-c.asyncPerformRead(mcs):
return err
case <-ctx.Done():
return ctx.Err()
}
}

func (c *MCSClient) asyncPerformRead(mcs *mcs) <-chan error {
ch := make(chan error)
go func() {
defer close(ch)
ch <- c.performRead(mcs)
}()
return ch
return c.readMessages(ctx, mcs)
}

func (c *MCSClient) performRead(mcs *mcs) error {
func (c *MCSClient) readMessages(ctx context.Context, mcs *mcs) error {
// receive version
err := mcs.ReceiveVersion()
if err != nil {
return errors.Wrap(err, "receive version failed")
}

for {
for ctx.Err() != nil {
// receive tag
data, err := mcs.PerformReadTag()
if err != nil {
return errors.Wrap(err, "receive tag failed")
}
if data == nil {
} else if data == nil {
return ErrFcmNotEnoughData
} else if ctx.Err() != nil {
break
}

err = c.onDataMessage(data)
Expand All @@ -181,6 +169,8 @@ func (c *MCSClient) performRead(mcs *mcs) error {
}
}
}

return nil
}

func (c *MCSClient) AckStreamPosition(mcs *mcs) error {
Expand Down

0 comments on commit 0c664ad

Please sign in to comment.