Skip to content
代码片段 群组 项目
提交 c2cf7b32 编辑于 作者: Grzegorz Bizon's avatar Grzegorz Bizon 提交者: Jacob Vosmaer
浏览文件

Add user-agent to all http requests initiated by Workhorse

上级 0c93b9e0
No related branches found
No related tags found
无相关合并请求
......@@ -19,7 +19,6 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/config"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/secret"
)
......
......@@ -9,12 +9,12 @@ import (
"gitlab.com/gitlab-org/labkit/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/httptransport"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/transport"
)
var httpClient = &http.Client{
Transport: httptransport.New(),
Transport: transport.NewRestrictedTransport(),
}
type Injector struct {
......
......@@ -21,9 +21,9 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/config"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/httptransport"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/transport"
)
type Resizer struct {
......@@ -69,7 +69,7 @@ const (
var envInjector = tracing.NewEnvInjector()
var httpClient = &http.Client{
Transport: httptransport.New(),
Transport: transport.NewRestrictedTransport(),
}
const (
......
......@@ -11,9 +11,9 @@ import (
"gitlab.com/gitlab-org/labkit/mask"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/httptransport"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/transport"
)
type entry struct{ senddata.Prefix }
......@@ -44,7 +44,7 @@ var preserveHeaderKeys = map[string]bool{
"Pragma": true, // Support for HTTP 1.0 proxies
}
var httpTransport = httptransport.New()
var httpTransport = transport.NewRestrictedTransport()
var httpClient = &http.Client{
Transport: httpTransport,
......
package httptransport
package transport
import (
"net/http"
......@@ -6,15 +6,42 @@ import (
"gitlab.com/gitlab-org/labkit/correlation"
"gitlab.com/gitlab-org/labkit/tracing"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/version"
)
// Creates a new default transport that has Workhorse's User-Agent header set.
func NewDefaultTransport() http.RoundTripper {
return &DefaultTransport{Next: http.DefaultTransport}
}
// Defines a http.Transport with values that are more restrictive than for
// http.DefaultTransport, they define shorter TLS Handshake, and more
// aggressive connection closing to prevent the connection hanging and reduce
// FD usage
func NewRestrictedTransport(options ...Option) http.RoundTripper {
return &DefaultTransport{Next: newRestrictedTransport(options...)}
}
type DefaultTransport struct {
Next http.RoundTripper
}
func (t DefaultTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", version.GetUserAgent())
return t.Next.RoundTrip(req)
}
type Option func(*http.Transport)
// Defines a http.Transport with values
// that are more restrictive than for http.DefaultTransport,
// they define shorter TLS Handshake, and more aggressive connection closing
// to prevent the connection hanging and reduce FD usage
func New(options ...Option) http.RoundTripper {
func WithDisabledCompression() Option {
return func(t *http.Transport) {
t.DisableCompression = true
}
}
func newRestrictedTransport(options ...Option) http.RoundTripper {
t := http.DefaultTransport.(*http.Transport).Clone()
// To avoid keep around TCP connections to http servers we're done with
......@@ -29,9 +56,3 @@ func New(options ...Option) http.RoundTripper {
return tracing.NewRoundTripper(correlation.NewInstrumentedRoundTripper(t))
}
func WithDisabledCompression() Option {
return func(t *http.Transport) {
t.DisableCompression = true
}
}
......@@ -8,11 +8,11 @@ import (
"gitlab.com/gitlab-org/labkit/mask"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/httptransport"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/transport"
)
var httpClient = &http.Client{
Transport: httptransport.New(),
Transport: transport.NewRestrictedTransport(),
}
// Object represents an object on a S3 compatible Object Store service.
......
package version
import "fmt"
var version = "unknown"
var build = "unknown"
var schema = "gitlab-workhorse (%s)-(%s)"
func SetVersion(v, b string) {
version = v
build = b
}
func GetUserAgent() string {
return GetApplicationVersion()
}
func GetApplicationVersion() string {
return fmt.Sprintf(schema, version, build)
}
package version
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestVersion(t *testing.T) {
require.Equal(t, GetApplicationVersion(), "gitlab-workhorse (unknown)-(unknown)")
SetVersion("15.3", "123.123")
require.Equal(t, GetApplicationVersion(), "gitlab-workhorse (15.3)-(123.123)")
SetVersion("", "123.123")
require.Equal(t, GetApplicationVersion(), "gitlab-workhorse ()-(123.123)")
}
......@@ -8,16 +8,16 @@ import (
"os"
"strings"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/httptransport"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/httprs"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/transport"
zip "gitlab.com/gitlab-org/golang-archive-zip"
"gitlab.com/gitlab-org/labkit/mask"
)
var httpClient = &http.Client{
Transport: httptransport.New(
httptransport.WithDisabledCompression(), // To avoid bugs when serving compressed files from object storage
Transport: transport.NewRestrictedTransport(
transport.WithDisabledCompression(), // To avoid bugs when serving compressed files from object storage
),
}
......
......@@ -23,6 +23,7 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/redis"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/secret"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/upstream"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/version"
)
// Version is the current version of GitLab Workhorse
......@@ -55,8 +56,10 @@ func main() {
os.Exit(2)
}
version.SetVersion(Version, BuildTime)
if boot.printVersion {
fmt.Printf("gitlab-workhorse %s-%s\n", Version, BuildTime)
fmt.Println(version.GetApplicationVersion())
os.Exit(0)
}
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册