forked from yarpc/yarpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
194 lines (170 loc) · 6.88 KB
/
router.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 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 yarpc
import (
"context"
"fmt"
"sort"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal/humanize"
"go.uber.org/yarpc/yarpcerrors"
)
var (
_ transport.Router = (*MapRouter)(nil)
)
type serviceProcedure struct {
service string
procedure string
}
type serviceProcedureEncoding struct {
service string
procedure string
encoding transport.Encoding
}
// MapRouter is a Router that maintains a map of the registered
// procedures.
type MapRouter struct {
defaultService string
serviceProcedures map[serviceProcedure]transport.Procedure
serviceProcedureEncodings map[serviceProcedureEncoding]transport.Procedure
supportedEncodings map[serviceProcedure][]string
}
// NewMapRouter builds a new MapRouter that uses the given name as the
// default service name.
func NewMapRouter(defaultService string) MapRouter {
return MapRouter{
defaultService: defaultService,
serviceProcedures: make(map[serviceProcedure]transport.Procedure),
serviceProcedureEncodings: make(map[serviceProcedureEncoding]transport.Procedure),
supportedEncodings: make(map[serviceProcedure][]string),
}
}
// Register registers the procedure with the MapRouter.
// If the procedure does not specify its service name, the procedure will
// inherit the default service name of the router.
// Procedures should specify their encoding, and multiple procedures with the
// same name and service name can exist if they handle different encodings.
// If a procedure does not specify an encoding, it can only support one handler.
// The router will select that handler regardless of the encoding.
func (m MapRouter) Register(rs []transport.Procedure) {
for _, r := range rs {
if r.Service == "" {
r.Service = m.defaultService
}
if r.Name == "" {
panic("Expected procedure name not to be empty string in registration")
}
sp := serviceProcedure{
service: r.Service,
procedure: r.Name,
}
if r.Encoding == "" {
// Protect against masking encoding-specific routes.
if _, ok := m.serviceProcedures[sp]; ok {
panic(fmt.Sprintf("Cannot register multiple handlers for every encoding for service %q and procedure %q", sp.service, sp.procedure))
}
if se, ok := m.supportedEncodings[sp]; ok {
panic(fmt.Sprintf("Cannot register a handler for every encoding for service %q and procedure %q when there are already handlers for %s", sp.service, sp.procedure, humanize.QuotedJoin(se, "and", "no encodings")))
}
// This supports wild card encodings (for backward compatibility,
// since type models like Thrift were not previously required to
// specify the encoding of every procedure).
m.serviceProcedures[sp] = r
continue
}
spe := serviceProcedureEncoding{
service: r.Service,
procedure: r.Name,
encoding: r.Encoding,
}
// Protect against overriding wildcards
if _, ok := m.serviceProcedures[sp]; ok {
panic(fmt.Sprintf("Cannot register a handler for both (service, procedure) on any * encoding and (service, procedure, encoding), specifically (%q, %q, %q)", r.Service, r.Name, r.Encoding))
}
// Route to individual handlers for unique combinations of service,
// procedure, and encoding. This shall henceforth be the
// recommended way for models to register procedures.
m.serviceProcedureEncodings[spe] = r
// Record supported encodings.
m.supportedEncodings[sp] = append(m.supportedEncodings[sp], string(r.Encoding))
}
}
// Procedures returns a list procedures that
// have been registered so far.
func (m MapRouter) Procedures() []transport.Procedure {
procs := make([]transport.Procedure, 0, len(m.serviceProcedures)+len(m.serviceProcedureEncodings))
for _, v := range m.serviceProcedures {
procs = append(procs, v)
}
for _, v := range m.serviceProcedureEncodings {
procs = append(procs, v)
}
sort.Sort(sortableProcedures(procs))
return procs
}
type sortableProcedures []transport.Procedure
func (ps sortableProcedures) Len() int {
return len(ps)
}
func (ps sortableProcedures) Less(i int, j int) bool {
return ps[i].Less(ps[j])
}
func (ps sortableProcedures) Swap(i int, j int) {
ps[i], ps[j] = ps[j], ps[i]
}
// Choose retrives the HandlerSpec for the service, procedure, and encoding
// noted on the transport request, or returns an unrecognized procedure error
// (testable with transport.IsUnrecognizedProcedureError(err)).
func (m MapRouter) Choose(ctx context.Context, req *transport.Request) (transport.HandlerSpec, error) {
service, procedure, encoding := req.Service, req.Procedure, req.Encoding
if service == "" {
service = m.defaultService
}
// Fully specified combinations of service, procedure, and encoding.
spe := serviceProcedureEncoding{
service: service,
procedure: procedure,
encoding: encoding,
}
if procedure, ok := m.serviceProcedureEncodings[spe]; ok {
return procedure.HandlerSpec, nil
}
// Alternately use the original behavior: route all encodings to the same
// handler.
sp := serviceProcedure{
service: service,
procedure: procedure,
}
if procedure, ok := m.serviceProcedures[sp]; ok {
return procedure.HandlerSpec, nil
}
// Supported procedure, unrecognized encoding.
if wantEncodings := m.supportedEncodings[sp]; len(wantEncodings) == 1 {
// To maintain backward compatibility with the error messages provided
// on the wire (as verified by Crossdock across all language
// implementations), this routes an invalid encoding to the sole
// implementation of a procedure.
// The handler is then responsible for detecting the invalid encoding
// and providing an error including "failed to decode".
spe.encoding = transport.Encoding(wantEncodings[0])
return m.serviceProcedureEncodings[spe].HandlerSpec, nil
}
return transport.HandlerSpec{}, yarpcerrors.UnimplementedErrorf("unrecognized procedure %q for service %q", req.Procedure, req.Service)
}