summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/net/proxy/proxy.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/golang.org/x/net/proxy/proxy.go')
-rw-r--r--vendor/golang.org/x/net/proxy/proxy.go44
1 files changed, 42 insertions, 2 deletions
diff --git a/vendor/golang.org/x/net/proxy/proxy.go b/vendor/golang.org/x/net/proxy/proxy.go
index 78a8b7bee..553ead7cf 100644
--- a/vendor/golang.org/x/net/proxy/proxy.go
+++ b/vendor/golang.org/x/net/proxy/proxy.go
@@ -11,6 +11,7 @@ import (
"net"
"net/url"
"os"
+ "sync"
)
// A Dialer is a means to establish a connection.
@@ -27,7 +28,7 @@ type Auth struct {
// FromEnvironment returns the dialer specified by the proxy related variables in
// the environment.
func FromEnvironment() Dialer {
- allProxy := os.Getenv("all_proxy")
+ allProxy := allProxyEnv.Get()
if len(allProxy) == 0 {
return Direct
}
@@ -41,7 +42,7 @@ func FromEnvironment() Dialer {
return Direct
}
- noProxy := os.Getenv("no_proxy")
+ noProxy := noProxyEnv.Get()
if len(noProxy) == 0 {
return proxy
}
@@ -92,3 +93,42 @@ func FromURL(u *url.URL, forward Dialer) (Dialer, error) {
return nil, errors.New("proxy: unknown scheme: " + u.Scheme)
}
+
+var (
+ allProxyEnv = &envOnce{
+ names: []string{"ALL_PROXY", "all_proxy"},
+ }
+ noProxyEnv = &envOnce{
+ names: []string{"NO_PROXY", "no_proxy"},
+ }
+)
+
+// envOnce looks up an environment variable (optionally by multiple
+// names) once. It mitigates expensive lookups on some platforms
+// (e.g. Windows).
+// (Borrowed from net/http/transport.go)
+type envOnce struct {
+ names []string
+ once sync.Once
+ val string
+}
+
+func (e *envOnce) Get() string {
+ e.once.Do(e.init)
+ return e.val
+}
+
+func (e *envOnce) init() {
+ for _, n := range e.names {
+ e.val = os.Getenv(n)
+ if e.val != "" {
+ return
+ }
+ }
+}
+
+// reset is used by tests
+func (e *envOnce) reset() {
+ e.once = sync.Once{}
+ e.val = ""
+}