Fix SetTimeout not changing the timeout in the underlying Dialer #45
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This branch solves the dialer timeout issue mentioned in #43 and #37.
It does so by creating a dummy struct
hasTimeout
which has theTimeout
field in its struct, and composes proxy.Dialer. This effectively creates a new struct with one property, Timeout, and one method, Dial.It then uses this dummy type to assert that the client.Dialer has the Timeout property. If it does, it is now able to set the Timeout property in the actual Dialer. This is all necessary because client uses
proxy.Dialer
which is a more general interface thannet.Dialer
and lacks the Timeout method, so Golang does not believe it has the property unless it is specifically asserted that it does.We could achieve this without creating a new type simply by asserting that client.Dialer is
net.Dialer
(i.e.if d, ok := c.dialer.(*net.Dialer)
, but then all Dialers would need to fully implement net.Dialer struct which might be incompatible with some uses of the application.Indeed, this implementation might not work already for any custom dialers that do not have the
Timeout
property (see Drawbacks).Drawbacks
This implementation has a couple of drawbacks. It should probably modify the SetDialer method to also return an error if client.Dialer does not have the Timeout, otherwise it will silently fail to change the timeout if someone is using a custom Dialer that does not implement a Timeout. However, I did not want to change the signature of a method as that is a breaking change, so I leave it to @likexian to determine if that is reasonable.
If this is desired, the implementation would look like
Generally, this is also just a bit of an awkward way to go about fixing this. There are probably cleaner ways to fix the problem (as mentioned in one of the Issues, a context could help), but this is certainly one of the fastest ways to resolve.
I welcome feedback on this change!