Skip to content

Commit

Permalink
config: thread Kit through config builders
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Mar 27, 2017
1 parent 6b323e5 commit 3f1893c
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 111 deletions.
14 changes: 7 additions & 7 deletions transport/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type TransportConfig struct {
KeepAlive time.Duration `config:"keepAlive"`
}

func (ts *transportSpec) buildTransport(tc *TransportConfig) (transport.Transport, error) {
func (ts *transportSpec) buildTransport(tc *TransportConfig, k *config.Kit) (transport.Transport, error) {
opts := ts.TransportOptions
if tc.KeepAlive > 0 {
opts = append(opts, KeepAlive(tc.KeepAlive))
Expand All @@ -109,7 +109,7 @@ type InboundConfig struct {
Address string `config:"address"`
}

func (ts *transportSpec) buildInbound(ic *InboundConfig, t transport.Transport) (transport.Inbound, error) {
func (ts *transportSpec) buildInbound(ic *InboundConfig, t transport.Transport, k *config.Kit) (transport.Inbound, error) {
if ic.Address == "" {
return nil, fmt.Errorf("inbound address is required")
}
Expand Down Expand Up @@ -138,14 +138,14 @@ type OutboundConfig struct {
URL string `config:"url"`
}

func (ts *transportSpec) buildOutbound(oc *OutboundConfig, t transport.Transport) (*Outbound, error) {
func (ts *transportSpec) buildOutbound(oc *OutboundConfig, t transport.Transport, k *config.Kit) (*Outbound, error) {
return t.(*Transport).NewSingleOutbound(oc.URL, ts.OutboundOptions...), nil
}

func (ts *transportSpec) buildUnaryOutbound(oc *OutboundConfig, t transport.Transport) (transport.UnaryOutbound, error) {
return ts.buildOutbound(oc, t)
func (ts *transportSpec) buildUnaryOutbound(oc *OutboundConfig, t transport.Transport, k *config.Kit) (transport.UnaryOutbound, error) {
return ts.buildOutbound(oc, t, k)
}

func (ts *transportSpec) buildOnewayOutbound(oc *OutboundConfig, t transport.Transport) (transport.OnewayOutbound, error) {
return ts.buildOutbound(oc, t)
func (ts *transportSpec) buildOnewayOutbound(oc *OutboundConfig, t transport.Transport, k *config.Kit) (transport.OnewayOutbound, error) {
return ts.buildOutbound(oc, t, k)
}
6 changes: 3 additions & 3 deletions transport/tchannel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TransportSpec() config.TransportSpec {
}
}

func buildTransport(tc *TransportConfig) (transport.Transport, error) {
func buildTransport(tc *TransportConfig, k *config.Kit) (transport.Transport, error) {
var opts []TransportOption
if tc.Address != "" {
opts = append(opts, ListenAddr(tc.Address))
Expand All @@ -63,11 +63,11 @@ func buildTransport(tc *TransportConfig) (transport.Transport, error) {
return NewTransport(opts...)
}

func buildInbound(_ *InboundConfig, t transport.Transport) (transport.Inbound, error) {
func buildInbound(_ *InboundConfig, t transport.Transport, k *config.Kit) (transport.Inbound, error) {
return t.(*Transport).NewInbound(), nil
}

func buildUnaryOutbound(oc *OutboundConfig, t transport.Transport) (transport.UnaryOutbound, error) {
func buildUnaryOutbound(oc *OutboundConfig, t transport.Transport, k *config.Kit) (transport.UnaryOutbound, error) {
return t.(*Transport).NewSingleOutbound(oc.Address), nil
}

Expand Down
6 changes: 3 additions & 3 deletions transport/x/redis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ func TransportSpec() config.TransportSpec {
}
}

func buildTransport(tc *TransportConfig) (transport.Transport, error) {
func buildTransport(tc *TransportConfig, k *config.Kit) (transport.Transport, error) {
if tc.Address == "" {
return nil, errors.New("address is required")
}
return NewRedis5Client(tc.Address), nil
}

func buildOnewayOutbound(oc *OutboundConfig, t transport.Transport) (transport.OnewayOutbound, error) {
func buildOnewayOutbound(oc *OutboundConfig, t transport.Transport, k *config.Kit) (transport.OnewayOutbound, error) {
if oc.QueueKey == "" {
return nil, errors.New("queue key is required")
}

return NewOnewayOutbound(t.(Client), oc.QueueKey), nil
}

func buildInbound(ic *InboundConfig, t transport.Transport) (transport.Inbound, error) {
func buildInbound(ic *InboundConfig, t transport.Transport, k *config.Kit) (transport.Inbound, error) {
if ic.QueueKey == "" {
return nil, errors.New("queue key is required")
}
Expand Down
28 changes: 15 additions & 13 deletions x/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type buildableOutbound struct {

type builder struct {
Name string
kit *Kit

// Transports that we actually need and their specs. We need a transport
// only if we have at least one inbound or outbound using it.
Expand All @@ -56,9 +57,10 @@ type builder struct {
clients map[string]*buildableOutbounds
}

func newBuilder(name string) *builder {
func newBuilder(name string, kit *Kit) *builder {
return &builder{
Name: name,
kit: kit,
needTransports: make(map[string]*compiledTransportSpec),
transports: make(map[string]*buildable),
clients: make(map[string]*buildableOutbounds),
Expand All @@ -84,14 +86,14 @@ func (b *builder) Build() (yarpc.Config, error) {
}
}

transports[name], err = buildTransport(cv)
transports[name], err = buildTransport(cv, b.kit)
if err != nil {
return yarpc.Config{}, err
}
}

for _, i := range b.inbounds {
ib, err := buildInbound(i.Value, transports[i.Transport])
ib, err := buildInbound(i.Value, transports[i.Transport], b.kit)
if err != nil {
errs = append(errs, err)
continue
Expand All @@ -109,14 +111,14 @@ func (b *builder) Build() (yarpc.Config, error) {
}

if o := c.Unary; o != nil {
ob.Unary, err = buildUnaryOutbound(o.Value, transports[o.Transport])
ob.Unary, err = buildUnaryOutbound(o.Value, transports[o.Transport], b.kit)
if err != nil {
errs = append(errs, err)
continue
}
}
if o := c.Oneway; o != nil {
ob.Oneway, err = buildOnewayOutbound(o.Value, transports[o.Transport])
ob.Oneway, err = buildOnewayOutbound(o.Value, transports[o.Transport], b.kit)
if err != nil {
errs = append(errs, err)
continue
Expand All @@ -134,8 +136,8 @@ func (b *builder) Build() (yarpc.Config, error) {

// buildTransport builds a Transport from the given value. This will panic if
// the output type is not a Transport.
func buildTransport(cv *buildable) (transport.Transport, error) {
result, err := cv.Build()
func buildTransport(cv *buildable, k *Kit) (transport.Transport, error) {
result, err := cv.Build(k)
if err != nil {
return nil, err
}
Expand All @@ -144,8 +146,8 @@ func buildTransport(cv *buildable) (transport.Transport, error) {

// buildInbound builds an Inbound from the given value. This will panic if the
// output type for this is not transport.Inbound.
func buildInbound(cv *buildable, t transport.Transport) (transport.Inbound, error) {
result, err := cv.Build(t)
func buildInbound(cv *buildable, t transport.Transport, k *Kit) (transport.Inbound, error) {
result, err := cv.Build(t, k)
if err != nil {
return nil, err
}
Expand All @@ -154,8 +156,8 @@ func buildInbound(cv *buildable, t transport.Transport) (transport.Inbound, erro

// buildUnaryOutbound builds an UnaryOutbound from the given value. This will panic
// if the output type for this is not transport.UnaryOutbound.
func buildUnaryOutbound(cv *buildable, t transport.Transport) (transport.UnaryOutbound, error) {
result, err := cv.Build(t)
func buildUnaryOutbound(cv *buildable, t transport.Transport, k *Kit) (transport.UnaryOutbound, error) {
result, err := cv.Build(t, k)
if err != nil {
return nil, err
}
Expand All @@ -164,8 +166,8 @@ func buildUnaryOutbound(cv *buildable, t transport.Transport) (transport.UnaryOu

// buildOnewayOutbound builds an OnewayOutbound from the given value. This will
// panic if the output type for this is not transport.OnewayOutbound.
func buildOnewayOutbound(cv *buildable, t transport.Transport) (transport.OnewayOutbound, error) {
result, err := cv.Build(t)
func buildOnewayOutbound(cv *buildable, t transport.Transport, k *Kit) (transport.OnewayOutbound, error) {
result, err := cv.Build(t, k)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/config/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *Configurator) NewDispatcher(data interface{}) (*yarpc.Dispatcher, error
func (c *Configurator) load(cfg *yarpcConfig) (yarpc.Config, error) {
var (
errors []error
b = newBuilder(cfg.Name)
b = newBuilder(cfg.Name, &Kit{c: c})
)

for _, inbound := range cfg.Inbounds {
Expand Down
48 changes: 24 additions & 24 deletions x/config/configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ func TestConfigurator(t *testing.T) {
inbound := transporttest.NewMockInbound(mockCtrl)

http.EXPECT().
BuildTransport(struct{}{}).
BuildTransport(struct{}{}, anyKit).
Return(transport, nil)
http.EXPECT().
BuildInbound(&inboundConfig{Address: ":80"}, transport).
BuildInbound(&inboundConfig{Address: ":80"}, transport, anyKit).
Return(inbound, nil)

tt.specs = []TransportSpec{http.Spec()}
Expand Down Expand Up @@ -261,16 +261,16 @@ func TestConfigurator(t *testing.T) {
InboundConfig: reflect.TypeOf(&inboundConfig{}),
}.Build(mockCtrl)
transport := transporttest.NewMockTransport(mockCtrl)
http.EXPECT().BuildTransport(struct{}{}).Return(transport, nil)
http.EXPECT().BuildTransport(struct{}{}, anyKit).Return(transport, nil)

inbound := transporttest.NewMockInbound(mockCtrl)
inbound2 := transporttest.NewMockInbound(mockCtrl)

http.EXPECT().
BuildInbound(&inboundConfig{Address: ":8080"}, transport).
BuildInbound(&inboundConfig{Address: ":8080"}, transport, anyKit).
Return(inbound, nil)
http.EXPECT().
BuildInbound(&inboundConfig{Address: ":8081"}, transport).
BuildInbound(&inboundConfig{Address: ":8081"}, transport, anyKit).
Return(inbound2, nil)

tt.specs = []TransportSpec{http.Spec()}
Expand Down Expand Up @@ -307,10 +307,10 @@ func TestConfigurator(t *testing.T) {
inbound := transporttest.NewMockInbound(mockCtrl)

http.EXPECT().
BuildTransport(struct{}{}).
BuildTransport(struct{}{}, anyKit).
Return(transport, nil)
http.EXPECT().
BuildInbound(&inboundConfig{Address: ":8081"}, transport).
BuildInbound(&inboundConfig{Address: ":8081"}, transport, anyKit).
Return(inbound, nil)

tt.specs = []TransportSpec{http.Spec()}
Expand Down Expand Up @@ -390,10 +390,10 @@ func TestConfigurator(t *testing.T) {
outbound := transporttest.NewMockUnaryOutbound(mockCtrl)

tchan.EXPECT().
BuildTransport(struct{}{}).
BuildTransport(struct{}{}, anyKit).
Return(transport, nil)
tchan.EXPECT().
BuildUnaryOutbound(&outboundConfig{Address: "localhost:4040"}, transport).
BuildUnaryOutbound(&outboundConfig{Address: "localhost:4040"}, transport, anyKit).
Return(outbound, nil)

tt.specs = []TransportSpec{tchan.Spec()}
Expand Down Expand Up @@ -435,10 +435,10 @@ func TestConfigurator(t *testing.T) {
outbound := transporttest.NewMockOnewayOutbound(mockCtrl)

redis.EXPECT().
BuildTransport(transportConfig{Address: "localhost:6379"}).
BuildTransport(transportConfig{Address: "localhost:6379"}, anyKit).
Return(transport, nil)
redis.EXPECT().
BuildOnewayOutbound(&outboundConfig{Queue: "requests"}, transport).
BuildOnewayOutbound(&outboundConfig{Queue: "requests"}, transport, anyKit).
Return(outbound, nil)

tt.specs = []TransportSpec{redis.Spec()}
Expand Down Expand Up @@ -482,15 +482,15 @@ func TestConfigurator(t *testing.T) {
oneway := transporttest.NewMockOnewayOutbound(mockCtrl)

http.EXPECT().
BuildTransport(&transportConfig{KeepAlive: time.Minute}).
BuildTransport(&transportConfig{KeepAlive: time.Minute}, anyKit).
Return(transport, nil)

outcfg := outboundConfig{URL: "http://localhost:8080/yarpc"}
http.EXPECT().
BuildUnaryOutbound(&outcfg, transport).
BuildUnaryOutbound(&outcfg, transport, anyKit).
Return(unary, nil)
http.EXPECT().
BuildOnewayOutbound(&outcfg, transport).
BuildOnewayOutbound(&outcfg, transport, anyKit).
Return(oneway, nil)

tt.specs = []TransportSpec{http.Spec()}
Expand Down Expand Up @@ -584,24 +584,24 @@ func TestConfigurator(t *testing.T) {
httpUnary := transporttest.NewMockUnaryOutbound(mockCtrl)
httpOneway := transporttest.NewMockOnewayOutbound(mockCtrl)
http.EXPECT().
BuildTransport(httpTransportConfig{KeepAlive: 5 * time.Minute}).
BuildTransport(httpTransportConfig{KeepAlive: 5 * time.Minute}, anyKit).
Return(httpTransport, nil)

redisTransport := transporttest.NewMockTransport(mockCtrl)
redisOneway := transporttest.NewMockOnewayOutbound(mockCtrl)
redis.EXPECT().
BuildTransport(redisTransportConfig{Address: "127.0.0.1:6379"}).
BuildTransport(redisTransportConfig{Address: "127.0.0.1:6379"}, anyKit).
Return(redisTransport, nil)

http.EXPECT().
BuildUnaryOutbound(httpOutboundConfig{URL: "http://localhost:8080/yarpc/v1"}, httpTransport).
BuildUnaryOutbound(httpOutboundConfig{URL: "http://localhost:8080/yarpc/v1"}, httpTransport, anyKit).
Return(httpUnary, nil)
http.EXPECT().
BuildOnewayOutbound(httpOutboundConfig{URL: "http://localhost:8081/yarpc/v2"}, httpTransport).
BuildOnewayOutbound(httpOutboundConfig{URL: "http://localhost:8081/yarpc/v2"}, httpTransport, anyKit).
Return(httpOneway, nil)

redis.EXPECT().
BuildOnewayOutbound(redisOutboundConfig{Queue: "requests"}, redisTransport).
BuildOnewayOutbound(redisOutboundConfig{Queue: "requests"}, redisTransport, anyKit).
Return(redisOneway, nil)

tt.specs = []TransportSpec{http.Spec(), redis.Spec()}
Expand Down Expand Up @@ -761,21 +761,21 @@ func TestConfigurator(t *testing.T) {
onewayStaging := transporttest.NewMockOnewayOutbound(mockCtrl)

http.EXPECT().
BuildTransport(struct{}{}).
BuildTransport(struct{}{}, anyKit).
Return(transport, nil)

http.EXPECT().
BuildUnaryOutbound(outboundConfig{URL: "http://localhost:8080/bar"}, transport).
BuildUnaryOutbound(outboundConfig{URL: "http://localhost:8080/bar"}, transport, anyKit).
Return(unary, nil)
http.EXPECT().
BuildOnewayOutbound(outboundConfig{URL: "http://localhost:8080/bar"}, transport).
BuildOnewayOutbound(outboundConfig{URL: "http://localhost:8080/bar"}, transport, anyKit).
Return(oneway, nil)

http.EXPECT().
BuildUnaryOutbound(outboundConfig{URL: "http://localhost:8081/bar"}, transport).
BuildUnaryOutbound(outboundConfig{URL: "http://localhost:8081/bar"}, transport, anyKit).
Return(unaryStaging, nil)
http.EXPECT().
BuildOnewayOutbound(outboundConfig{URL: "http://localhost:8081/bar"}, transport).
BuildOnewayOutbound(outboundConfig{URL: "http://localhost:8081/bar"}, transport, anyKit).
Return(onewayStaging, nil)

tt.specs = []TransportSpec{http.Spec()}
Expand Down
31 changes: 31 additions & 0 deletions x/config/kit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package config

import "reflect"

// Kit carries tools for building transports, inbounds, and outbounds using
// plugins.
type Kit struct {
c *Configurator
}

var _typeOfKit = reflect.TypeOf((*Kit)(nil))
Loading

0 comments on commit 3f1893c

Please sign in to comment.