Skip to content

Commit

Permalink
Set default user agent in proxy client
Browse files Browse the repository at this point in the history
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
  • Loading branch information
welteki committed Aug 6, 2024
1 parent 637b0b0 commit 9ba8323
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions gateway/types/proxy_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
package types

import (
"fmt"
"net"
"net/http"
"net/url"
"time"

"github.com/openfaas/faas/gateway/version"
)

// NewHTTPClientReverseProxy proxies to an upstream host through the use of a http.Client
Expand All @@ -31,18 +34,20 @@ func NewHTTPClientReverseProxy(baseURL *url.URL, timeout time.Duration, maxIdleC
// https://github.com/minio/minio/pull/5860

// Taken from http.DefaultTransport in Go 1.11
h.Client.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: timeout,
DualStack: true,
}).DialContext,
MaxIdleConns: maxIdleConns,
MaxIdleConnsPerHost: maxIdleConnsPerHost,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
h.Client.Transport = &ProxyTransport{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: timeout,
DualStack: true,
}).DialContext,
MaxIdleConns: maxIdleConns,
MaxIdleConnsPerHost: maxIdleConnsPerHost,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}

return &h
Expand All @@ -54,3 +59,13 @@ type HTTPClientReverseProxy struct {
Client *http.Client
Timeout time.Duration
}

type ProxyTransport struct {
Transport http.RoundTripper
}

func (pt *ProxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("User-Agent", fmt.Sprintf("openfaas-ce-gateway/%s", version.BuildVersion()))

return pt.Transport.RoundTrip(req)
}

0 comments on commit 9ba8323

Please sign in to comment.