Skip to content

Commit

Permalink
🐛 http dialer add socket config; sockopt.mark use uint32 (#1264)
Browse files Browse the repository at this point in the history
(cherry picked from commit 4d155bc)
  • Loading branch information
Vigilans authored and xiaokangwang committed Sep 27, 2021
1 parent 38f10d0 commit 27614e5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion common/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type Content struct {
// Sockopt is the settings for socket connection.
type Sockopt struct {
// Mark of the socket connection.
Mark int32
Mark uint32
}

// SetAttribute attachs additional string attributes to content.
Expand Down
2 changes: 1 addition & 1 deletion transport/internet/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ message ProxyConfig {
// SocketConfig is options to be applied on network sockets.
message SocketConfig {
// Mark of the connection. If non-zero, the value will be set to SO_MARK.
int32 mark = 1;
uint32 mark = 1;

enum TCPFastOpenState {
// AsIs is to leave the current TFO state as is, unmodified.
Expand Down
19 changes: 14 additions & 5 deletions transport/internet/http/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,24 @@ var (
globalDialerAccess sync.Mutex
)

func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.Config) *http.Client {
type dialerCanceller func()

func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.Config, streamSettings *internet.MemoryStreamConfig) (*http.Client, dialerCanceller) {
globalDialerAccess.Lock()
defer globalDialerAccess.Unlock()

canceller := func() {
globalDialerAccess.Lock()
defer globalDialerAccess.Unlock()
delete(globalDialerMap, dest)
}

if globalDialerMap == nil {
globalDialerMap = make(map[net.Destination]*http.Client)
}

if client, found := globalDialerMap[dest]; found {
return client
return client, canceller
}

transport := &http2.Transport{
Expand All @@ -52,7 +60,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.C
address := net.ParseAddress(rawHost)

detachedContext := core.ToBackgroundDetachedContext(ctx)
pconn, err := internet.DialSystem(detachedContext, net.TCPDestination(address, port), nil)
pconn, err := internet.DialSystem(detachedContext, net.TCPDestination(address, port), streamSettings.SocketSettings)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -80,7 +88,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.C
}

globalDialerMap[dest] = client
return client
return client, canceller
}

// Dial dials a new TCP connection to the given destination.
Expand All @@ -90,7 +98,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
if tlsConfig == nil {
return nil, newError("TLS must be enabled for http transport.").AtWarning()
}
client := getHTTPClient(ctx, dest, tlsConfig)
client, canceller := getHTTPClient(ctx, dest, tlsConfig, streamSettings)

opts := pipe.OptionsFromContext(ctx)
preader, pwriter := pipe.New(opts...)
Expand Down Expand Up @@ -128,6 +136,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me

response, err := client.Do(request) // nolint: bodyclose
if err != nil {
canceller()
return nil, newError("failed to dial to ", dest).Base(err).AtWarning()
}
if response.StatusCode != 200 {
Expand Down

0 comments on commit 27614e5

Please sign in to comment.