WithParams returns an initialized Proxy.
(p Params)
| 792 | |
| 793 | // WithParams returns an initialized Proxy. |
| 794 | func WithParams(p Params) *Proxy { |
| 795 | if p.IdleConnectionsPerHost <= 0 { |
| 796 | p.IdleConnectionsPerHost = DefaultIdleConnsPerHost |
| 797 | } |
| 798 | |
| 799 | if p.CloseIdleConnsPeriod == 0 { |
| 800 | p.CloseIdleConnsPeriod = DefaultCloseIdleConnsPeriod |
| 801 | } |
| 802 | |
| 803 | if p.ResponseHeaderTimeout == 0 { |
| 804 | p.ResponseHeaderTimeout = DefaultResponseHeaderTimeout |
| 805 | } |
| 806 | |
| 807 | if p.ExpectContinueTimeout == 0 { |
| 808 | p.ExpectContinueTimeout = DefaultExpectContinueTimeout |
| 809 | } |
| 810 | |
| 811 | if p.CustomHttpRoundTripperWrap == nil { |
| 812 | // default wrapper which does nothing |
| 813 | p.CustomHttpRoundTripperWrap = func(original http.RoundTripper) http.RoundTripper { |
| 814 | return original |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | tr := &http.Transport{ |
| 819 | DialContext: newSkipperDialer(net.Dialer{ |
| 820 | Timeout: p.Timeout, |
| 821 | KeepAlive: p.KeepAlive, |
| 822 | DualStack: p.DualStack, |
| 823 | }).DialContext, |
| 824 | TLSHandshakeTimeout: p.TLSHandshakeTimeout, |
| 825 | ResponseHeaderTimeout: p.ResponseHeaderTimeout, |
| 826 | ExpectContinueTimeout: p.ExpectContinueTimeout, |
| 827 | MaxIdleConns: p.MaxIdleConns, |
| 828 | MaxIdleConnsPerHost: p.IdleConnectionsPerHost, |
| 829 | IdleConnTimeout: p.CloseIdleConnsPeriod, |
| 830 | DisableKeepAlives: p.DisableHTTPKeepalives, |
| 831 | Proxy: proxyFromContext, |
| 832 | } |
| 833 | |
| 834 | quit := make(chan struct{}) |
| 835 | // We need this to reliably fade on DNS change, which is right |
| 836 | // now not fixed with IdleConnTimeout in the http.Transport. |
| 837 | // https://github.com/golang/go/issues/23427 |
| 838 | if p.CloseIdleConnsPeriod > 0 { |
| 839 | go func() { |
| 840 | ticker := time.NewTicker(p.CloseIdleConnsPeriod) |
| 841 | defer ticker.Stop() |
| 842 | for { |
| 843 | select { |
| 844 | case <-ticker.C: |
| 845 | tr.CloseIdleConnections() |
| 846 | case <-quit: |
| 847 | return |
| 848 | } |
| 849 | } |
| 850 | }() |
| 851 | } |
searching dependent graphs…