Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/implement autopaho rpc #98

Merged
merged 13 commits into from
Aug 26, 2022
12 changes: 8 additions & 4 deletions autopaho/extensions/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (h *Handler) getCorrelIDChan(cID string) chan *paho.Publish {
return rChan
}

func (h *Handler) Request(ctx context.Context, pb *paho.Publish) (*paho.Publish, error) {
func (h *Handler) Request(ctx context.Context, pb *paho.Publish) (resp *paho.Publish, err error) {
cID := fmt.Sprintf("%d", time.Now().UnixNano())
rChan := make(chan *paho.Publish)

Expand All @@ -79,13 +79,17 @@ func (h *Handler) Request(ctx context.Context, pb *paho.Publish) (*paho.Publish,
pb.Properties.ResponseTopic = h.responseTopic
pb.Retain = false

_, err := h.cm.Publish(ctx, pb)
_, err = h.cm.Publish(ctx, pb)
if err != nil {
return nil, err
}

resp := <-rChan
return resp, nil
select {
case <-ctx.Done():
return nil, fmt.Errorf("context ended")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to return ctx.Error() here (its fairly common to check for context errors).

case resp = <-rChan:
return resp, nil
}
}

func (h *Handler) responseHandler(pb *paho.Publish) {
Expand Down