Skip to content

Commit c66575c

Browse files
committed
Added http2 support to yab
Remove old comments Fix comment stating default http2 behavior in options Fixed test TimedOutUsingHTTP2Transport Fix comments Check if http2 flag is enabled when sending Thrift encoding
1 parent b3cacff commit c66575c

8 files changed

Lines changed: 245 additions & 26 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/yarpc/yab
22

3-
go 1.23
3+
go 1.23.0
44

55
toolchain go1.24.0
66

main.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func runWithOptions(opts Options, out output, logger *zap.Logger) {
323323
opts.TOpts.PeerList = ""
324324
opts.TOpts.Peers = peers
325325

326-
resolved := resolveProtocolEncoding(protocolScheme, opts.ROpts)
326+
resolved := resolveProtocolEncoding(protocolScheme, opts)
327327

328328
serializer, err := NewSerializer(opts, resolved)
329329
if err != nil {
@@ -406,8 +406,8 @@ type resolvedProtocolEncoding struct {
406406
enc encoding.Encoding
407407
}
408408

409-
func resolveProtocolEncoding(protocolScheme string, rOpts RequestOptions) resolvedProtocolEncoding {
410-
enc := rOpts.detectEncoding()
409+
func resolveProtocolEncoding(protocolScheme string, opts Options) resolvedProtocolEncoding {
410+
enc := opts.ROpts.detectEncoding()
411411

412412
switch protocolScheme {
413413
case "tchannel":
@@ -432,6 +432,9 @@ func resolveProtocolEncoding(protocolScheme string, rOpts RequestOptions) resolv
432432
// Try to determine a transport based on the guessed encoding.
433433
switch enc {
434434
case encoding.Thrift:
435+
if opts.TOpts.UseHTTP2 {
436+
return resolvedProtocolEncoding{transport.HTTP, encoding.Thrift}
437+
}
435438
return resolvedProtocolEncoding{transport.TChannel, encoding.Thrift}
436439
case encoding.Protobuf:
437440
return resolvedProtocolEncoding{transport.GRPC, encoding.Protobuf}
@@ -443,7 +446,7 @@ func resolveProtocolEncoding(protocolScheme string, rOpts RequestOptions) resolv
443446

444447
// Special case --health which is for TChannel + Thrift health calls.
445448
// This is for compatibility with tcurl.
446-
if rOpts.Health {
449+
if opts.ROpts.Health {
447450
return resolvedProtocolEncoding{transport.TChannel, encoding.Thrift}
448451
}
449452

main_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,9 +1032,11 @@ func TestResolveProtocolEncoding(t *testing.T) {
10321032
t.Run(tt.msg, func(t *testing.T) {
10331033
// Note: We test detectEncoding separately, so we just set the encoding which
10341034
// determines the result of detectEncoding.
1035-
got := resolveProtocolEncoding(tt.protocolScheme, RequestOptions{
1036-
Encoding: tt.encoding,
1037-
Health: tt.health,
1035+
got := resolveProtocolEncoding(tt.protocolScheme, Options{
1036+
ROpts: RequestOptions{
1037+
Encoding: tt.encoding,
1038+
Health: tt.health,
1039+
},
10381040
})
10391041
assert.Equal(t, tt.want, got)
10401042
})

options.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ type TransportOptions struct {
9595
HTTPMethod string `long:"http-method" description:"The HTTP method to use"`
9696
GRPCMaxResponseSize int `long:"grpc-max-response-size" description:"Maximum response size for gRPC requests. Default value is 4MB"`
9797
ForceJaegerSample bool `long:"force-jaeger-sample" description:"Force all requests to be sampled for Jaeger tracing (use with --jaeger)"`
98+
99+
// Enables HTTP2 transport
100+
UseHTTP2 bool `long:"http2" description:"Enable HTTP/2 for HTTP transport"`
101+
98102
// This is a hack to work around go-flags not allowing disabling flags:
99103
// https://github.com/jessevdk/go-flags/issues/191
100104
// Do not specify this value in a defaults.ini file as it is not possible

request_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,6 @@ func resolveOpts(t *testing.T, opts Options) (Options, resolvedProtocolEncoding)
556556

557557
opts.TOpts.Peers = peers
558558

559-
resolved := resolveProtocolEncoding(scheme, opts.ROpts)
559+
resolved := resolveProtocolEncoding(scheme, opts)
560560
return opts, resolved
561561
}

transport.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ func getTransport(opts TransportOptions, resolved resolvedProtocolEncoding, trac
185185
Encoding: resolved.enc.String(),
186186
URLs: opts.Peers,
187187
Tracer: tracer,
188+
UseHTTP2: opts.UseHTTP2,
188189
}
189190
return transport.NewHTTP(hopts)
190191
}

transport/http.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ package transport
2222

2323
import (
2424
"bytes"
25+
"crypto/tls"
2526
"errors"
2627
"fmt"
2728
"io/ioutil"
2829
"math/rand"
30+
"net"
2931
"net/http"
3032
"strconv"
3133
"time"
3234

35+
"golang.org/x/net/http2"
36+
3337
"github.com/opentracing/opentracing-go"
3438
"golang.org/x/net/context"
3539
)
@@ -51,6 +55,9 @@ type HTTPOptions struct {
5155
ShardKey string
5256
Encoding string
5357
Tracer opentracing.Tracer
58+
59+
// HTTP/2 specific options
60+
UseHTTP2 bool
5461
}
5562

5663
var (
@@ -70,11 +77,25 @@ func NewHTTP(opts HTTPOptions) (Transport, error) {
7077
opts.Method = "POST"
7178
}
7279

80+
var transport http.RoundTripper
81+
82+
if opts.UseHTTP2 {
83+
transport = &http2.Transport{
84+
AllowHTTP: true,
85+
DialTLSContext: func(ctx context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
86+
var d net.Dialer
87+
return d.DialContext(ctx, network, addr)
88+
},
89+
}
90+
} else {
91+
transport = &http.Transport{}
92+
}
93+
7394
return &httpTransport{
7495
opts: opts,
7596
// Use independent HTTP clients for each transport.
7697
client: &http.Client{
77-
Transport: &http.Transport{},
98+
Transport: transport,
7899
},
79100
tracer: opts.Tracer,
80101
}, nil

0 commit comments

Comments
 (0)