Skip to content

Commit

Permalink
Remove With prefix from transport options
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Dec 8, 2016
1 parent 0ac4b3f commit 8c09a9a
Show file tree
Hide file tree
Showing 19 changed files with 94 additions and 65 deletions.
8 changes: 4 additions & 4 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ func Benchmark_HTTP_NetHTTPToNetHTTP(b *testing.B) {

func Benchmark_TChannel_YARPCToYARPC(b *testing.B) {
serverTChannel := ytchannel.NewChannelTransport(
ytchannel.WithServiceName("server"),
ytchannel.ServiceName("server"),
)
serverCfg := yarpc.Config{
Name: "server",
Inbounds: yarpc.Inbounds{serverTChannel.NewInbound()},
}

clientTChannel := ytchannel.NewChannelTransport(
ytchannel.WithServiceName("client"),
ytchannel.ServiceName("client"),
)

// no defer close on channels because YARPC will take care of that
Expand Down Expand Up @@ -228,7 +228,7 @@ func Benchmark_TChannel_YARPCToTChannel(b *testing.B) {
serverCh.Register(traw.Wrap(tchannelEcho{t: b}), "echo")
require.NoError(b, serverCh.ListenAndServe(":0"), "failed to start up TChannel")

clientTChannel := ytchannel.NewChannelTransport(ytchannel.WithServiceName("client"))
clientTChannel := ytchannel.NewChannelTransport(ytchannel.ServiceName("client"))
clientCfg := yarpc.Config{
Name: "client",
Outbounds: yarpc.Outbounds{
Expand All @@ -246,7 +246,7 @@ func Benchmark_TChannel_YARPCToTChannel(b *testing.B) {

func Benchmark_TChannel_TChannelToYARPC(b *testing.B) {
tchannelTransport := ytchannel.NewChannelTransport(
ytchannel.WithServiceName("server"),
ytchannel.ServiceName("server"),
)

serverCfg := yarpc.Config{
Expand Down
2 changes: 1 addition & 1 deletion dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (

func basicDispatcher(t *testing.T) Dispatcher {
httpTransport := http.NewTransport()
tchannelTransport := tchannel.NewChannelTransport(tchannel.WithServiceName("test"))
tchannelTransport := tchannel.NewChannelTransport(tchannel.ServiceName("test"))
return NewDispatcher(Config{
Name: "test",
Inbounds: Inbounds{
Expand Down
18 changes: 12 additions & 6 deletions internal/crossdock/client/apachethrift/behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ func Run(t crossdock.T) {
httpTransport := http.NewTransport()
url := fmt.Sprintf("http://%v:%v/", server, serverPort)

thriftOutbound := httpTransport.NewSingleOutbound(url).
WithURLTemplate("http://host:port/thrift/ThriftTest")
secondOutbound := httpTransport.NewSingleOutbound(url).
WithURLTemplate("http://host:port/thrift/SecondService")
multiplexOutbound := httpTransport.NewSingleOutbound(url).
WithURLTemplate("http://host:port/thrift/multiplexed")
thriftOutbound := httpTransport.NewSingleOutbound(
url,
http.URLTemplate("http://host:port/thrift/ThriftTest"),
)
secondOutbound := httpTransport.NewSingleOutbound(
url,
http.URLTemplate("http://host:port/thrift/SecondService"),
)
multiplexOutbound := httpTransport.NewSingleOutbound(
url,
http.URLTemplate("http://host:port/thrift/multiplexed"),
)

dispatcher := yarpc.NewDispatcher(yarpc.Config{
Name: "apache-thrift-client",
Expand Down
2 changes: 1 addition & 1 deletion internal/crossdock/client/ctxpropagation/behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func buildDispatcher(t crossdock.T) (dispatcher yarpc.Dispatcher, tconfig server
fatals.NotEmpty(subject, "ctxserver is required")

httpTransport := http.NewTransport()
tchannelTransport := tch.NewChannelTransport(tch.WithListenAddr(":8087"), tch.WithServiceName("ctxclient"))
tchannelTransport := tch.NewChannelTransport(tch.ListenAddr(":8087"), tch.ServiceName("ctxclient"))

var outbound transport.UnaryOutbound
switch trans := t.Param(params.Transport); trans {
Expand Down
2 changes: 1 addition & 1 deletion internal/crossdock/client/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Create(t crossdock.T) yarpc.Dispatcher {
httpTransport := http.NewTransport()
unaryOutbound = httpTransport.NewSingleOutbound(fmt.Sprintf("http://%s:8081", server))
case "tchannel":
tchannelTransport := tchannel.NewChannelTransport(tchannel.WithServiceName("client"))
tchannelTransport := tchannel.NewChannelTransport(tchannel.ServiceName("client"))
unaryOutbound = tchannelTransport.NewSingleOutbound(server + ":8082")
default:
fatals.Fail("", "unknown transport %q", trans)
Expand Down
2 changes: 1 addition & 1 deletion internal/crossdock/client/tchserver/behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Run(t crossdock.T) {
server := t.Param(params.Server)
serverHostPort := fmt.Sprintf("%v:%v", server, serverPort)

tchannelTransport := tchannel.NewChannelTransport(tchannel.WithServiceName("yarpc-client"))
tchannelTransport := tchannel.NewChannelTransport(tchannel.ServiceName("yarpc-client"))
dispatcher := yarpc.NewDispatcher(yarpc.Config{
Name: "yarpc-client",
Outbounds: yarpc.Outbounds{
Expand Down
2 changes: 1 addition & 1 deletion internal/crossdock/server/yarpc/phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Phone(ctx context.Context, reqMeta yarpc.ReqMeta, body *PhoneRequest) (*Pho
var outbound transport.UnaryOutbound

httpTransport := http.NewTransport()
tchannelTransport := tchannel.NewChannelTransport(tchannel.WithServiceName("yarpc-test-client"))
tchannelTransport := tchannel.NewChannelTransport(tchannel.ServiceName("yarpc-test-client"))

switch {
case body.Transport.HTTP != nil:
Expand Down
4 changes: 2 additions & 2 deletions internal/crossdock/server/yarpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ var dispatcher yarpc.Dispatcher
// Start starts the test server that clients will make requests to
func Start() {
tchannelTransport := tchannel.NewChannelTransport(
tchannel.WithListenAddr(":8082"),
tchannel.WithServiceName("yarpc-test"),
tchannel.ListenAddr(":8082"),
tchannel.ServiceName("yarpc-test"),
)
httpTransport := http.NewTransport()
dispatcher = yarpc.NewDispatcher(yarpc.Config{
Expand Down
2 changes: 1 addition & 1 deletion internal/examples/json/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func main() {
flag.Parse()

httpTransport := http.NewTransport()
tchannelTransport := tchannel.NewChannelTransport(tchannel.WithServiceName("keyvalue-client"))
tchannelTransport := tchannel.NewChannelTransport(tchannel.ServiceName("keyvalue-client"))

var outbound transport.UnaryOutbound
switch strings.ToLower(outboundName) {
Expand Down
4 changes: 2 additions & 2 deletions internal/examples/json/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func (h *handler) Set(ctx context.Context, reqMeta yarpc.ReqMeta, body *setReque

func main() {
tchannelTransport := tchannel.NewChannelTransport(
tchannel.WithServiceName("keyvalue"),
tchannel.WithListenAddr(":28941"),
tchannel.ServiceName("keyvalue"),
tchannel.ListenAddr(":28941"),
)
httpTransport := http.NewTransport()
dispatcher := yarpc.NewDispatcher(yarpc.Config{
Expand Down
2 changes: 1 addition & 1 deletion internal/examples/thrift/keyvalue/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
flag.Parse()

httpTransport := http.NewTransport()
tchannelTransport := tchannel.NewChannelTransport(tchannel.WithServiceName("keyvalue-client"))
tchannelTransport := tchannel.NewChannelTransport(tchannel.ServiceName("keyvalue-client"))

var outbound transport.UnaryOutbound
switch strings.ToLower(outboundName) {
Expand Down
4 changes: 2 additions & 2 deletions internal/examples/thrift/keyvalue/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (h *handler) SetValue(ctx context.Context, reqMeta yarpc.ReqMeta, key *stri

func main() {
tchannelTransport := tchannel.NewChannelTransport(
tchannel.WithServiceName("keyvalue"),
tchannel.WithListenAddr(":28941"),
tchannel.ServiceName("keyvalue"),
tchannel.ListenAddr(":28941"),
)
httpTransport := http.NewTransport()
dispatcher := yarpc.NewDispatcher(yarpc.Config{
Expand Down
33 changes: 21 additions & 12 deletions transport/http/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,30 @@ import (
"github.com/opentracing/opentracing-go"
)

// InboundOption can be added as a variadic argument to the NewInbound
// constructor.
type InboundOption func(*Inbound)

// Mux specifies the ServeMux that the HTTP server should use and the pattern
// under which the YARPC endpoint should be registered.
func Mux(pattern string, mux *http.ServeMux) InboundOption {
return func(i *Inbound) {
i.mux = mux
i.muxPattern = pattern
}
}

// NewInbound builds a new HTTP inbound that listens on the given address and
// sharing this transport.
func (t *Transport) NewInbound(addr string) *Inbound {
return &Inbound{
func (t *Transport) NewInbound(addr string, opts ...InboundOption) *Inbound {
i := &Inbound{
addr: addr,
tracer: t.tracer,
}
for _, opt := range opts {
opt(i)
}
return i
}

// Inbound represents an HTTP Inbound. It is the same as the transport Inbound
Expand All @@ -52,16 +69,8 @@ type Inbound struct {
tracer opentracing.Tracer
}

// WithMux specifies the ServeMux that the HTTP server should use and the
// pattern under which the YARPC endpoint should be registered.
func (i *Inbound) WithMux(pattern string, mux *http.ServeMux) *Inbound {
i.mux = mux
i.muxPattern = pattern
return i
}

// WithTracer configures a tracer on this inbound.
func (i *Inbound) WithTracer(tracer opentracing.Tracer) *Inbound {
// Tracer configures a tracer on this inbound.
func (i *Inbound) Tracer(tracer opentracing.Tracer) *Inbound {
i.tracer = tracer
return i
}
Expand Down
4 changes: 2 additions & 2 deletions transport/http/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestInboundMux(t *testing.T) {
w.Write([]byte("healthy"))
})

i := httpTransport.NewInbound(":0").WithMux("/rpc/v1", mux)
i := httpTransport.NewInbound(":0", Mux("/rpc/v1", mux))
h := transporttest.NewMockUnaryHandler(mockCtrl)
reg := transporttest.NewMockRegistry(mockCtrl)
i.SetRegistry(reg)
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestInboundMux(t *testing.T) {
assert.Equal(t, err.Error(), "404 page not found")
}

o = o.WithURLTemplate("http://host:port/rpc/v1")
o.setURLTemplate("http://host:port/rpc/v1")
require.NoError(t, o.Start(), "failed to start outbound")
defer o.Stop()

Expand Down
44 changes: 29 additions & 15 deletions transport/http/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,43 +51,64 @@ var (

var defaultURLTemplate, _ = url.Parse("http://localhost")

// OutboundOption is suitable as an argument to NewOutbound
type OutboundOption func(*Outbound)

// URLTemplate specifies a template for URLs to this outbound.
// The peer (host:port) may vary from call to call.
// The URL template specifies the protocol and path.
func URLTemplate(template string) OutboundOption {
return func(o *Outbound) {
o.setURLTemplate(template)
}
}

// NewOutbound builds a new HTTP outbound built around a peer.Chooser
// for getting potential downstream hosts.
// Chooser.Choose MUST return *hostport.Peer objects.
// Chooser.Start MUST be called before Outbound.Start
func (t *Transport) NewOutbound(chooser peer.Chooser) *Outbound {
return &Outbound{
o := &Outbound{
started: atomic.NewBool(false),
chooser: chooser,
urlTemplate: defaultURLTemplate,
tracer: t.tracer,
}
return o
}

// NewOutbound builds a new HTTP outbound built around a peer.Chooser
// for getting potential downstream hosts.
// Chooser.Choose MUST return *hostport.Peer objects.
// Chooser.Start MUST be called before Outbound.Start
func NewOutbound(chooser peer.Chooser) *Outbound {
return &Outbound{
func NewOutbound(chooser peer.Chooser, opts ...OutboundOption) *Outbound {
o := &Outbound{
started: atomic.NewBool(false),
chooser: chooser,
urlTemplate: defaultURLTemplate,
tracer: opentracing.GlobalTracer(),
}
for _, opt := range opts {
opt(o)
}
return o
}

// NewSingleOutbound creates an outbound from a single URL (a bare host:port is
// not sufficient).
// This form defers to the underlying HTTP agent's peer selection and load
// balancing, using DNS.
func (t *Transport) NewSingleOutbound(URL string) *Outbound {
func (t *Transport) NewSingleOutbound(URL string, opts ...OutboundOption) *Outbound {
parsedURL, err := url.Parse(URL)
if err != nil {
panic(err.Error())
}
return t.NewOutbound(single.New(hostport.PeerIdentifier(parsedURL.Host), t)).
WithURLTemplate(URL)
o := t.NewOutbound(single.New(hostport.PeerIdentifier(parsedURL.Host), t))
o.setURLTemplate(URL)
for _, opt := range opts {
opt(o)
}
return o
}

// Outbound is an HTTP UnaryOutbound and OnewayOutbound
Expand All @@ -98,22 +119,15 @@ type Outbound struct {
tracer opentracing.Tracer
}

// WithURLTemplate configures an alternate URL template.
// setURLTemplate configures an alternate URL template.
// The host:port portion of the URL template gets replaced by the chosen peer's
// identifier for each outbound request.
func (o *Outbound) WithURLTemplate(URL string) *Outbound {
func (o *Outbound) setURLTemplate(URL string) {
parsedURL, err := url.Parse(URL)
if err != nil {
log.Fatalf("failed to configure HTTP outbound: invalid URL template %q: %s", URL, err)
}
o.urlTemplate = parsedURL
return o
}

// WithTracer configures a tracer for the outbound
func (o *Outbound) WithTracer(tracer opentracing.Tracer) *Outbound {
o.tracer = tracer
return o
}

// Transports returns the outbound's HTTP transport.
Expand Down
4 changes: 2 additions & 2 deletions transport/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func KeepAlive(t time.Duration) TransportOption {
}
}

// WithTracer configures a tracer for the transport and all its inbounds and
// Tracer configures a tracer for the transport and all its inbounds and
// outbounds.
func WithTracer(tracer opentracing.Tracer) TransportOption {
func Tracer(tracer opentracing.Tracer) TransportOption {
return func(c *transportConfig) {
c.tracer = tracer
}
Expand Down
2 changes: 1 addition & 1 deletion transport/tchannel/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestInboundStopWithoutStarting(t *testing.T) {
}

func TestInboundInvalidAddress(t *testing.T) {
x := NewChannelTransport(WithServiceName("foo"), WithListenAddr("not valid"))
x := NewChannelTransport(ServiceName("foo"), ListenAddr("not valid"))
i := x.NewInbound()
i.SetRegistry(new(transporttest.MockRegistry))
assert.Nil(t, i.Start())
Expand Down
12 changes: 6 additions & 6 deletions transport/tchannel/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type transportConfig struct {
// TransportOption will eventually also be suitable for passing to NewTransport.
type TransportOption func(*transportConfig)

// WithTracer is an option that configures the tracer for a TChannel transport.
func WithTracer(tracer opentracing.Tracer) TransportOption {
// Tracer is an option that configures the tracer for a TChannel transport.
func Tracer(tracer opentracing.Tracer) TransportOption {
return func(t *transportConfig) {
t.tracer = tracer
}
Expand All @@ -60,19 +60,19 @@ func WithChannel(ch Channel) TransportOption {
}
}

// WithListenAddr informs a transport constructor what address (in the form of
// ListenAddr informs a transport constructor what address (in the form of
// host:port) to listen on. This option does not apply to NewChannelTransport
// if it is called with WithChannel and a channel that is already listening.
func WithListenAddr(addr string) TransportOption {
func ListenAddr(addr string) TransportOption {
return func(t *transportConfig) {
t.addr = addr
}
}

// WithServiceName informs the NewChannelTransport constructor which service
// ServiceName informs the NewChannelTransport constructor which service
// name to use if it needs to construct a root Channel object, as when called
// without the WithChannel option.
func WithServiceName(name string) TransportOption {
func ServiceName(name string) TransportOption {
return func(t *transportConfig) {
t.name = name
}
Expand Down

0 comments on commit 8c09a9a

Please sign in to comment.