We have a function like below in our codebase:package tls
func NewTransport(config *tls.Config) http.RoundTripper {
// default values as of Go 1.9.2 + custom TLSClientConfig
// NOTE: don't try to simplify it like below:
// defaultCopy := *http.DefaultTransport.(*http.Transport)
// defaultCopy.TLSClientConfig = cConfig
// return &defaultCopy, nil
// as this would result in data races on private fields of http.Transport
// TODO: somehow make this code not need reviewing on each Go upgrade
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: config,
}
}
The problem here is, that we need to create new http.Clients with some custom TLS settings at various places. Unfortunately, it is not easy to reuse the default "sane" values from http.DefaultTransport, because:
Il giorno mercoledì 20 giugno 2018 17:29:57 UTC+2, Mateusz Czapliński ha scritto:
The problem here is, that we need to create new http.Clients with some custom TLS settings at various places. Unfortunately, it is not easy to reuse the default "sane" values from http.DefaultTransport, because:
If you want to generalize it, you can use reflect to copy all exported fields (again, not tested).Note that a custom transport does not support HTTP2 by default, and must be enabled explicitly.
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/JmpHoAd76aU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
So, thanks for first ideas! That said, I still see this more as unresolved, than resolved...As to the first idea, copying fields by hand does not solve the need for review during upgrade, as new fields may be added in new Go releases. So it does not help me, unfortunately.As to the second one, copying with reflect... eh; right, that's also something I pondered; what bothers me here, is first of all, that it's *suuuper* overcomplicated for the situation. And secondly, the DialContext field is somewhat risky in this situation. In this particular case, reviewing it seems to show there's no possibility for storing internal state; but it feels much too fragile, more like crossing fingers than solid & robust engineering... Again, in some next revision, something could be added which would invalidate this assumption, and introduce some subtle unexpected behavior.
I have tried this once in the past and believe that it worked.