2016-08-19 15:03:53 +00:00
|
|
|
/*
|
|
|
|
*
|
2017-06-16 15:14:18 +00:00
|
|
|
* Copyright 2014 gRPC authors.
|
2016-08-19 15:03:53 +00:00
|
|
|
*
|
2017-06-16 15:14:18 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2016-08-19 15:03:53 +00:00
|
|
|
*
|
2017-06-16 15:14:18 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-19 15:03:53 +00:00
|
|
|
*
|
2017-06-16 15:14:18 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2016-08-19 15:03:53 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/net/trace"
|
2017-09-05 22:06:47 +00:00
|
|
|
"google.golang.org/grpc/balancer"
|
2016-08-19 15:03:53 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
2017-02-17 04:42:07 +00:00
|
|
|
"google.golang.org/grpc/peer"
|
2016-11-16 23:22:54 +00:00
|
|
|
"google.golang.org/grpc/stats"
|
2017-04-17 15:17:06 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2016-08-19 15:03:53 +00:00
|
|
|
"google.golang.org/grpc/transport"
|
|
|
|
)
|
|
|
|
|
|
|
|
// recvResponse receives and parses an RPC response.
|
|
|
|
// On error, it returns the error and indicates whether the call should be retried.
|
|
|
|
//
|
|
|
|
// TODO(zhaoq): Check whether the received message sequence is valid.
|
2016-11-16 23:22:54 +00:00
|
|
|
// TODO ctx is used for stats collection and processing. It is the context passed from the application.
|
|
|
|
func recvResponse(ctx context.Context, dopts dialOptions, t transport.ClientTransport, c *callInfo, stream *transport.Stream, reply interface{}) (err error) {
|
2016-08-19 15:03:53 +00:00
|
|
|
// Try to acquire header metadata from the server if there is any.
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(transport.ConnectionError); !ok {
|
|
|
|
t.CloseStream(stream, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
c.headerMD, err = stream.Header()
|
|
|
|
if err != nil {
|
2016-11-02 19:34:30 +00:00
|
|
|
return
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
|
|
|
p := &parser{r: stream}
|
2016-11-16 23:22:54 +00:00
|
|
|
var inPayload *stats.InPayload
|
2017-01-13 14:06:34 +00:00
|
|
|
if dopts.copts.StatsHandler != nil {
|
2016-11-16 23:22:54 +00:00
|
|
|
inPayload = &stats.InPayload{
|
|
|
|
Client: true,
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 15:03:53 +00:00
|
|
|
for {
|
2017-05-24 13:34:59 +00:00
|
|
|
if c.maxReceiveMessageSize == nil {
|
|
|
|
return Errorf(codes.Internal, "callInfo maxReceiveMessageSize field uninitialized(nil)")
|
|
|
|
}
|
|
|
|
if err = recv(p, dopts.codec, stream, dopts.dc, reply, *c.maxReceiveMessageSize, inPayload); err != nil {
|
2016-08-19 15:03:53 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2016-11-02 19:34:30 +00:00
|
|
|
return
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-17 15:17:06 +00:00
|
|
|
if inPayload != nil && err == io.EOF && stream.Status().Code() == codes.OK {
|
2016-11-16 23:22:54 +00:00
|
|
|
// TODO in the current implementation, inTrailer may be handled before inPayload in some cases.
|
|
|
|
// Fix the order if necessary.
|
2017-01-13 14:06:34 +00:00
|
|
|
dopts.copts.StatsHandler.HandleRPC(ctx, inPayload)
|
2016-11-16 23:22:54 +00:00
|
|
|
}
|
2016-08-19 15:03:53 +00:00
|
|
|
c.trailerMD = stream.Trailer()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendRequest writes out various information of an RPC such as Context and Message.
|
2017-05-24 13:34:59 +00:00
|
|
|
func sendRequest(ctx context.Context, dopts dialOptions, compressor Compressor, c *callInfo, callHdr *transport.CallHdr, stream *transport.Stream, t transport.ClientTransport, args interface{}, opts *transport.Options) (err error) {
|
2016-08-19 15:03:53 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// If err is connection error, t will be closed, no need to close stream here.
|
|
|
|
if _, ok := err.(transport.ConnectionError); !ok {
|
|
|
|
t.CloseStream(stream, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-11-16 23:22:54 +00:00
|
|
|
var (
|
|
|
|
cbuf *bytes.Buffer
|
|
|
|
outPayload *stats.OutPayload
|
|
|
|
)
|
2016-08-19 15:03:53 +00:00
|
|
|
if compressor != nil {
|
|
|
|
cbuf = new(bytes.Buffer)
|
|
|
|
}
|
2017-01-13 14:06:34 +00:00
|
|
|
if dopts.copts.StatsHandler != nil {
|
2016-11-16 23:22:54 +00:00
|
|
|
outPayload = &stats.OutPayload{
|
|
|
|
Client: true,
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 22:06:47 +00:00
|
|
|
hdr, data, err := encode(dopts.codec, args, compressor, cbuf, outPayload)
|
2016-08-19 15:03:53 +00:00
|
|
|
if err != nil {
|
2017-06-05 14:50:46 +00:00
|
|
|
return err
|
2017-05-24 13:34:59 +00:00
|
|
|
}
|
|
|
|
if c.maxSendMessageSize == nil {
|
|
|
|
return Errorf(codes.Internal, "callInfo maxSendMessageSize field uninitialized(nil)")
|
|
|
|
}
|
2017-09-05 22:06:47 +00:00
|
|
|
if len(data) > *c.maxSendMessageSize {
|
|
|
|
return Errorf(codes.ResourceExhausted, "grpc: trying to send message larger than max (%d vs. %d)", len(data), *c.maxSendMessageSize)
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
2017-09-05 22:06:47 +00:00
|
|
|
err = t.Write(stream, hdr, data, opts)
|
2016-11-16 23:22:54 +00:00
|
|
|
if err == nil && outPayload != nil {
|
|
|
|
outPayload.SentTime = time.Now()
|
2017-01-13 14:06:34 +00:00
|
|
|
dopts.copts.StatsHandler.HandleRPC(ctx, outPayload)
|
2016-11-16 23:22:54 +00:00
|
|
|
}
|
2016-08-19 15:03:53 +00:00
|
|
|
// t.NewStream(...) could lead to an early rejection of the RPC (e.g., the service/method
|
|
|
|
// does not exist.) so that t.Write could get io.EOF from wait(...). Leave the following
|
|
|
|
// recvResponse to get the final status.
|
|
|
|
if err != nil && err != io.EOF {
|
2017-05-24 13:34:59 +00:00
|
|
|
return err
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
|
|
|
// Sent successfully.
|
2017-05-24 13:34:59 +00:00
|
|
|
return nil
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke sends the RPC request on the wire and returns after response is received.
|
|
|
|
// Invoke is called by generated code. Also users can call Invoke directly when it
|
|
|
|
// is really needed in their use cases.
|
2016-09-12 19:16:55 +00:00
|
|
|
func Invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) error {
|
|
|
|
if cc.dopts.unaryInt != nil {
|
|
|
|
return cc.dopts.unaryInt(ctx, method, args, reply, cc, invoke, opts...)
|
|
|
|
}
|
|
|
|
return invoke(ctx, method, args, reply, cc, opts...)
|
|
|
|
}
|
|
|
|
|
2016-11-16 23:22:54 +00:00
|
|
|
func invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) (e error) {
|
2017-09-05 22:06:47 +00:00
|
|
|
c := defaultCallInfo()
|
2017-05-24 13:34:59 +00:00
|
|
|
mc := cc.GetMethodConfig(method)
|
|
|
|
if mc.WaitForReady != nil {
|
|
|
|
c.failFast = !*mc.WaitForReady
|
|
|
|
}
|
|
|
|
|
|
|
|
if mc.Timeout != nil && *mc.Timeout >= 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, *mc.Timeout)
|
|
|
|
defer cancel()
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
2017-05-24 13:34:59 +00:00
|
|
|
|
|
|
|
opts = append(cc.dopts.callOptions, opts...)
|
2016-08-19 15:03:53 +00:00
|
|
|
for _, o := range opts {
|
2017-09-05 22:06:47 +00:00
|
|
|
if err := o.before(c); err != nil {
|
2016-08-19 15:03:53 +00:00
|
|
|
return toRPCErr(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
for _, o := range opts {
|
2017-09-05 22:06:47 +00:00
|
|
|
o.after(c)
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
|
|
|
}()
|
2017-05-24 13:34:59 +00:00
|
|
|
|
|
|
|
c.maxSendMessageSize = getMaxSize(mc.MaxReqSize, c.maxSendMessageSize, defaultClientMaxSendMessageSize)
|
|
|
|
c.maxReceiveMessageSize = getMaxSize(mc.MaxRespSize, c.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
|
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
if EnableTracing {
|
|
|
|
c.traceInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
|
|
|
|
defer c.traceInfo.tr.Finish()
|
|
|
|
c.traceInfo.firstLine.client = true
|
|
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
|
|
c.traceInfo.firstLine.deadline = deadline.Sub(time.Now())
|
|
|
|
}
|
|
|
|
c.traceInfo.tr.LazyLog(&c.traceInfo.firstLine, false)
|
|
|
|
// TODO(dsymonds): Arrange for c.traceInfo.firstLine.remoteAddr to be set.
|
|
|
|
defer func() {
|
2016-11-16 23:22:54 +00:00
|
|
|
if e != nil {
|
|
|
|
c.traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{e}}, true)
|
2016-08-19 15:03:53 +00:00
|
|
|
c.traceInfo.tr.SetError()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-09-05 22:06:47 +00:00
|
|
|
ctx = newContextWithRPCInfo(ctx, c.failFast)
|
2017-01-13 14:06:34 +00:00
|
|
|
sh := cc.dopts.copts.StatsHandler
|
|
|
|
if sh != nil {
|
2017-05-24 13:34:59 +00:00
|
|
|
ctx = sh.TagRPC(ctx, &stats.RPCTagInfo{FullMethodName: method, FailFast: c.failFast})
|
2016-11-16 23:22:54 +00:00
|
|
|
begin := &stats.Begin{
|
|
|
|
Client: true,
|
|
|
|
BeginTime: time.Now(),
|
|
|
|
FailFast: c.failFast,
|
|
|
|
}
|
2017-01-13 14:06:34 +00:00
|
|
|
sh.HandleRPC(ctx, begin)
|
2017-05-24 13:34:59 +00:00
|
|
|
defer func() {
|
2016-11-16 23:22:54 +00:00
|
|
|
end := &stats.End{
|
|
|
|
Client: true,
|
|
|
|
EndTime: time.Now(),
|
|
|
|
Error: e,
|
|
|
|
}
|
2017-01-13 14:06:34 +00:00
|
|
|
sh.HandleRPC(ctx, end)
|
2017-05-24 13:34:59 +00:00
|
|
|
}()
|
|
|
|
}
|
2016-08-19 15:03:53 +00:00
|
|
|
topts := &transport.Options{
|
|
|
|
Last: true,
|
|
|
|
Delay: false,
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
t transport.ClientTransport
|
|
|
|
stream *transport.Stream
|
|
|
|
// Record the put handler from Balancer.Get(...). It is called once the
|
|
|
|
// RPC has completed or failed.
|
2017-09-05 22:06:47 +00:00
|
|
|
put func(balancer.DoneInfo)
|
2016-08-19 15:03:53 +00:00
|
|
|
)
|
|
|
|
// TODO(zhaoq): Need a formal spec of fail-fast.
|
|
|
|
callHdr := &transport.CallHdr{
|
|
|
|
Host: cc.authority,
|
|
|
|
Method: method,
|
|
|
|
}
|
|
|
|
if cc.dopts.cp != nil {
|
|
|
|
callHdr.SendCompress = cc.dopts.cp.Type()
|
|
|
|
}
|
2017-05-24 13:34:59 +00:00
|
|
|
if c.creds != nil {
|
|
|
|
callHdr.Creds = c.creds
|
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
|
2016-08-19 15:03:53 +00:00
|
|
|
gopts := BalancerGetOptions{
|
|
|
|
BlockingWait: !c.failFast,
|
|
|
|
}
|
|
|
|
t, put, err = cc.getTransport(ctx, gopts)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(zhaoq): Probably revisit the error handling.
|
2017-04-17 15:17:06 +00:00
|
|
|
if _, ok := status.FromError(err); ok {
|
2016-08-19 15:03:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err == errConnClosing || err == errConnUnavailable {
|
|
|
|
if c.failFast {
|
|
|
|
return Errorf(codes.Unavailable, "%v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// All the other errors are treated as Internal errors.
|
|
|
|
return Errorf(codes.Internal, "%v", err)
|
|
|
|
}
|
|
|
|
if c.traceInfo.tr != nil {
|
|
|
|
c.traceInfo.tr.LazyLog(&payload{sent: true, msg: args}, true)
|
|
|
|
}
|
2017-05-24 13:34:59 +00:00
|
|
|
stream, err = t.NewStream(ctx, callHdr)
|
|
|
|
if err != nil {
|
|
|
|
if put != nil {
|
|
|
|
if _, ok := err.(transport.ConnectionError); ok {
|
|
|
|
// If error is connection error, transport was sending data on wire,
|
|
|
|
// and we are not sure if anything has been sent on wire.
|
|
|
|
// If error is not connection error, we are sure nothing has been sent.
|
|
|
|
updateRPCInfoInContext(ctx, rpcInfo{bytesSent: true, bytesReceived: false})
|
|
|
|
}
|
2017-09-05 22:06:47 +00:00
|
|
|
put(balancer.DoneInfo{Err: err})
|
2017-05-24 13:34:59 +00:00
|
|
|
}
|
|
|
|
if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return toRPCErr(err)
|
|
|
|
}
|
2017-09-05 22:06:47 +00:00
|
|
|
if peer, ok := peer.FromContext(stream.Context()); ok {
|
|
|
|
c.peer = peer
|
|
|
|
}
|
|
|
|
err = sendRequest(ctx, cc.dopts, cc.dopts.cp, c, callHdr, stream, t, args, topts)
|
2016-08-19 15:03:53 +00:00
|
|
|
if err != nil {
|
|
|
|
if put != nil {
|
2017-05-24 13:34:59 +00:00
|
|
|
updateRPCInfoInContext(ctx, rpcInfo{
|
|
|
|
bytesSent: stream.BytesSent(),
|
|
|
|
bytesReceived: stream.BytesReceived(),
|
|
|
|
})
|
2017-09-05 22:06:47 +00:00
|
|
|
put(balancer.DoneInfo{Err: err})
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
|
|
|
// Retry a non-failfast RPC when
|
|
|
|
// i) there is a connection error; or
|
|
|
|
// ii) the server started to drain before this RPC was initiated.
|
2017-05-24 13:34:59 +00:00
|
|
|
if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
|
2016-08-19 15:03:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return toRPCErr(err)
|
|
|
|
}
|
2017-09-05 22:06:47 +00:00
|
|
|
err = recvResponse(ctx, cc.dopts, t, c, stream, reply)
|
2016-08-19 15:03:53 +00:00
|
|
|
if err != nil {
|
|
|
|
if put != nil {
|
2017-05-24 13:34:59 +00:00
|
|
|
updateRPCInfoInContext(ctx, rpcInfo{
|
|
|
|
bytesSent: stream.BytesSent(),
|
|
|
|
bytesReceived: stream.BytesReceived(),
|
|
|
|
})
|
2017-09-05 22:06:47 +00:00
|
|
|
put(balancer.DoneInfo{Err: err})
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
2017-05-24 13:34:59 +00:00
|
|
|
if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
|
2016-08-19 15:03:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return toRPCErr(err)
|
|
|
|
}
|
|
|
|
if c.traceInfo.tr != nil {
|
|
|
|
c.traceInfo.tr.LazyLog(&payload{sent: false, msg: reply}, true)
|
|
|
|
}
|
|
|
|
t.CloseStream(stream, nil)
|
|
|
|
if put != nil {
|
2017-05-24 13:34:59 +00:00
|
|
|
updateRPCInfoInContext(ctx, rpcInfo{
|
|
|
|
bytesSent: stream.BytesSent(),
|
|
|
|
bytesReceived: stream.BytesReceived(),
|
|
|
|
})
|
2017-09-05 22:06:47 +00:00
|
|
|
put(balancer.DoneInfo{Err: err})
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
2017-04-17 15:17:06 +00:00
|
|
|
return stream.Status().Err()
|
2016-08-19 15:03:53 +00:00
|
|
|
}
|
|
|
|
}
|