forked from yarpc/yarpc-go
/
dispatcher.go
114 lines (98 loc) · 4 KB
/
dispatcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2019 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 dispatcher
import (
"fmt"
"github.com/crossdock/crossdock-go"
"go.uber.org/yarpc"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/encoding/raw"
"go.uber.org/yarpc/internal/crossdock/client/params"
"go.uber.org/yarpc/internal/yarpctest"
"go.uber.org/yarpc/transport/grpc"
"go.uber.org/yarpc/transport/http"
"go.uber.org/yarpc/transport/tchannel"
)
// Create creates an RPC from the given parameters or fails the whole behavior.
func Create(t crossdock.T) *yarpc.Dispatcher {
return CreateDispatcherForTransport(t, "")
}
// CreateDispatcherForTransport creates an RPC from the given parameters or fails the whole behavior.
//
// If trans is non-empty, this will be used instead of the behavior transport.
func CreateDispatcherForTransport(t crossdock.T, trans string) *yarpc.Dispatcher {
fatals := crossdock.Fatals(t)
server := t.Param(params.Server)
fatals.NotEmpty(server, "server is required")
var unaryOutbound transport.UnaryOutbound
if trans == "" {
trans = t.Param(params.Transport)
}
switch trans {
case "http":
httpTransport := http.NewTransport()
unaryOutbound = httpTransport.NewSingleOutbound(fmt.Sprintf("http://%s:8081", server))
case "tchannel":
tchannelTransport, err := tchannel.NewChannelTransport(tchannel.ServiceName("client"))
fatals.NoError(err, "Failed to build ChannelTransport")
unaryOutbound = tchannelTransport.NewSingleOutbound(server + ":8082")
case "grpc":
unaryOutbound = grpc.NewTransport().NewSingleOutbound(server + ":8089")
default:
fatals.Fail("", "unknown transport %q", trans)
}
return yarpc.NewDispatcher(yarpc.Config{
Name: "client",
Outbounds: yarpc.Outbounds{
"yarpc-test": {
Unary: unaryOutbound,
},
},
})
}
// CreateOnewayDispatcher returns a started dispatcher and returns the address the
// server should call back to (ie this host)
func CreateOnewayDispatcher(t crossdock.T, handler raw.OnewayHandler) (*yarpc.Dispatcher, string) {
fatals := crossdock.Fatals(t)
server := t.Param("server_oneway")
fatals.NotEmpty(server, "oneway server is required")
httpTransport := http.NewTransport()
var outbound transport.OnewayOutbound
trans := t.Param("transport_oneway")
switch trans {
case "http":
outbound = httpTransport.NewSingleOutbound(fmt.Sprintf("http://%s:8084", server))
default:
fatals.Fail("", "unknown transport %q", trans)
}
client := t.Param("client_oneway")
callBackInbound := httpTransport.NewInbound(fmt.Sprintf("%s:0", client))
dispatcher := yarpc.NewDispatcher(yarpc.Config{
Name: "oneway-client",
Outbounds: yarpc.Outbounds{
"oneway-server": {Oneway: outbound},
},
Inbounds: yarpc.Inbounds{callBackInbound},
})
// register procedure for server to call us back on
dispatcher.Register(raw.OnewayProcedure("call-back", raw.OnewayHandler(handler)))
fatals.NoError(dispatcher.Start(), "could not start oneway Dispatcher")
return dispatcher, client + ":" + yarpctest.ZeroAddrToPort(callBackInbound.Addr())
}