-
Notifications
You must be signed in to change notification settings - Fork 86
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
Conversation
httpu/multiclient.go
Outdated
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") | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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.
981f6b1
to
b0a0064
Compare
ssdp/ssdp.go
Outdated
defer cancel() | ||
} | ||
|
||
// Check the timeout on the context (if any); if the context would time |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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>
b0a0064
to
ab5fddf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good!
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