/
request.go
220 lines (188 loc) · 7.28 KB
/
request.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (c) 2022 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 transport
import (
"context"
"io"
"strings"
"go.uber.org/yarpc/yarpcerrors"
"go.uber.org/zap/zapcore"
)
// Request is the low level request representation.
type Request struct {
// Name of the service making the request.
Caller string
// Name of the service to which the request is being made.
// The service refers to the canonical traffic group for the service.
Service string
// Name of the transport used for the call.
Transport string
// Name of the encoding used for the request body.
Encoding Encoding
// Name of the procedure being called.
Procedure string
// Headers for the request.
Headers Headers
// ShardKey is an opaque string that is meaningful to the destined service
// for how to relay a request within a cluster to the shard that owns the
// key.
ShardKey string
// RoutingKey refers to a traffic group for the destined service, and when
// present may override the service name for purposes of routing.
RoutingKey string
// RoutingDelegate refers to the traffic group for a service that proxies
// for the destined service for routing purposes. The routing delegate may
// override the routing key and service.
RoutingDelegate string
// CallerProcedure refers to the name of the rpc procedure from the service making this request.
CallerProcedure string
// Request payload.
Body io.Reader
// Request payload size before any compression applied by the protocol
// When using the HTTP transport, this value is set from the HTTP header
// content-length. It should be noted that this value is set manually and
// will not be updated automatically if the body is being modified
BodySize int
}
// ToRequestMeta converts a Request into a RequestMeta.
func (r *Request) ToRequestMeta() *RequestMeta {
return &RequestMeta{
Caller: r.Caller,
Service: r.Service,
Transport: r.Transport,
Encoding: r.Encoding,
Procedure: r.Procedure,
Headers: r.Headers,
ShardKey: r.ShardKey,
RoutingKey: r.RoutingKey,
RoutingDelegate: r.RoutingDelegate,
CallerProcedure: r.CallerProcedure,
}
}
// MarshalLogObject implements zap.ObjectMarshaler.
func (r *Request) MarshalLogObject(enc zapcore.ObjectEncoder) error {
// TODO (#788): Include headers once we can omit PII.
enc.AddString("caller", r.Caller)
enc.AddString("service", r.Service)
enc.AddString("transport", r.Transport)
enc.AddString("encoding", string(r.Encoding))
enc.AddString("procedure", r.Procedure)
enc.AddString("shardKey", r.ShardKey)
enc.AddString("routingKey", r.RoutingKey)
enc.AddString("routingDelegate", r.RoutingDelegate)
enc.AddString("callerProcedure", r.CallerProcedure)
return nil
}
// Encoding represents an encoding format for requests.
type Encoding string
// ValidateRequest validates the given request. An error is returned if the
// request is invalid.
//
// Inbound transport implementations may use this to validate requests before
// handling them. Outbound implementations don't need to validate requests;
// they are always validated before the outbound is called.
func ValidateRequest(req *Request) error {
var missingParams []string
if req.Service == "" {
missingParams = append(missingParams, "service name")
}
if req.Procedure == "" {
missingParams = append(missingParams, "procedure")
}
if req.Caller == "" {
missingParams = append(missingParams, "caller name")
}
if req.Encoding == "" {
missingParams = append(missingParams, "encoding")
}
if len(missingParams) > 0 {
return yarpcerrors.Newf(yarpcerrors.CodeInvalidArgument, "missing %s", strings.Join(missingParams, ", "))
}
return nil
}
// ValidateUnaryContext validates that a context for a unary request is valid
// and contains all required information, and returns a YARPC error with code
// yarpcerrors.CodeInvalidArgument otherwise.
//
// Deprecated: Use ValidateRequestContext instead.
func ValidateUnaryContext(ctx context.Context) error {
if _, hasDeadline := ctx.Deadline(); !hasDeadline {
return yarpcerrors.Newf(yarpcerrors.CodeInvalidArgument, "missing TTL")
}
return nil
}
// ValidateRequestContext validates that a context for a request is valid
// and contains all required information, and returns a YARPC error with code
// yarpcerrors.CodeInvalidArgument otherwise.
func ValidateRequestContext(ctx context.Context) error {
if _, hasDeadline := ctx.Deadline(); !hasDeadline {
return yarpcerrors.Newf(yarpcerrors.CodeInvalidArgument, "missing TTL")
}
return nil
}
// RequestMeta is the low level request metadata representation. It does not
// include any "body" information, and should only be used for information about
// a connection's metadata.
type RequestMeta struct {
// Name of the service making the request.
Caller string
// Name of the service to which the request is being made.
// The service refers to the canonical traffic group for the service.
Service string
// Name of the transport used for the call.
Transport string
// Name of the encoding used for the request body.
Encoding Encoding
// Name of the procedure being called.
Procedure string
// Headers for the request.
Headers Headers
// ShardKey is an opaque string that is meaningful to the destined service
// for how to relay a request within a cluster to the shard that owns the
// key.
ShardKey string
// RoutingKey refers to a traffic group for the destined service, and when
// present may override the service name for purposes of routing.
RoutingKey string
// RoutingDelegate refers to the traffic group for a service that proxies
// for the destined service for routing purposes. The routing delegate may
// override the routing key and service.
RoutingDelegate string
// CallerProcedure refers to the name of the rpc procedure of the service making this request.
CallerProcedure string
}
// ToRequest converts a RequestMeta into a Request.
func (r *RequestMeta) ToRequest() *Request {
if r == nil {
return &Request{}
}
return &Request{
Caller: r.Caller,
Service: r.Service,
Transport: r.Transport,
Encoding: r.Encoding,
Procedure: r.Procedure,
Headers: r.Headers,
ShardKey: r.ShardKey,
RoutingKey: r.RoutingKey,
RoutingDelegate: r.RoutingDelegate,
CallerProcedure: r.CallerProcedure,
}
}