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

httpu: add context.Context and related interface #61

Merged
merged 1 commit into from
Aug 29, 2023

Conversation

andrew-d
Copy link
Contributor

This adds a new interface for httpu that supports a Context, and uses that context to set a deadline/timeout and also cancel the request if the context is canceled. Additionally, implement the Do method in terms of the DoWithContext method for the HTTPUClient implementation.

Updates #55

httpu/httpu.go Show resolved Hide resolved
Comment on lines 81 to 88
var ctxDelegates []ClientInterfaceCtx
for _, del := range mc.delegates {
if cd, ok := del.(ClientInterfaceCtx); ok {
ctxDelegates = append(ctxDelegates, cd)
} else {
return nil, fmt.Errorf("client does not implement ClientInterfaceCtx")
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is ugly because we have to check at runtime whether the mc.delegates members implement the ClientInterfaceCtx interface; if we put DoWithContext in the main ClientInterface interface, then we can avoid this.

Copy link
Owner

@huin huin Aug 22, 2023

Choose a reason for hiding this comment

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

Is there a compelling case to have ClientInterfaceCtx include ClientInterface? If not, then we can have two separate MultiClient implementations, one of which takes contexts.

Copy link
Owner

Choose a reason for hiding this comment

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

I think that MultiClient is only used (instantiated) by goupnp.httpClient(), which in turn is only used by goupnp.DiscoverDevicesCtx(), which ideally would pass its ctx into it.

So we could keep compatibility by having a context-respecting version of MultiClient, and also have DiscoverDevicesCtx use the context in one extra place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I started working on this, but DiscoverDevicesCtx calls ssdp.SSDPRawSearchCtx with the httpClient, so in order to get the Context behaviour, we'll either have to change the signature, or add another method that does the ~same thing but takes a ClientInterfaceCtx. Thoughts?

Copy link
Owner

Choose a reason for hiding this comment

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

Argh, that's a good point. Another function isn't too bad though, and could be named more simply RawSearch, which removes the ssdp.SSDPRawSearch stutter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, pushed an approach that splits up the interface, and adds the aforementioned function. What do you think?

(also, separately: I've got an example using NewWANIPConnection2ClientsCtx that I've been using for testing since my router doesn't support NewWANPPPConnection1Clients; any interest in me adding that example to this PR, or as a follow-up?)

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks, I'll try to take a look at this this evening (UK time).

As to your second question: that sounds like it should be a separate PR, but sounds good.

httpu/httpu.go Show resolved Hide resolved
httpu/httpu.go Show resolved Hide resolved
ssdp/ssdp.go Outdated
defer cancel()
}

// Check the timeout on the context (if any); if the context would time
Copy link
Owner

Choose a reason for hiding this comment

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

This line through and including the request creation (lines 111-130) look like they are common code that can be factored out (func prepareRequest(...) (*http.Request, error) ?).

Or could SSDPRawSearchCtx be implemented mostly by calling RawSearch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I factored them out into a shared function. I didn't want to change the behaviour of SSDPRawSearchCtx since users may have been expecting that the Do function would be called, and RawSearch calls DoWithContext instead (and requires a different interface type).

This adds a new interface for httpu that supports a Context, and uses
that context to set a deadline/timeout and also cancel the request if
the context is canceled. Additionally, add a new method to the SSDP
package that takes a ClientInterfaceCtx.

Updates huin#55

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Copy link
Owner

@huin huin left a comment

Choose a reason for hiding this comment

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

Thanks, looks good!

@huin huin merged commit 00783e7 into huin:main Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants