Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DSCP value to outgoing HTTP requests #464

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"github.com/mwitkow/go-conntrack"
"golang.org/x/net/http/httpproxy"
"golang.org/x/net/http2"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
"gopkg.in/yaml.v2"
Expand All @@ -43,6 +45,7 @@ var (
DefaultHTTPClientConfig = HTTPClientConfig{
FollowRedirects: true,
EnableHTTP2: true,
DSCP: 0,
}

// defaultHTTPClientOptions holds the default HTTP client options.
Expand Down Expand Up @@ -309,6 +312,8 @@ type HTTPClientConfig struct {
EnableHTTP2 bool `yaml:"enable_http2" json:"enable_http2"`
// Proxy configuration.
ProxyConfig `yaml:",inline"`
// DSCP configuration
DSCP int `yaml:"dscp" json:"dscp"`
}

// SetDirectory joins any relative file paths with dir.
Expand Down Expand Up @@ -1159,3 +1164,23 @@ func (c *ProxyConfig) Proxy() (fn func(*http.Request) (*url.URL, error)) {
func (c *ProxyConfig) GetProxyConnectHeader() http.Header {
return c.ProxyConnectHeader.HTTPHeader()
}

type DSCPDialer struct {
DSCP int
}

func (d *DSCPDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := (&net.Dialer{}).DialContext(ctx, network, addr)
if err != nil {
return conn, err
}

err = ipv6.NewConn(conn).SetTrafficClass(d.DSCP << 2)
if err != nil {
// fallback to IPv4
// err is ignored. It's probably not implemented on this platform if it fails
_ = ipv4.NewConn(conn).SetTOS(d.DSCP << 2)
}

return conn, nil
}
11 changes: 11 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1838,3 +1838,14 @@ no_proxy: promcon.io,cncf.io`, proxyServer.URL),
})
}
}

func TestDSCPDialContext(t *testing.T) {
cfg := HTTPClientConfig{DSCP: 46}
dialer := &DSCPDialer{DSCP: cfg.DSCP}
httpClientOption := WithDialContextFunc(dialer.DialContext)

_, err := NewClientFromConfig(cfg, "test", httpClientOption)
if err != nil {
t.Fatalf("Can't create a client from this config: %+v", cfg)
}
}