Skip to content

Commit

Permalink
Add DSCP value to outgoing HTTP requests
Browse files Browse the repository at this point in the history
Replace syscall with golang.org/x/net calls

Signed-off-by: Gary Lee <gary.lee.ext@ericsson.com>
  • Loading branch information
garyclee committed Mar 28, 2023
1 parent 77dedcb commit 831916b
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import (
"path/filepath"
"strings"
"sync"
"syscall"
"time"

"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 Down Expand Up @@ -1169,16 +1170,18 @@ type DSCPDialer struct {
}

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
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
}

0 comments on commit 831916b

Please sign in to comment.