Skip to content

Commit

Permalink
Implement singleflight to ensure that only one proxyContext is in-fli…
Browse files Browse the repository at this point in the history
…ght at any given time for a host/port request
  • Loading branch information
bdwyertech committed Mar 11, 2021
1 parent 9238629 commit e61d46c
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ require (
github.com/mattn/go-colorable v0.1.8
github.com/mattn/go-isatty v0.0.12
github.com/sirupsen/logrus v1.8.1
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
45 changes: 27 additions & 18 deletions pkg/gontlm-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ReneKroon/ttlcache/v2"
"github.com/bdwyertech/proxyplease"
"github.com/elazarl/goproxy"
"golang.org/x/sync/singleflight"
// "github.com/bhendo/concord"
// "github.com/bhendo/concord/handshakers"
)
Expand Down Expand Up @@ -83,8 +84,9 @@ func Run() {
//
dialerCache := ttlcache.NewCache()
dialerCache.SetTTL(time.Duration(30 * time.Minute))
dialerCacheGroup := singleflight.Group{}

proxyDialer := func(scheme, addr string, pxyUrl *url.URL) (pxyCtx proxyplease.DialContext) {
proxyDialer := func(scheme, addr string, pxyUrl *url.URL) proxyplease.DialContext {
cacheKey := addr
if pxyUrl != nil && pxyUrl.Host != "" && ProxyOverrides == nil {
cacheKey = pxyUrl.Host
Expand All @@ -94,29 +96,36 @@ func Run() {
return dctx.(proxyplease.DialContext)
}

if ProxyOverrides != nil {
for _, host := range []string{addr, strings.Split(addr, ":")[0]} {
if pxy, ok := (*ProxyOverrides)[strings.ToLower(host)]; ok {
if pxy == nil {
d := net.Dialer{}
return d.DialContext
dctx, err, _ := dialerCacheGroup.Do(cacheKey, func() (pxyCtx interface{}, err error) {
if ProxyOverrides != nil {
for _, host := range []string{addr, strings.Split(addr, ":")[0]} {
if pxy, ok := (*ProxyOverrides)[strings.ToLower(host)]; ok {
if pxy == nil {
d := net.Dialer{}
return d.DialContext, nil
}
pxyUrl = pxy
}
pxyUrl = pxy
}
}
}

pxyCtx = proxyplease.NewDialContext(proxyplease.Proxy{
URL: pxyUrl,
Username: ProxyUser,
Password: ProxyPass,
Domain: ProxyDomain,
TargetURL: &url.URL{Host: addr, Scheme: scheme},
})
pxyCtx = proxyplease.NewDialContext(proxyplease.Proxy{
URL: pxyUrl,
Username: ProxyUser,
Password: ProxyPass,
Domain: ProxyDomain,
TargetURL: &url.URL{Host: addr, Scheme: scheme},
})

dialerCache.Set(cacheKey, pxyCtx)
err = dialerCache.Set(cacheKey, pxyCtx)

return
})
if err != nil {
log.Fatal(err)
}

return
return dctx.(proxyplease.DialContext)
}

//
Expand Down
3 changes: 3 additions & 0 deletions vendor/golang.org/x/sync/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/golang.org/x/sync/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/golang.org/x/sync/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/golang.org/x/sync/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

212 changes: 212 additions & 0 deletions vendor/golang.org/x/sync/singleflight/singleflight.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ golang.org/x/crypto/md4
# golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
golang.org/x/net/internal/socks
golang.org/x/net/proxy
# golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
## explicit
golang.org/x/sync/singleflight
# golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b
## explicit
golang.org/x/sys/internal/unsafeheader
Expand Down

0 comments on commit e61d46c

Please sign in to comment.