go.uber.org/yarpc@v1.72.1/internal/request/validator_outbound.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package request 22 23 import ( 24 "context" 25 26 "go.uber.org/yarpc/api/transport" 27 "go.uber.org/yarpc/api/x/introspection" 28 ) 29 30 // UnaryValidatorOutbound wraps an Outbound to validate all outgoing unary requests. 31 type UnaryValidatorOutbound struct { 32 transport.UnaryOutbound 33 transport.Namer 34 } 35 36 // Call performs the given request, failing early if the request is invalid. 37 func (o UnaryValidatorOutbound) Call(ctx context.Context, request *transport.Request) (*transport.Response, error) { 38 if err := transport.ValidateRequest(request); err != nil { 39 return nil, err 40 } 41 42 if err := transport.ValidateRequestContext(ctx); err != nil { 43 return nil, err 44 } 45 46 return o.UnaryOutbound.Call(ctx, request) 47 } 48 49 // Introspect returns the introspection status of the underlying outbound. 50 func (o UnaryValidatorOutbound) Introspect() introspection.OutboundStatus { 51 if o, ok := o.UnaryOutbound.(introspection.IntrospectableOutbound); ok { 52 return o.Introspect() 53 } 54 return introspection.OutboundStatusNotSupported 55 } 56 57 // OnewayValidatorOutbound wraps an Outbound to validate all outgoing oneway requests. 58 type OnewayValidatorOutbound struct { 59 transport.OnewayOutbound 60 transport.Namer 61 } 62 63 // CallOneway performs the given request, failing early if the request is invalid. 64 func (o OnewayValidatorOutbound) CallOneway(ctx context.Context, request *transport.Request) (transport.Ack, error) { 65 if err := transport.ValidateRequest(request); err != nil { 66 return nil, err 67 } 68 69 if err := transport.ValidateRequestContext(ctx); err != nil { 70 return nil, err 71 } 72 73 return o.OnewayOutbound.CallOneway(ctx, request) 74 } 75 76 // Introspect returns the introspection status of the underlying outbound. 77 func (o OnewayValidatorOutbound) Introspect() introspection.OutboundStatus { 78 if o, ok := o.OnewayOutbound.(introspection.IntrospectableOutbound); ok { 79 return o.Introspect() 80 } 81 return introspection.OutboundStatusNotSupported 82 } 83 84 // StreamValidatorOutbound wraps an Outbound to validate all outgoing oneway requests. 85 type StreamValidatorOutbound struct { 86 transport.Namer 87 transport.StreamOutbound 88 } 89 90 // CallStream performs the given request, failing early if the request is invalid. 91 func (o StreamValidatorOutbound) CallStream(ctx context.Context, request *transport.StreamRequest) (*transport.ClientStream, error) { 92 if err := transport.ValidateRequest(request.Meta.ToRequest()); err != nil { 93 return nil, err 94 } 95 96 return o.StreamOutbound.CallStream(ctx, request) 97 } 98 99 // Introspect returns the introspection status of the underlying outbound. 100 func (o StreamValidatorOutbound) Introspect() introspection.OutboundStatus { 101 if o, ok := o.StreamOutbound.(introspection.IntrospectableOutbound); ok { 102 return o.Introspect() 103 } 104 return introspection.OutboundStatusNotSupported 105 }