Skip to content

Commit

Permalink
Add DSCP value to outgoing HTTP requests
Browse files Browse the repository at this point in the history
Add ability to specify a DSCP value in IPv4/IPv6 header of
outgoing HTTP requests

Signed-off-by: Gary Lee <gary.lee.ext@ericsson.com>
  • Loading branch information
garyclee committed Mar 23, 2023
1 parent 2f04d2e commit 77dedcb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"path/filepath"
"strings"
"sync"
"syscall"
"time"

"github.com/mwitkow/go-conntrack"
Expand All @@ -43,6 +44,7 @@ var (
DefaultHTTPClientConfig = HTTPClientConfig{
FollowRedirects: true,
EnableHTTP2: true,
DSCP: 0,
}

// defaultHTTPClientOptions holds the default HTTP client options.
Expand Down Expand Up @@ -309,6 +311,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 +1163,22 @@ 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{
Control: func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
if network == "tcp6" || network == "udp6" {
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_TCLASS, d.DSCP<<2)
} else {
_ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TOS, d.DSCP<<2)
}
})
}}).DialContext(ctx, network, addr)

return conn, err
}
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)
}
}

0 comments on commit 77dedcb

Please sign in to comment.