forked from yarpc/yarpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
118 lines (102 loc) · 4.04 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
// 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 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 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
// Request payload.
Body io.Reader
}
// 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("encoding", string(r.Encoding))
enc.AddString("procedure", r.Procedure)
enc.AddString("shardKey", r.ShardKey)
enc.AddString("routingKey", r.RoutingKey)
enc.AddString("routingDelegate", r.RoutingDelegate)
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.
func ValidateUnaryContext(ctx context.Context) error {
if _, hasDeadline := ctx.Deadline(); !hasDeadline {
return yarpcerrors.Newf(yarpcerrors.CodeInvalidArgument, "missing TTL")
}
return nil
}