2017-04-17 18:08:24 -04:00
|
|
|
/*
|
|
|
|
*
|
2018-06-08 05:26:10 -04:00
|
|
|
* Copyright 2014 gRPC authors.
|
2017-04-17 18:08:24 -04:00
|
|
|
*
|
2018-06-08 05:26:10 -04: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
|
2017-04-17 18:08:24 -04:00
|
|
|
*
|
2018-06-08 05:26:10 -04:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-04-17 18:08:24 -04:00
|
|
|
*
|
2018-06-08 05:26:10 -04: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.
|
2017-04-17 18:08:24 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2019-04-12 14:36:17 -04:00
|
|
|
"context"
|
2017-04-17 18:08:24 -04:00
|
|
|
"errors"
|
|
|
|
"io"
|
2019-04-12 14:36:17 -04:00
|
|
|
"math"
|
|
|
|
"strconv"
|
2017-04-17 18:08:24 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
"google.golang.org/grpc/balancer"
|
2017-04-17 18:08:24 -04:00
|
|
|
"google.golang.org/grpc/codes"
|
2018-06-08 05:26:10 -04:00
|
|
|
"google.golang.org/grpc/encoding"
|
2023-11-01 11:03:44 -04:00
|
|
|
"google.golang.org/grpc/internal"
|
2019-04-12 14:36:17 -04:00
|
|
|
"google.golang.org/grpc/internal/balancerload"
|
|
|
|
"google.golang.org/grpc/internal/binarylog"
|
|
|
|
"google.golang.org/grpc/internal/channelz"
|
|
|
|
"google.golang.org/grpc/internal/grpcrand"
|
2021-08-12 11:11:44 -04:00
|
|
|
"google.golang.org/grpc/internal/grpcutil"
|
2022-11-16 10:32:17 -05:00
|
|
|
imetadata "google.golang.org/grpc/internal/metadata"
|
2021-08-12 11:11:44 -04:00
|
|
|
iresolver "google.golang.org/grpc/internal/resolver"
|
|
|
|
"google.golang.org/grpc/internal/serviceconfig"
|
2023-07-19 18:26:26 -04:00
|
|
|
istatus "google.golang.org/grpc/internal/status"
|
2019-04-12 14:36:17 -04:00
|
|
|
"google.golang.org/grpc/internal/transport"
|
2017-04-17 18:08:24 -04:00
|
|
|
"google.golang.org/grpc/metadata"
|
2019-04-12 14:36:17 -04:00
|
|
|
"google.golang.org/grpc/peer"
|
2017-07-02 03:02:24 -04:00
|
|
|
"google.golang.org/grpc/stats"
|
|
|
|
"google.golang.org/grpc/status"
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
2024-09-12 12:38:40 -04:00
|
|
|
var metadataFromOutgoingContextRaw = internal.FromOutgoingContextRaw.(func(context.Context) (metadata.MD, [][]string, bool))
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
// StreamHandler defines the handler called by gRPC server to complete the
|
2022-04-29 14:48:09 -04:00
|
|
|
// execution of a streaming RPC.
|
|
|
|
//
|
|
|
|
// If a StreamHandler returns an error, it should either be produced by the
|
|
|
|
// status package, or be one of the context errors. Otherwise, gRPC will use
|
|
|
|
// codes.Unknown as the status code and err.Error() as the status message of the
|
|
|
|
// RPC.
|
2023-11-01 11:03:44 -04:00
|
|
|
type StreamHandler func(srv any, stream ServerStream) error
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2021-08-12 11:11:44 -04:00
|
|
|
// StreamDesc represents a streaming RPC service's method specification. Used
|
|
|
|
// on the server when registering services and on the client when initiating
|
|
|
|
// new streams.
|
2017-04-17 18:08:24 -04:00
|
|
|
type StreamDesc struct {
|
2021-08-12 11:11:44 -04:00
|
|
|
// StreamName and Handler are only used when registering handlers on a
|
|
|
|
// server.
|
|
|
|
StreamName string // the name of the method excluding the service
|
|
|
|
Handler StreamHandler // the handler called for the method
|
|
|
|
|
|
|
|
// ServerStreams and ClientStreams are used for registering handlers on a
|
|
|
|
// server as well as defining RPC behavior when passed to NewClientStream
|
|
|
|
// and ClientConn.NewStream. At least one must be true.
|
|
|
|
ServerStreams bool // indicates the server can perform streaming sends
|
|
|
|
ClientStreams bool // indicates the client can perform streaming sends
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stream defines the common interface a client or server stream has to satisfy.
|
2018-06-08 05:26:10 -04:00
|
|
|
//
|
2019-04-12 14:36:17 -04:00
|
|
|
// Deprecated: See ClientStream and ServerStream documentation instead.
|
2017-04-17 18:08:24 -04:00
|
|
|
type Stream interface {
|
2019-04-12 14:36:17 -04:00
|
|
|
// Deprecated: See ClientStream and ServerStream documentation instead.
|
2017-04-17 18:08:24 -04:00
|
|
|
Context() context.Context
|
2019-04-12 14:36:17 -04:00
|
|
|
// Deprecated: See ClientStream and ServerStream documentation instead.
|
2023-11-01 11:03:44 -04:00
|
|
|
SendMsg(m any) error
|
2019-04-12 14:36:17 -04:00
|
|
|
// Deprecated: See ClientStream and ServerStream documentation instead.
|
2023-11-01 11:03:44 -04:00
|
|
|
RecvMsg(m any) error
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
// ClientStream defines the client-side behavior of a streaming RPC.
|
|
|
|
//
|
|
|
|
// All errors returned from ClientStream methods are compatible with the
|
|
|
|
// status package.
|
2017-04-17 18:08:24 -04:00
|
|
|
type ClientStream interface {
|
|
|
|
// Header returns the header metadata received from the server if there
|
2023-11-01 11:03:44 -04:00
|
|
|
// is any. It blocks if the metadata is not ready to read. If the metadata
|
|
|
|
// is nil and the error is also nil, then the stream was terminated without
|
|
|
|
// headers, and the status can be discovered by calling RecvMsg.
|
2017-04-17 18:08:24 -04:00
|
|
|
Header() (metadata.MD, error)
|
|
|
|
// Trailer returns the trailer metadata from the server, if there is any.
|
|
|
|
// It must only be called after stream.CloseAndRecv has returned, or
|
|
|
|
// stream.Recv has returned a non-nil error (including io.EOF).
|
|
|
|
Trailer() metadata.MD
|
|
|
|
// CloseSend closes the send direction of the stream. It closes the stream
|
2019-04-12 14:36:17 -04:00
|
|
|
// when non-nil error is met. It is also not safe to call CloseSend
|
|
|
|
// concurrently with SendMsg.
|
2017-04-17 18:08:24 -04:00
|
|
|
CloseSend() error
|
2019-04-12 14:36:17 -04:00
|
|
|
// Context returns the context for this stream.
|
|
|
|
//
|
|
|
|
// It should not be called until after Header or RecvMsg has returned. Once
|
|
|
|
// called, subsequent client-side retries are disabled.
|
|
|
|
Context() context.Context
|
|
|
|
// SendMsg is generally called by generated code. On error, SendMsg aborts
|
|
|
|
// the stream. If the error was generated by the client, the status is
|
|
|
|
// returned directly; otherwise, io.EOF is returned and the status of
|
|
|
|
// the stream may be discovered using RecvMsg.
|
|
|
|
//
|
|
|
|
// SendMsg blocks until:
|
|
|
|
// - There is sufficient flow control to schedule m with the transport, or
|
|
|
|
// - The stream is done, or
|
|
|
|
// - The stream breaks.
|
2018-06-08 05:26:10 -04:00
|
|
|
//
|
2019-04-12 14:36:17 -04:00
|
|
|
// SendMsg does not wait until the message is received by the server. An
|
|
|
|
// untimely stream closure may result in lost messages. To ensure delivery,
|
|
|
|
// users should ensure the RPC completed successfully using RecvMsg.
|
|
|
|
//
|
|
|
|
// It is safe to have a goroutine calling SendMsg and another goroutine
|
|
|
|
// calling RecvMsg on the same stream at the same time, but it is not safe
|
|
|
|
// to call SendMsg on the same stream in different goroutines. It is also
|
|
|
|
// not safe to call CloseSend concurrently with SendMsg.
|
2023-09-19 10:37:37 -04:00
|
|
|
//
|
|
|
|
// It is not safe to modify the message after calling SendMsg. Tracing
|
|
|
|
// libraries and stats handlers may use the message lazily.
|
2023-11-01 11:03:44 -04:00
|
|
|
SendMsg(m any) error
|
2019-04-12 14:36:17 -04:00
|
|
|
// RecvMsg blocks until it receives a message into m or the stream is
|
|
|
|
// done. It returns io.EOF when the stream completes successfully. On
|
|
|
|
// any other error, the stream is aborted and the error contains the RPC
|
|
|
|
// status.
|
|
|
|
//
|
|
|
|
// It is safe to have a goroutine calling SendMsg and another goroutine
|
|
|
|
// calling RecvMsg on the same stream at the same time, but it is not
|
|
|
|
// safe to call RecvMsg on the same stream in different goroutines.
|
2023-11-01 11:03:44 -04:00
|
|
|
RecvMsg(m any) error
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
// NewStream creates a new Stream for the client side. This is typically
|
2019-04-12 14:36:17 -04:00
|
|
|
// called by generated code. ctx is used for the lifetime of the stream.
|
|
|
|
//
|
|
|
|
// To ensure resources are not leaked due to the stream returned, one of the following
|
|
|
|
// actions must be performed:
|
|
|
|
//
|
2023-03-14 20:24:52 -04:00
|
|
|
// 1. Call Close on the ClientConn.
|
|
|
|
// 2. Cancel the context provided.
|
|
|
|
// 3. Call RecvMsg until a non-nil error is returned. A protobuf-generated
|
|
|
|
// client-streaming RPC, for instance, might use the helper function
|
|
|
|
// CloseAndRecv (note that CloseSend does not Recv, therefore is not
|
|
|
|
// guaranteed to release all resources).
|
|
|
|
// 4. Receive a non-nil, non-io.EOF error from Header or SendMsg.
|
2019-04-12 14:36:17 -04:00
|
|
|
//
|
|
|
|
// If none of the above happen, a goroutine and a context will be leaked, and grpc
|
|
|
|
// will not call the optionally-configured stats handler with a stats.End message.
|
2018-06-08 05:26:10 -04:00
|
|
|
func (cc *ClientConn) NewStream(ctx context.Context, desc *StreamDesc, method string, opts ...CallOption) (ClientStream, error) {
|
|
|
|
// allow interceptor to see all applicable call options, which means those
|
|
|
|
// configured as defaults from dial option as well as per-call options
|
|
|
|
opts = combine(cc.dopts.callOptions, opts)
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
if cc.dopts.streamInt != nil {
|
|
|
|
return cc.dopts.streamInt(ctx, desc, cc, method, newClientStream, opts...)
|
|
|
|
}
|
|
|
|
return newClientStream(ctx, desc, cc, method, opts...)
|
|
|
|
}
|
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
// NewClientStream is a wrapper for ClientConn.NewStream.
|
2018-06-08 05:26:10 -04:00
|
|
|
func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error) {
|
|
|
|
return cc.NewStream(ctx, desc, method, opts...)
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (_ ClientStream, err error) {
|
2023-11-01 11:03:44 -04:00
|
|
|
// Start tracking the RPC for idleness purposes. This is where a stream is
|
|
|
|
// created for both streaming and unary RPCs, and hence is a good place to
|
|
|
|
// track active RPC count.
|
|
|
|
if err := cc.idlenessMgr.OnCallBegin(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Add a calloption, to decrement the active call count, that gets executed
|
|
|
|
// when the RPC completes.
|
|
|
|
opts = append([]CallOption{OnFinish(func(error) { cc.idlenessMgr.OnCallEnd() })}, opts...)
|
|
|
|
|
2024-09-12 12:38:40 -04:00
|
|
|
if md, added, ok := metadataFromOutgoingContextRaw(ctx); ok {
|
2023-09-19 10:37:37 -04:00
|
|
|
// validate md
|
2022-11-16 10:32:17 -05:00
|
|
|
if err := imetadata.Validate(md); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2023-09-19 10:37:37 -04:00
|
|
|
// validate added
|
|
|
|
for _, kvs := range added {
|
|
|
|
for i := 0; i < len(kvs); i += 2 {
|
|
|
|
if err := imetadata.ValidatePair(kvs[i], kvs[i+1]); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 10:32:17 -05:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
if channelz.IsOn() {
|
|
|
|
cc.incrCallsStarted()
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
cc.incrCallsFailed()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
// Provide an opportunity for the first RPC to see the first service config
|
|
|
|
// provided by the resolver.
|
|
|
|
if err := cc.waitForResolvedAddrs(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-12 11:11:44 -04:00
|
|
|
|
|
|
|
var mc serviceconfig.MethodConfig
|
|
|
|
var onCommit func()
|
|
|
|
var newStream = func(ctx context.Context, done func()) (iresolver.ClientStream, error) {
|
|
|
|
return newClientStreamWithParams(ctx, desc, cc, method, mc, onCommit, done, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcInfo := iresolver.RPCInfo{Context: ctx, Method: method}
|
|
|
|
rpcConfig, err := cc.safeConfigSelector.SelectConfig(rpcInfo)
|
|
|
|
if err != nil {
|
2023-07-19 18:26:26 -04:00
|
|
|
if st, ok := status.FromError(err); ok {
|
|
|
|
// Restrict the code to the list allowed by gRFC A54.
|
|
|
|
if istatus.IsRestrictedControlPlaneCode(st) {
|
|
|
|
err = status.Errorf(codes.Internal, "config selector returned illegal status: %v", err)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-12 11:11:44 -04:00
|
|
|
return nil, toRPCErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rpcConfig != nil {
|
|
|
|
if rpcConfig.Context != nil {
|
|
|
|
ctx = rpcConfig.Context
|
|
|
|
}
|
|
|
|
mc = rpcConfig.MethodConfig
|
|
|
|
onCommit = rpcConfig.OnCommitted
|
|
|
|
if rpcConfig.Interceptor != nil {
|
|
|
|
rpcInfo.Context = nil
|
|
|
|
ns := newStream
|
|
|
|
newStream = func(ctx context.Context, done func()) (iresolver.ClientStream, error) {
|
|
|
|
cs, err := rpcConfig.Interceptor.NewStream(ctx, rpcInfo, done, ns)
|
|
|
|
if err != nil {
|
|
|
|
return nil, toRPCErr(err)
|
|
|
|
}
|
|
|
|
return cs, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newStream(ctx, func() {})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClientStreamWithParams(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, mc serviceconfig.MethodConfig, onCommit, doneFunc func(), opts ...CallOption) (_ iresolver.ClientStream, err error) {
|
|
|
|
c := defaultCallInfo()
|
2018-06-08 05:26:10 -04:00
|
|
|
if mc.WaitForReady != nil {
|
|
|
|
c.failFast = !*mc.WaitForReady
|
|
|
|
}
|
|
|
|
|
|
|
|
// Possible context leak:
|
|
|
|
// The cancel function for the child context we create will only be called
|
|
|
|
// when RecvMsg returns a non-nil error, if the ClientConn is closed, or if
|
|
|
|
// an error is generated by SendMsg.
|
|
|
|
// https://github.com/grpc/grpc-go/issues/1818.
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
if mc.Timeout != nil && *mc.Timeout >= 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, *mc.Timeout)
|
|
|
|
} else {
|
|
|
|
ctx, cancel = context.WithCancel(ctx)
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
for _, o := range opts {
|
2018-06-08 05:26:10 -04:00
|
|
|
if err := o.before(c); err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, toRPCErr(err)
|
|
|
|
}
|
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
c.maxSendMessageSize = getMaxSize(mc.MaxReqSize, c.maxSendMessageSize, defaultClientMaxSendMessageSize)
|
|
|
|
c.maxReceiveMessageSize = getMaxSize(mc.MaxRespSize, c.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
|
|
|
|
if err := setCallInfoCodec(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
callHdr := &transport.CallHdr{
|
2019-04-12 14:36:17 -04:00
|
|
|
Host: cc.authority,
|
|
|
|
Method: method,
|
2018-06-08 05:26:10 -04:00
|
|
|
ContentSubtype: c.contentSubtype,
|
2021-08-12 11:11:44 -04:00
|
|
|
DoneFunc: doneFunc,
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set our outgoing compression according to the UseCompressor CallOption, if
|
|
|
|
// set. In that case, also find the compressor from the encoding package.
|
|
|
|
// Otherwise, use the compressor configured by the WithCompressor DialOption,
|
|
|
|
// if set.
|
|
|
|
var cp Compressor
|
|
|
|
var comp encoding.Compressor
|
|
|
|
if ct := c.compressorType; ct != "" {
|
|
|
|
callHdr.SendCompress = ct
|
|
|
|
if ct != encoding.Identity {
|
|
|
|
comp = encoding.GetCompressor(ct)
|
|
|
|
if comp == nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "grpc: Compressor is not installed for requested grpc-encoding %q", ct)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if cc.dopts.cp != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
callHdr.SendCompress = cc.dopts.cp.Type()
|
2018-06-08 05:26:10 -04:00
|
|
|
cp = cc.dopts.cp
|
|
|
|
}
|
|
|
|
if c.creds != nil {
|
|
|
|
callHdr.Creds = c.creds
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
cs := &clientStream{
|
|
|
|
callHdr: callHdr,
|
|
|
|
ctx: ctx,
|
|
|
|
methodConfig: &mc,
|
|
|
|
opts: opts,
|
|
|
|
callInfo: c,
|
|
|
|
cc: cc,
|
|
|
|
desc: desc,
|
|
|
|
codec: c.codec,
|
|
|
|
cp: cp,
|
|
|
|
comp: comp,
|
|
|
|
cancel: cancel,
|
|
|
|
firstAttempt: true,
|
2021-08-12 11:11:44 -04:00
|
|
|
onCommit: onCommit,
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
if !cc.dopts.disableRetry {
|
|
|
|
cs.retryThrottler = cc.retryThrottler.Load().(*retryThrottler)
|
|
|
|
}
|
2023-03-14 20:24:52 -04:00
|
|
|
if ml := binarylog.GetMethodLogger(method); ml != nil {
|
|
|
|
cs.binlogs = append(cs.binlogs, ml)
|
|
|
|
}
|
|
|
|
if cc.dopts.binaryLogger != nil {
|
|
|
|
if ml := cc.dopts.binaryLogger.GetMethodLogger(method); ml != nil {
|
|
|
|
cs.binlogs = append(cs.binlogs, ml)
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2022-11-16 10:32:17 -05:00
|
|
|
// Pick the transport to use and create a new stream on the transport.
|
|
|
|
// Assign cs.attempt upon success.
|
|
|
|
op := func(a *csAttempt) error {
|
|
|
|
if err := a.getTransport(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := a.newStream(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Because this operation is always called either here (while creating
|
|
|
|
// the clientStream) or by the retry code while locked when replaying
|
|
|
|
// the operation, it is safe to access cs.attempt directly.
|
|
|
|
cs.attempt = a
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
if err := cs.withRetry(op, func() { cs.bufferForRetryLocked(0, op) }); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(cs.binlogs) != 0 {
|
2019-04-12 14:36:17 -04:00
|
|
|
md, _ := metadata.FromOutgoingContext(ctx)
|
|
|
|
logEntry := &binarylog.ClientHeader{
|
|
|
|
OnClientSide: true,
|
|
|
|
Header: md,
|
|
|
|
MethodName: method,
|
|
|
|
Authority: cs.cc.authority,
|
|
|
|
}
|
|
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
|
|
logEntry.Timeout = time.Until(deadline)
|
|
|
|
if logEntry.Timeout < 0 {
|
|
|
|
logEntry.Timeout = 0
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
2023-03-14 20:24:52 -04:00
|
|
|
for _, binlog := range cs.binlogs {
|
2023-09-19 10:37:37 -04:00
|
|
|
binlog.Log(cs.ctx, logEntry)
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
|
|
|
|
if desc != unaryStreamDesc {
|
|
|
|
// Listen on cc and stream contexts to cleanup when the user closes the
|
|
|
|
// ClientConn or cancels the stream context. In all other cases, an error
|
|
|
|
// should already be injected into the recv buffer by the transport, which
|
|
|
|
// the client will eventually receive, and then we will cancel the stream's
|
|
|
|
// context in clientStream.finish.
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-cc.ctx.Done():
|
|
|
|
cs.finish(ErrClientConnClosing)
|
|
|
|
case <-ctx.Done():
|
|
|
|
cs.finish(toRPCErr(ctx.Err()))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
return cs, nil
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:32:17 -05:00
|
|
|
// newAttemptLocked creates a new csAttempt without a transport or stream.
|
|
|
|
func (cs *clientStream) newAttemptLocked(isTransparent bool) (*csAttempt, error) {
|
|
|
|
if err := cs.ctx.Err(); err != nil {
|
|
|
|
return nil, toRPCErr(err)
|
|
|
|
}
|
|
|
|
if err := cs.cc.ctx.Err(); err != nil {
|
|
|
|
return nil, ErrClientConnClosing
|
|
|
|
}
|
|
|
|
|
2022-03-24 08:21:41 -04:00
|
|
|
ctx := newContextWithRPCInfo(cs.ctx, cs.callInfo.failFast, cs.callInfo.codec, cs.cp, cs.comp)
|
|
|
|
method := cs.callHdr.Method
|
|
|
|
var beginTime time.Time
|
2022-11-21 10:03:02 -05:00
|
|
|
shs := cs.cc.dopts.copts.StatsHandlers
|
|
|
|
for _, sh := range shs {
|
2022-03-24 08:21:41 -04:00
|
|
|
ctx = sh.TagRPC(ctx, &stats.RPCTagInfo{FullMethodName: method, FailFast: cs.callInfo.failFast})
|
|
|
|
beginTime = time.Now()
|
|
|
|
begin := &stats.Begin{
|
|
|
|
Client: true,
|
|
|
|
BeginTime: beginTime,
|
|
|
|
FailFast: cs.callInfo.failFast,
|
|
|
|
IsClientStream: cs.desc.ClientStreams,
|
|
|
|
IsServerStream: cs.desc.ServerStreams,
|
|
|
|
IsTransparentRetryAttempt: isTransparent,
|
|
|
|
}
|
|
|
|
sh.HandleRPC(ctx, begin)
|
|
|
|
}
|
|
|
|
|
|
|
|
var trInfo *traceInfo
|
|
|
|
if EnableTracing {
|
|
|
|
trInfo = &traceInfo{
|
2024-09-12 12:38:40 -04:00
|
|
|
tr: newTrace("grpc.Sent."+methodFamily(method), method),
|
2022-03-24 08:21:41 -04:00
|
|
|
firstLine: firstLine{
|
|
|
|
client: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
|
|
trInfo.firstLine.deadline = time.Until(deadline)
|
|
|
|
}
|
|
|
|
trInfo.tr.LazyLog(&trInfo.firstLine, false)
|
2024-09-12 12:38:40 -04:00
|
|
|
ctx = newTraceContext(ctx, trInfo.tr)
|
2022-03-24 08:21:41 -04:00
|
|
|
}
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
if cs.cc.parsedTarget.URL.Scheme == internal.GRPCResolverSchemeExtraMetadata {
|
2022-11-16 10:32:17 -05:00
|
|
|
// Add extra metadata (metadata that will be added by transport) to context
|
|
|
|
// so the balancer can see them.
|
|
|
|
ctx = grpcutil.WithExtraMetadata(ctx, metadata.Pairs(
|
|
|
|
"content-type", grpcutil.ContentType(cs.callHdr.ContentSubtype),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &csAttempt{
|
2022-11-21 10:03:02 -05:00
|
|
|
ctx: ctx,
|
|
|
|
beginTime: beginTime,
|
|
|
|
cs: cs,
|
|
|
|
dc: cs.cc.dopts.dc,
|
|
|
|
statsHandlers: shs,
|
|
|
|
trInfo: trInfo,
|
2022-11-16 10:32:17 -05:00
|
|
|
}, nil
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
|
2022-11-16 10:32:17 -05:00
|
|
|
func (a *csAttempt) getTransport() error {
|
|
|
|
cs := a.cs
|
2021-08-12 11:11:44 -04:00
|
|
|
|
2022-11-16 10:32:17 -05:00
|
|
|
var err error
|
2023-07-19 18:26:26 -04:00
|
|
|
a.t, a.pickResult, err = cs.cc.getTransport(a.ctx, cs.callInfo.failFast, cs.callHdr.Method)
|
2019-04-12 14:36:17 -04:00
|
|
|
if err != nil {
|
2022-11-16 10:32:17 -05:00
|
|
|
if de, ok := err.(dropError); ok {
|
|
|
|
err = de.error
|
|
|
|
a.drop = true
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-11-16 10:32:17 -05:00
|
|
|
if a.trInfo != nil {
|
|
|
|
a.trInfo.firstLine.SetRemoteAddr(a.t.RemoteAddr())
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *csAttempt) newStream() error {
|
|
|
|
cs := a.cs
|
|
|
|
cs.callHdr.PreviousAttempts = cs.numRetries
|
2023-07-19 18:26:26 -04:00
|
|
|
|
|
|
|
// Merge metadata stored in PickResult, if any, with existing call metadata.
|
|
|
|
// It is safe to overwrite the csAttempt's context here, since all state
|
|
|
|
// maintained in it are local to the attempt. When the attempt has to be
|
|
|
|
// retried, a new instance of csAttempt will be created.
|
2023-09-19 10:37:37 -04:00
|
|
|
if a.pickResult.Metadata != nil {
|
2023-07-19 18:26:26 -04:00
|
|
|
// We currently do not have a function it the metadata package which
|
|
|
|
// merges given metadata with existing metadata in a context. Existing
|
|
|
|
// function `AppendToOutgoingContext()` takes a variadic argument of key
|
|
|
|
// value pairs.
|
|
|
|
//
|
|
|
|
// TODO: Make it possible to retrieve key value pairs from metadata.MD
|
|
|
|
// in a form passable to AppendToOutgoingContext(), or create a version
|
|
|
|
// of AppendToOutgoingContext() that accepts a metadata.MD.
|
|
|
|
md, _ := metadata.FromOutgoingContext(a.ctx)
|
2023-09-19 10:37:37 -04:00
|
|
|
md = metadata.Join(md, a.pickResult.Metadata)
|
2023-07-19 18:26:26 -04:00
|
|
|
a.ctx = metadata.NewOutgoingContext(a.ctx, md)
|
|
|
|
}
|
|
|
|
|
2022-03-24 08:21:41 -04:00
|
|
|
s, err := a.t.NewStream(a.ctx, cs.callHdr)
|
2019-04-12 14:36:17 -04:00
|
|
|
if err != nil {
|
2022-11-16 10:32:17 -05:00
|
|
|
nse, ok := err.(*transport.NewStreamError)
|
|
|
|
if !ok {
|
|
|
|
// Unexpected.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if nse.AllowTransparentRetry {
|
|
|
|
a.allowTransparentRetry = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap and convert error.
|
|
|
|
return toRPCErr(nse.Err)
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2022-11-16 10:32:17 -05:00
|
|
|
a.s = s
|
2023-11-01 11:03:44 -04:00
|
|
|
a.p = &parser{r: s, recvBufferPool: a.cs.cc.dopts.recvBufferPool}
|
2019-04-12 14:36:17 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
// clientStream implements a client side Stream.
|
|
|
|
type clientStream struct {
|
2019-04-12 14:36:17 -04:00
|
|
|
callHdr *transport.CallHdr
|
|
|
|
opts []CallOption
|
|
|
|
callInfo *callInfo
|
|
|
|
cc *ClientConn
|
|
|
|
desc *StreamDesc
|
2018-06-08 05:26:10 -04:00
|
|
|
|
|
|
|
codec baseCodec
|
|
|
|
cp Compressor
|
|
|
|
comp encoding.Compressor
|
|
|
|
|
|
|
|
cancel context.CancelFunc // cancels all attempts
|
|
|
|
|
2022-03-24 08:21:41 -04:00
|
|
|
sentLast bool // sent an end stream
|
2018-06-08 05:26:10 -04:00
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
methodConfig *MethodConfig
|
2018-06-08 05:26:10 -04:00
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
ctx context.Context // the application's context, wrapped by stats/tracing
|
|
|
|
|
|
|
|
retryThrottler *retryThrottler // The throttler active when the RPC began.
|
|
|
|
|
2023-03-14 20:24:52 -04:00
|
|
|
binlogs []binarylog.MethodLogger
|
2019-04-12 14:36:17 -04:00
|
|
|
// serverHeaderBinlogged is a boolean for whether server header has been
|
|
|
|
// logged. Server header will be logged when the first time one of those
|
|
|
|
// happens: stream.Header(), stream.Recv().
|
|
|
|
//
|
|
|
|
// It's only read and used by Recv() and Header(), so it doesn't need to be
|
|
|
|
// synchronized.
|
|
|
|
serverHeaderBinlogged bool
|
|
|
|
|
|
|
|
mu sync.Mutex
|
2019-10-02 13:42:55 -04:00
|
|
|
firstAttempt bool // if true, transparent retry is valid
|
|
|
|
numRetries int // exclusive of transparent retry attempt(s)
|
|
|
|
numRetriesSincePushback int // retries since pushback; to reset backoff
|
|
|
|
finished bool // TODO: replace with atomic cmpxchg or sync.Once?
|
|
|
|
// attempt is the active client stream attempt.
|
|
|
|
// The only place where it is written is the newAttemptLocked method and this method never writes nil.
|
|
|
|
// So, attempt can be nil only inside newClientStream function when clientStream is first created.
|
|
|
|
// One of the first things done after clientStream's creation, is to call newAttemptLocked which either
|
|
|
|
// assigns a non nil value to the attempt or returns an error. If an error is returned from newAttemptLocked,
|
|
|
|
// then newClientStream calls finish on the clientStream and returns. So, finish method is the only
|
|
|
|
// place where we need to check if the attempt is nil.
|
|
|
|
attempt *csAttempt
|
2018-06-08 05:26:10 -04:00
|
|
|
// TODO(hedging): hedging will have multiple attempts simultaneously.
|
2021-08-12 11:11:44 -04:00
|
|
|
committed bool // active attempt committed for retry?
|
|
|
|
onCommit func()
|
2019-04-12 14:36:17 -04:00
|
|
|
buffer []func(a *csAttempt) error // operations to replay on retry
|
|
|
|
bufferSize int // current size of buffer
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// csAttempt implements a single transport stream attempt within a
|
|
|
|
// clientStream.
|
|
|
|
type csAttempt struct {
|
2023-07-19 18:26:26 -04:00
|
|
|
ctx context.Context
|
|
|
|
cs *clientStream
|
|
|
|
t transport.ClientTransport
|
|
|
|
s *transport.Stream
|
|
|
|
p *parser
|
|
|
|
pickResult balancer.PickResult
|
2018-06-08 05:26:10 -04:00
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
finished bool
|
2018-06-08 05:26:10 -04:00
|
|
|
dc Decompressor
|
|
|
|
decomp encoding.Compressor
|
|
|
|
decompSet bool
|
|
|
|
|
|
|
|
mu sync.Mutex // guards trInfo.tr
|
2019-04-12 14:36:17 -04:00
|
|
|
// trInfo may be nil (if EnableTracing is false).
|
2018-06-08 05:26:10 -04:00
|
|
|
// trInfo.tr is set when created (if EnableTracing is true),
|
|
|
|
// and cleared when the finish method is called.
|
2019-04-12 14:36:17 -04:00
|
|
|
trInfo *traceInfo
|
2017-07-02 03:02:24 -04:00
|
|
|
|
2022-11-21 10:03:02 -05:00
|
|
|
statsHandlers []stats.Handler
|
|
|
|
beginTime time.Time
|
2022-11-16 10:32:17 -05:00
|
|
|
|
|
|
|
// set for newStream errors that may be transparently retried
|
|
|
|
allowTransparentRetry bool
|
|
|
|
// set for pick errors that are returned as a status
|
|
|
|
drop bool
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) commitAttemptLocked() {
|
2021-08-12 11:11:44 -04:00
|
|
|
if !cs.committed && cs.onCommit != nil {
|
|
|
|
cs.onCommit()
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
cs.committed = true
|
|
|
|
cs.buffer = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) commitAttempt() {
|
|
|
|
cs.mu.Lock()
|
|
|
|
cs.commitAttemptLocked()
|
|
|
|
cs.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// shouldRetry returns nil if the RPC should be retried; otherwise it returns
|
2022-03-24 08:21:41 -04:00
|
|
|
// the error that should be returned by the operation. If the RPC should be
|
|
|
|
// retried, the bool indicates whether it is being retried transparently.
|
2022-11-16 10:32:17 -05:00
|
|
|
func (a *csAttempt) shouldRetry(err error) (bool, error) {
|
|
|
|
cs := a.cs
|
2022-03-24 08:21:41 -04:00
|
|
|
|
2022-11-16 10:32:17 -05:00
|
|
|
if cs.finished || cs.committed || a.drop {
|
|
|
|
// RPC is finished or committed or was dropped by the picker; cannot retry.
|
2022-03-24 08:21:41 -04:00
|
|
|
return false, err
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2022-11-16 10:32:17 -05:00
|
|
|
if a.s == nil && a.allowTransparentRetry {
|
|
|
|
return true, nil
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
// Wait for the trailers.
|
2022-03-24 08:21:41 -04:00
|
|
|
unprocessed := false
|
2022-11-16 10:32:17 -05:00
|
|
|
if a.s != nil {
|
|
|
|
<-a.s.Done()
|
|
|
|
unprocessed = a.s.Unprocessed()
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2021-08-12 11:11:44 -04:00
|
|
|
if cs.firstAttempt && unprocessed {
|
2019-10-02 13:42:55 -04:00
|
|
|
// First attempt, stream unprocessed: transparently retry.
|
2022-03-24 08:21:41 -04:00
|
|
|
return true, nil
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
if cs.cc.dopts.disableRetry {
|
2022-03-24 08:21:41 -04:00
|
|
|
return false, err
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pushback := 0
|
|
|
|
hasPushback := false
|
2022-11-16 10:32:17 -05:00
|
|
|
if a.s != nil {
|
|
|
|
if !a.s.TrailersOnly() {
|
2022-03-24 08:21:41 -04:00
|
|
|
return false, err
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(retry): Move down if the spec changes to not check server pushback
|
|
|
|
// before considering this a failure for throttling.
|
2022-11-16 10:32:17 -05:00
|
|
|
sps := a.s.Trailer()["grpc-retry-pushback-ms"]
|
2019-04-12 14:36:17 -04:00
|
|
|
if len(sps) == 1 {
|
|
|
|
var e error
|
|
|
|
if pushback, e = strconv.Atoi(sps[0]); e != nil || pushback < 0 {
|
2021-08-12 11:11:44 -04:00
|
|
|
channelz.Infof(logger, cs.cc.channelzID, "Server retry pushback specified to abort (%q).", sps[0])
|
2019-04-12 14:36:17 -04:00
|
|
|
cs.retryThrottler.throttle() // This counts as a failure for throttling.
|
2022-03-24 08:21:41 -04:00
|
|
|
return false, err
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
hasPushback = true
|
|
|
|
} else if len(sps) > 1 {
|
2021-08-12 11:11:44 -04:00
|
|
|
channelz.Warningf(logger, cs.cc.channelzID, "Server retry pushback specified multiple values (%q); not retrying.", sps)
|
2019-04-12 14:36:17 -04:00
|
|
|
cs.retryThrottler.throttle() // This counts as a failure for throttling.
|
2022-03-24 08:21:41 -04:00
|
|
|
return false, err
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var code codes.Code
|
2022-11-16 10:32:17 -05:00
|
|
|
if a.s != nil {
|
|
|
|
code = a.s.Status().Code()
|
2019-04-12 14:36:17 -04:00
|
|
|
} else {
|
2022-11-16 10:32:17 -05:00
|
|
|
code = status.Code(err)
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
|
2021-08-12 11:11:44 -04:00
|
|
|
rp := cs.methodConfig.RetryPolicy
|
|
|
|
if rp == nil || !rp.RetryableStatusCodes[code] {
|
2022-03-24 08:21:41 -04:00
|
|
|
return false, err
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: the ordering here is important; we count this as a failure
|
|
|
|
// only if the code matched a retryable code.
|
|
|
|
if cs.retryThrottler.throttle() {
|
2022-03-24 08:21:41 -04:00
|
|
|
return false, err
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2021-08-12 11:11:44 -04:00
|
|
|
if cs.numRetries+1 >= rp.MaxAttempts {
|
2022-03-24 08:21:41 -04:00
|
|
|
return false, err
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var dur time.Duration
|
|
|
|
if hasPushback {
|
|
|
|
dur = time.Millisecond * time.Duration(pushback)
|
|
|
|
cs.numRetriesSincePushback = 0
|
|
|
|
} else {
|
2021-08-12 11:11:44 -04:00
|
|
|
fact := math.Pow(rp.BackoffMultiplier, float64(cs.numRetriesSincePushback))
|
|
|
|
cur := float64(rp.InitialBackoff) * fact
|
|
|
|
if max := float64(rp.MaxBackoff); cur > max {
|
2019-04-12 14:36:17 -04:00
|
|
|
cur = max
|
|
|
|
}
|
|
|
|
dur = time.Duration(grpcrand.Int63n(int64(cur)))
|
|
|
|
cs.numRetriesSincePushback++
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(dfawley): we could eagerly fail here if dur puts us past the
|
|
|
|
// deadline, but unsure if it is worth doing.
|
|
|
|
t := time.NewTimer(dur)
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
cs.numRetries++
|
2022-03-24 08:21:41 -04:00
|
|
|
return false, nil
|
2019-04-12 14:36:17 -04:00
|
|
|
case <-cs.ctx.Done():
|
|
|
|
t.Stop()
|
2022-03-24 08:21:41 -04:00
|
|
|
return false, status.FromContextError(cs.ctx.Err()).Err()
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns nil if a retry was performed and succeeded; error otherwise.
|
2022-11-16 10:32:17 -05:00
|
|
|
func (cs *clientStream) retryLocked(attempt *csAttempt, lastErr error) error {
|
2019-04-12 14:36:17 -04:00
|
|
|
for {
|
2022-11-16 10:32:17 -05:00
|
|
|
attempt.finish(toRPCErr(lastErr))
|
|
|
|
isTransparent, err := attempt.shouldRetry(lastErr)
|
2022-03-24 08:21:41 -04:00
|
|
|
if err != nil {
|
2019-04-12 14:36:17 -04:00
|
|
|
cs.commitAttemptLocked()
|
|
|
|
return err
|
|
|
|
}
|
2021-08-12 11:11:44 -04:00
|
|
|
cs.firstAttempt = false
|
2022-11-16 10:32:17 -05:00
|
|
|
attempt, err = cs.newAttemptLocked(isTransparent)
|
|
|
|
if err != nil {
|
|
|
|
// Only returns error if the clientconn is closed or the context of
|
|
|
|
// the stream is canceled.
|
2019-04-12 14:36:17 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-11-16 10:32:17 -05:00
|
|
|
// Note that the first op in the replay buffer always sets cs.attempt
|
|
|
|
// if it is able to pick a transport and create a stream.
|
|
|
|
if lastErr = cs.replayBufferLocked(attempt); lastErr == nil {
|
2019-04-12 14:36:17 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) Context() context.Context {
|
2019-04-12 14:36:17 -04:00
|
|
|
cs.commitAttempt()
|
|
|
|
// No need to lock before using attempt, since we know it is committed and
|
|
|
|
// cannot change.
|
2022-11-16 10:32:17 -05:00
|
|
|
if cs.attempt.s != nil {
|
|
|
|
return cs.attempt.s.Context()
|
|
|
|
}
|
|
|
|
return cs.ctx
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) withRetry(op func(a *csAttempt) error, onSuccess func()) error {
|
|
|
|
cs.mu.Lock()
|
|
|
|
for {
|
|
|
|
if cs.committed {
|
|
|
|
cs.mu.Unlock()
|
2022-03-24 08:21:41 -04:00
|
|
|
// toRPCErr is used in case the error from the attempt comes from
|
|
|
|
// NewClientStream, which intentionally doesn't return a status
|
|
|
|
// error to allow for further inspection; all other errors should
|
|
|
|
// already be status errors.
|
|
|
|
return toRPCErr(op(cs.attempt))
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(cs.buffer) == 0 {
|
|
|
|
// For the first op, which controls creation of the stream and
|
|
|
|
// assigns cs.attempt, we need to create a new attempt inline
|
|
|
|
// before executing the first op. On subsequent ops, the attempt
|
|
|
|
// is created immediately before replaying the ops.
|
|
|
|
var err error
|
|
|
|
if cs.attempt, err = cs.newAttemptLocked(false /* isTransparent */); err != nil {
|
|
|
|
cs.mu.Unlock()
|
|
|
|
cs.finish(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
a := cs.attempt
|
|
|
|
cs.mu.Unlock()
|
|
|
|
err := op(a)
|
|
|
|
cs.mu.Lock()
|
|
|
|
if a != cs.attempt {
|
|
|
|
// We started another attempt already.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
<-a.s.Done()
|
|
|
|
}
|
|
|
|
if err == nil || (err == io.EOF && a.s.Status().Code() == codes.OK) {
|
|
|
|
onSuccess()
|
|
|
|
cs.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
2022-11-16 10:32:17 -05:00
|
|
|
if err := cs.retryLocked(a, err); err != nil {
|
2019-04-12 14:36:17 -04:00
|
|
|
cs.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) Header() (metadata.MD, error) {
|
2019-04-12 14:36:17 -04:00
|
|
|
var m metadata.MD
|
|
|
|
err := cs.withRetry(func(a *csAttempt) error {
|
|
|
|
var err error
|
|
|
|
m, err = a.s.Header()
|
|
|
|
return toRPCErr(err)
|
|
|
|
}, cs.commitAttemptLocked)
|
2023-07-19 18:26:26 -04:00
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
if m == nil && err == nil {
|
|
|
|
// The stream ended with success. Finish the clientStream.
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
if err != nil {
|
2018-06-08 05:26:10 -04:00
|
|
|
cs.finish(err)
|
2023-11-01 11:03:44 -04:00
|
|
|
// Do not return the error. The user should get it by calling Recv().
|
|
|
|
return nil, nil
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2023-07-19 18:26:26 -04:00
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
if len(cs.binlogs) != 0 && !cs.serverHeaderBinlogged && m != nil {
|
2023-07-19 18:26:26 -04:00
|
|
|
// Only log if binary log is on and header has not been logged, and
|
|
|
|
// there is actually headers to log.
|
2019-04-12 14:36:17 -04:00
|
|
|
logEntry := &binarylog.ServerHeader{
|
|
|
|
OnClientSide: true,
|
|
|
|
Header: m,
|
|
|
|
PeerAddr: nil,
|
|
|
|
}
|
|
|
|
if peer, ok := peer.FromContext(cs.Context()); ok {
|
|
|
|
logEntry.PeerAddr = peer.Addr
|
|
|
|
}
|
|
|
|
cs.serverHeaderBinlogged = true
|
2023-03-14 20:24:52 -04:00
|
|
|
for _, binlog := range cs.binlogs {
|
2023-09-19 10:37:37 -04:00
|
|
|
binlog.Log(cs.ctx, logEntry)
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2023-11-01 11:03:44 -04:00
|
|
|
|
2022-11-16 10:32:17 -05:00
|
|
|
return m, nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) Trailer() metadata.MD {
|
2019-04-12 14:36:17 -04:00
|
|
|
// On RPC failure, we never need to retry, because usage requires that
|
|
|
|
// RecvMsg() returned a non-nil error before calling this function is valid.
|
|
|
|
// We would have retried earlier if necessary.
|
|
|
|
//
|
|
|
|
// Commit the attempt anyway, just in case users are not following those
|
|
|
|
// directions -- it will prevent races and should not meaningfully impact
|
|
|
|
// performance.
|
|
|
|
cs.commitAttempt()
|
|
|
|
if cs.attempt.s == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cs.attempt.s.Trailer()
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:32:17 -05:00
|
|
|
func (cs *clientStream) replayBufferLocked(attempt *csAttempt) error {
|
2019-04-12 14:36:17 -04:00
|
|
|
for _, f := range cs.buffer {
|
2022-11-16 10:32:17 -05:00
|
|
|
if err := f(attempt); err != nil {
|
2019-04-12 14:36:17 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) bufferForRetryLocked(sz int, op func(a *csAttempt) error) {
|
|
|
|
// Note: we still will buffer if retry is disabled (for transparent retries).
|
|
|
|
if cs.committed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cs.bufferSize += sz
|
|
|
|
if cs.bufferSize > cs.callInfo.maxRetryRPCBufferSize {
|
|
|
|
cs.commitAttemptLocked()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cs.buffer = append(cs.buffer, op)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
func (cs *clientStream) SendMsg(m any) (err error) {
|
2019-04-12 14:36:17 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
// Call finish on the client stream for errors generated by this SendMsg
|
|
|
|
// call, as these indicate problems created by this client. (Transport
|
|
|
|
// errors are converted to an io.EOF error in csAttempt.sendMsg; the real
|
|
|
|
// error will be returned from RecvMsg eventually in that case, or be
|
|
|
|
// retried.)
|
|
|
|
cs.finish(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if cs.sentLast {
|
|
|
|
return status.Errorf(codes.Internal, "SendMsg called after CloseSend")
|
|
|
|
}
|
|
|
|
if !cs.desc.ClientStreams {
|
|
|
|
cs.sentLast = true
|
|
|
|
}
|
2019-10-02 13:42:55 -04:00
|
|
|
|
|
|
|
// load hdr, payload, data
|
|
|
|
hdr, payload, data, err := prepareMsg(m, cs.codec, cs.cp, cs.comp)
|
2019-04-12 14:36:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-02 13:42:55 -04:00
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
// TODO(dfawley): should we be checking len(data) instead?
|
|
|
|
if len(payload) > *cs.callInfo.maxSendMessageSize {
|
|
|
|
return status.Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", len(payload), *cs.callInfo.maxSendMessageSize)
|
|
|
|
}
|
|
|
|
op := func(a *csAttempt) error {
|
2022-11-16 10:32:17 -05:00
|
|
|
return a.sendMsg(m, hdr, payload, data)
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
err = cs.withRetry(op, func() { cs.bufferForRetryLocked(len(hdr)+len(payload), op) })
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(cs.binlogs) != 0 && err == nil {
|
|
|
|
cm := &binarylog.ClientMessage{
|
2019-04-12 14:36:17 -04:00
|
|
|
OnClientSide: true,
|
2022-11-16 10:32:17 -05:00
|
|
|
Message: data,
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
|
|
|
for _, binlog := range cs.binlogs {
|
2023-09-19 10:37:37 -04:00
|
|
|
binlog.Log(cs.ctx, cm)
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2022-11-16 10:32:17 -05:00
|
|
|
return err
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
func (cs *clientStream) RecvMsg(m any) error {
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(cs.binlogs) != 0 && !cs.serverHeaderBinlogged {
|
2019-04-12 14:36:17 -04:00
|
|
|
// Call Header() to binary log header if it's not already logged.
|
|
|
|
cs.Header()
|
|
|
|
}
|
|
|
|
var recvInfo *payloadInfo
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(cs.binlogs) != 0 {
|
2019-04-12 14:36:17 -04:00
|
|
|
recvInfo = &payloadInfo{}
|
|
|
|
}
|
|
|
|
err := cs.withRetry(func(a *csAttempt) error {
|
|
|
|
return a.recvMsg(m, recvInfo)
|
|
|
|
}, cs.commitAttemptLocked)
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(cs.binlogs) != 0 && err == nil {
|
|
|
|
sm := &binarylog.ServerMessage{
|
2019-04-12 14:36:17 -04:00
|
|
|
OnClientSide: true,
|
|
|
|
Message: recvInfo.uncompressedBytes,
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
|
|
|
for _, binlog := range cs.binlogs {
|
2023-09-19 10:37:37 -04:00
|
|
|
binlog.Log(cs.ctx, sm)
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
if err != nil || !cs.desc.ServerStreams {
|
|
|
|
// err != nil or non-server-streaming indicates end of stream.
|
|
|
|
cs.finish(err)
|
|
|
|
}
|
|
|
|
return err
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) CloseSend() error {
|
2019-04-12 14:36:17 -04:00
|
|
|
if cs.sentLast {
|
|
|
|
// TODO: return an error and finish the stream instead, due to API misuse?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cs.sentLast = true
|
|
|
|
op := func(a *csAttempt) error {
|
|
|
|
a.t.Write(a.s, nil, nil, &transport.Options{Last: true})
|
|
|
|
// Always return nil; io.EOF is the only error that might make sense
|
|
|
|
// instead, but there is no need to signal the client to call RecvMsg
|
|
|
|
// as the only use left for the stream after CloseSend is to call
|
|
|
|
// RecvMsg. This also matches historical behavior.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cs.withRetry(op, func() { cs.bufferForRetryLocked(0, op) })
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(cs.binlogs) != 0 {
|
|
|
|
chc := &binarylog.ClientHalfClose{
|
2019-04-12 14:36:17 -04:00
|
|
|
OnClientSide: true,
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
|
|
|
for _, binlog := range cs.binlogs {
|
2023-09-19 10:37:37 -04:00
|
|
|
binlog.Log(cs.ctx, chc)
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
// We never returned an error here for reasons.
|
2018-06-08 05:26:10 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) finish(err error) {
|
|
|
|
if err == io.EOF {
|
|
|
|
// Ending a stream with EOF indicates a success.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
cs.mu.Lock()
|
|
|
|
if cs.finished {
|
2017-04-17 18:08:24 -04:00
|
|
|
cs.mu.Unlock()
|
2018-06-08 05:26:10 -04:00
|
|
|
return
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
cs.finished = true
|
2023-09-19 10:37:37 -04:00
|
|
|
for _, onFinish := range cs.callInfo.onFinish {
|
|
|
|
onFinish(err)
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
cs.commitAttemptLocked()
|
2021-08-12 11:11:44 -04:00
|
|
|
if cs.attempt != nil {
|
|
|
|
cs.attempt.finish(err)
|
|
|
|
// after functions all rely upon having a stream.
|
|
|
|
if cs.attempt.s != nil {
|
|
|
|
for _, o := range cs.opts {
|
|
|
|
o.after(cs.callInfo, cs.attempt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-01 11:03:44 -04:00
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
cs.mu.Unlock()
|
2023-11-01 11:03:44 -04:00
|
|
|
// Only one of cancel or trailer needs to be logged.
|
|
|
|
if len(cs.binlogs) != 0 {
|
|
|
|
switch err {
|
|
|
|
case errContextCanceled, errContextDeadline, ErrClientConnClosing:
|
|
|
|
c := &binarylog.Cancel{
|
|
|
|
OnClientSide: true,
|
|
|
|
}
|
|
|
|
for _, binlog := range cs.binlogs {
|
|
|
|
binlog.Log(cs.ctx, c)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
logEntry := &binarylog.ServerTrailer{
|
|
|
|
OnClientSide: true,
|
|
|
|
Trailer: cs.Trailer(),
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
if peer, ok := peer.FromContext(cs.Context()); ok {
|
|
|
|
logEntry.PeerAddr = peer.Addr
|
|
|
|
}
|
|
|
|
for _, binlog := range cs.binlogs {
|
|
|
|
binlog.Log(cs.ctx, logEntry)
|
|
|
|
}
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
cs.retryThrottler.successfulRPC()
|
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
if channelz.IsOn() {
|
|
|
|
if err != nil {
|
|
|
|
cs.cc.incrCallsFailed()
|
|
|
|
} else {
|
|
|
|
cs.cc.incrCallsSucceeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cs.cancel()
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
func (a *csAttempt) sendMsg(m any, hdr, payld, data []byte) error {
|
2018-06-08 05:26:10 -04:00
|
|
|
cs := a.cs
|
2019-04-12 14:36:17 -04:00
|
|
|
if a.trInfo != nil {
|
2018-06-08 05:26:10 -04:00
|
|
|
a.mu.Lock()
|
|
|
|
if a.trInfo.tr != nil {
|
|
|
|
a.trInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
|
|
|
|
}
|
|
|
|
a.mu.Unlock()
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
if err := a.t.Write(a.s, hdr, payld, &transport.Options{Last: !cs.desc.ClientStreams}); err != nil {
|
|
|
|
if !cs.desc.ClientStreams {
|
|
|
|
// For non-client-streaming RPCs, we return nil instead of EOF on error
|
|
|
|
// because the generated code requires it. finish is not called; RecvMsg()
|
|
|
|
// will call it with the stream's status independently.
|
|
|
|
return nil
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
return io.EOF
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
2022-11-21 10:03:02 -05:00
|
|
|
for _, sh := range a.statsHandlers {
|
|
|
|
sh.HandleRPC(a.ctx, outPayload(true, m, data, payld, time.Now()))
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
if channelz.IsOn() {
|
|
|
|
a.t.IncrMsgSent()
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
return nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
func (a *csAttempt) recvMsg(m any, payInfo *payloadInfo) (err error) {
|
2018-06-08 05:26:10 -04:00
|
|
|
cs := a.cs
|
2022-11-21 10:03:02 -05:00
|
|
|
if len(a.statsHandlers) != 0 && payInfo == nil {
|
2019-04-12 14:36:17 -04:00
|
|
|
payInfo = &payloadInfo{}
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
if !a.decompSet {
|
|
|
|
// Block until we receive headers containing received message encoding.
|
|
|
|
if ct := a.s.RecvCompress(); ct != "" && ct != encoding.Identity {
|
|
|
|
if a.dc == nil || a.dc.Type() != ct {
|
|
|
|
// No configured decompressor, or it does not match the incoming
|
|
|
|
// message encoding; attempt to find a registered compressor that does.
|
|
|
|
a.dc = nil
|
|
|
|
a.decomp = encoding.GetCompressor(ct)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
} else {
|
|
|
|
// No compression is used; disable our decompressor.
|
|
|
|
a.dc = nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
// Only initialize this state once per stream.
|
|
|
|
a.decompSet = true
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
err = recv(a.p, cs.codec, a.s, a.dc, m, *cs.callInfo.maxReceiveMessageSize, payInfo, a.decomp)
|
2018-06-08 05:26:10 -04:00
|
|
|
if err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
if err == io.EOF {
|
2018-06-08 05:26:10 -04:00
|
|
|
if statusErr := a.s.Status().Err(); statusErr != nil {
|
|
|
|
return statusErr
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
return io.EOF // indicates successful end of stream.
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2023-03-14 20:24:52 -04:00
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
return toRPCErr(err)
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
if a.trInfo != nil {
|
2018-06-08 05:26:10 -04:00
|
|
|
a.mu.Lock()
|
|
|
|
if a.trInfo.tr != nil {
|
|
|
|
a.trInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
a.mu.Unlock()
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2022-11-21 10:03:02 -05:00
|
|
|
for _, sh := range a.statsHandlers {
|
|
|
|
sh.HandleRPC(a.ctx, &stats.InPayload{
|
2019-04-12 14:36:17 -04:00
|
|
|
Client: true,
|
|
|
|
RecvTime: time.Now(),
|
|
|
|
Payload: m,
|
|
|
|
// TODO truncate large payload.
|
2023-09-19 10:37:37 -04:00
|
|
|
Data: payInfo.uncompressedBytes,
|
|
|
|
WireLength: payInfo.compressedLength + headerLen,
|
|
|
|
CompressedLength: payInfo.compressedLength,
|
|
|
|
Length: len(payInfo.uncompressedBytes),
|
2019-04-12 14:36:17 -04:00
|
|
|
})
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
if channelz.IsOn() {
|
|
|
|
a.t.IncrMsgRecv()
|
|
|
|
}
|
|
|
|
if cs.desc.ServerStreams {
|
|
|
|
// Subsequent messages should be received by subsequent RecvMsg calls.
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil
|
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
// Special handling for non-server-stream rpcs.
|
|
|
|
// This recv expects EOF or errors, so we don't collect inPayload.
|
2019-04-12 14:36:17 -04:00
|
|
|
err = recv(a.p, cs.codec, a.s, a.dc, m, *cs.callInfo.maxReceiveMessageSize, nil, a.decomp)
|
2018-06-08 05:26:10 -04:00
|
|
|
if err == nil {
|
|
|
|
return toRPCErr(errors.New("grpc: client streaming protocol violation: get <nil>, want <EOF>"))
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
if err == io.EOF {
|
|
|
|
return a.s.Status().Err() // non-server streaming Recv returns nil on success
|
|
|
|
}
|
|
|
|
return toRPCErr(err)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
func (a *csAttempt) finish(err error) {
|
|
|
|
a.mu.Lock()
|
2019-04-12 14:36:17 -04:00
|
|
|
if a.finished {
|
|
|
|
a.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.finished = true
|
|
|
|
if err == io.EOF {
|
|
|
|
// Ending a stream with EOF indicates a success.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
var tr metadata.MD
|
|
|
|
if a.s != nil {
|
|
|
|
a.t.CloseStream(a.s, err)
|
|
|
|
tr = a.s.Trailer()
|
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
|
2023-07-19 18:26:26 -04:00
|
|
|
if a.pickResult.Done != nil {
|
2019-04-12 14:36:17 -04:00
|
|
|
br := false
|
|
|
|
if a.s != nil {
|
|
|
|
br = a.s.BytesReceived()
|
|
|
|
}
|
2023-07-19 18:26:26 -04:00
|
|
|
a.pickResult.Done(balancer.DoneInfo{
|
2018-06-08 05:26:10 -04:00
|
|
|
Err: err,
|
2019-04-12 14:36:17 -04:00
|
|
|
Trailer: tr,
|
|
|
|
BytesSent: a.s != nil,
|
|
|
|
BytesReceived: br,
|
|
|
|
ServerLoad: balancerload.Parse(tr),
|
2017-07-02 03:02:24 -04:00
|
|
|
})
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2022-11-21 10:03:02 -05:00
|
|
|
for _, sh := range a.statsHandlers {
|
2017-07-02 03:02:24 -04:00
|
|
|
end := &stats.End{
|
2018-06-08 05:26:10 -04:00
|
|
|
Client: true,
|
2022-03-24 08:21:41 -04:00
|
|
|
BeginTime: a.beginTime,
|
2018-06-08 05:26:10 -04:00
|
|
|
EndTime: time.Now(),
|
2019-04-12 14:36:17 -04:00
|
|
|
Trailer: tr,
|
2018-06-08 05:26:10 -04:00
|
|
|
Error: err,
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
2022-11-21 10:03:02 -05:00
|
|
|
sh.HandleRPC(a.ctx, end)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
if a.trInfo != nil && a.trInfo.tr != nil {
|
2018-06-08 05:26:10 -04:00
|
|
|
if err == nil {
|
|
|
|
a.trInfo.tr.LazyPrintf("RPC: [OK]")
|
2017-04-17 18:08:24 -04:00
|
|
|
} else {
|
2018-06-08 05:26:10 -04:00
|
|
|
a.trInfo.tr.LazyPrintf("RPC: [%v]", err)
|
|
|
|
a.trInfo.tr.SetError()
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
a.trInfo.tr.Finish()
|
|
|
|
a.trInfo.tr = nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
a.mu.Unlock()
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2019-10-02 13:42:55 -04:00
|
|
|
// newClientStream creates a ClientStream with the specified transport, on the
|
|
|
|
// given addrConn.
|
|
|
|
//
|
|
|
|
// It's expected that the given transport is either the same one in addrConn, or
|
|
|
|
// is already closed. To avoid race, transport is specified separately, instead
|
|
|
|
// of using ac.transpot.
|
|
|
|
//
|
|
|
|
// Main difference between this and ClientConn.NewStream:
|
|
|
|
// - no retry
|
|
|
|
// - no service config (or wait for service config)
|
|
|
|
// - no tracing or stats
|
|
|
|
func newNonRetryClientStream(ctx context.Context, desc *StreamDesc, method string, t transport.ClientTransport, ac *addrConn, opts ...CallOption) (_ ClientStream, err error) {
|
2019-04-12 14:36:17 -04:00
|
|
|
if t == nil {
|
|
|
|
// TODO: return RPC error here?
|
|
|
|
return nil, errors.New("transport provided is nil")
|
|
|
|
}
|
|
|
|
// defaultCallInfo contains unnecessary info(i.e. failfast, maxRetryRPCBufferSize), so we just initialize an empty struct.
|
|
|
|
c := &callInfo{}
|
|
|
|
|
|
|
|
// Possible context leak:
|
|
|
|
// The cancel function for the child context we create will only be called
|
|
|
|
// when RecvMsg returns a non-nil error, if the ClientConn is closed, or if
|
|
|
|
// an error is generated by SendMsg.
|
|
|
|
// https://github.com/grpc/grpc-go/issues/1818.
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-10-02 13:42:55 -04:00
|
|
|
for _, o := range opts {
|
|
|
|
if err := o.before(c); err != nil {
|
|
|
|
return nil, toRPCErr(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.maxReceiveMessageSize = getMaxSize(nil, c.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
|
|
|
|
c.maxSendMessageSize = getMaxSize(nil, c.maxSendMessageSize, defaultServerMaxSendMessageSize)
|
2019-04-12 14:36:17 -04:00
|
|
|
if err := setCallInfoCodec(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
callHdr := &transport.CallHdr{
|
|
|
|
Host: ac.cc.authority,
|
|
|
|
Method: method,
|
|
|
|
ContentSubtype: c.contentSubtype,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set our outgoing compression according to the UseCompressor CallOption, if
|
|
|
|
// set. In that case, also find the compressor from the encoding package.
|
|
|
|
// Otherwise, use the compressor configured by the WithCompressor DialOption,
|
|
|
|
// if set.
|
|
|
|
var cp Compressor
|
|
|
|
var comp encoding.Compressor
|
|
|
|
if ct := c.compressorType; ct != "" {
|
|
|
|
callHdr.SendCompress = ct
|
|
|
|
if ct != encoding.Identity {
|
|
|
|
comp = encoding.GetCompressor(ct)
|
|
|
|
if comp == nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "grpc: Compressor is not installed for requested grpc-encoding %q", ct)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ac.cc.dopts.cp != nil {
|
|
|
|
callHdr.SendCompress = ac.cc.dopts.cp.Type()
|
|
|
|
cp = ac.cc.dopts.cp
|
|
|
|
}
|
|
|
|
if c.creds != nil {
|
|
|
|
callHdr.Creds = c.creds
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:42:55 -04:00
|
|
|
// Use a special addrConnStream to avoid retry.
|
2019-04-12 14:36:17 -04:00
|
|
|
as := &addrConnStream{
|
|
|
|
callHdr: callHdr,
|
|
|
|
ac: ac,
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
opts: opts,
|
|
|
|
callInfo: c,
|
|
|
|
desc: desc,
|
|
|
|
codec: c.codec,
|
|
|
|
cp: cp,
|
|
|
|
comp: comp,
|
|
|
|
t: t,
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := as.t.NewStream(as.ctx, as.callHdr)
|
|
|
|
if err != nil {
|
|
|
|
err = toRPCErr(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
as.s = s
|
2023-11-01 11:03:44 -04:00
|
|
|
as.p = &parser{r: s, recvBufferPool: ac.dopts.recvBufferPool}
|
2019-04-12 14:36:17 -04:00
|
|
|
ac.incrCallsStarted()
|
|
|
|
if desc != unaryStreamDesc {
|
2023-09-19 10:37:37 -04:00
|
|
|
// Listen on stream context to cleanup when the stream context is
|
|
|
|
// canceled. Also listen for the addrConn's context in case the
|
|
|
|
// addrConn is closed or reconnects to a different address. In all
|
|
|
|
// other cases, an error should already be injected into the recv
|
|
|
|
// buffer by the transport, which the client will eventually receive,
|
|
|
|
// and then we will cancel the stream's context in
|
|
|
|
// addrConnStream.finish.
|
2019-04-12 14:36:17 -04:00
|
|
|
go func() {
|
2023-09-19 10:37:37 -04:00
|
|
|
ac.mu.Lock()
|
|
|
|
acCtx := ac.ctx
|
|
|
|
ac.mu.Unlock()
|
2019-04-12 14:36:17 -04:00
|
|
|
select {
|
2023-09-19 10:37:37 -04:00
|
|
|
case <-acCtx.Done():
|
2019-04-12 14:36:17 -04:00
|
|
|
as.finish(status.Error(codes.Canceled, "grpc: the SubConn is closing"))
|
|
|
|
case <-ctx.Done():
|
|
|
|
as.finish(toRPCErr(ctx.Err()))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return as, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type addrConnStream struct {
|
|
|
|
s *transport.Stream
|
|
|
|
ac *addrConn
|
|
|
|
callHdr *transport.CallHdr
|
|
|
|
cancel context.CancelFunc
|
|
|
|
opts []CallOption
|
|
|
|
callInfo *callInfo
|
|
|
|
t transport.ClientTransport
|
|
|
|
ctx context.Context
|
|
|
|
sentLast bool
|
|
|
|
desc *StreamDesc
|
|
|
|
codec baseCodec
|
|
|
|
cp Compressor
|
|
|
|
comp encoding.Compressor
|
|
|
|
decompSet bool
|
|
|
|
dc Decompressor
|
|
|
|
decomp encoding.Compressor
|
|
|
|
p *parser
|
|
|
|
mu sync.Mutex
|
|
|
|
finished bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *addrConnStream) Header() (metadata.MD, error) {
|
|
|
|
m, err := as.s.Header()
|
|
|
|
if err != nil {
|
|
|
|
as.finish(toRPCErr(err))
|
|
|
|
}
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *addrConnStream) Trailer() metadata.MD {
|
|
|
|
return as.s.Trailer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *addrConnStream) CloseSend() error {
|
|
|
|
if as.sentLast {
|
|
|
|
// TODO: return an error and finish the stream instead, due to API misuse?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
as.sentLast = true
|
|
|
|
|
|
|
|
as.t.Write(as.s, nil, nil, &transport.Options{Last: true})
|
|
|
|
// Always return nil; io.EOF is the only error that might make sense
|
|
|
|
// instead, but there is no need to signal the client to call RecvMsg
|
|
|
|
// as the only use left for the stream after CloseSend is to call
|
|
|
|
// RecvMsg. This also matches historical behavior.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *addrConnStream) Context() context.Context {
|
|
|
|
return as.s.Context()
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
func (as *addrConnStream) SendMsg(m any) (err error) {
|
2019-04-12 14:36:17 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
// Call finish on the client stream for errors generated by this SendMsg
|
|
|
|
// call, as these indicate problems created by this client. (Transport
|
|
|
|
// errors are converted to an io.EOF error in csAttempt.sendMsg; the real
|
|
|
|
// error will be returned from RecvMsg eventually in that case, or be
|
|
|
|
// retried.)
|
|
|
|
as.finish(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if as.sentLast {
|
|
|
|
return status.Errorf(codes.Internal, "SendMsg called after CloseSend")
|
|
|
|
}
|
|
|
|
if !as.desc.ClientStreams {
|
|
|
|
as.sentLast = true
|
|
|
|
}
|
2019-10-02 13:42:55 -04:00
|
|
|
|
|
|
|
// load hdr, payload, data
|
|
|
|
hdr, payld, _, err := prepareMsg(m, as.codec, as.cp, as.comp)
|
2019-04-12 14:36:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-02 13:42:55 -04:00
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
// TODO(dfawley): should we be checking len(data) instead?
|
|
|
|
if len(payld) > *as.callInfo.maxSendMessageSize {
|
|
|
|
return status.Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", len(payld), *as.callInfo.maxSendMessageSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := as.t.Write(as.s, hdr, payld, &transport.Options{Last: !as.desc.ClientStreams}); err != nil {
|
|
|
|
if !as.desc.ClientStreams {
|
|
|
|
// For non-client-streaming RPCs, we return nil instead of EOF on error
|
|
|
|
// because the generated code requires it. finish is not called; RecvMsg()
|
|
|
|
// will call it with the stream's status independently.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
if channelz.IsOn() {
|
|
|
|
as.t.IncrMsgSent()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
func (as *addrConnStream) RecvMsg(m any) (err error) {
|
2019-04-12 14:36:17 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil || !as.desc.ServerStreams {
|
|
|
|
// err != nil or non-server-streaming indicates end of stream.
|
|
|
|
as.finish(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if !as.decompSet {
|
|
|
|
// Block until we receive headers containing received message encoding.
|
|
|
|
if ct := as.s.RecvCompress(); ct != "" && ct != encoding.Identity {
|
|
|
|
if as.dc == nil || as.dc.Type() != ct {
|
|
|
|
// No configured decompressor, or it does not match the incoming
|
|
|
|
// message encoding; attempt to find a registered compressor that does.
|
|
|
|
as.dc = nil
|
|
|
|
as.decomp = encoding.GetCompressor(ct)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No compression is used; disable our decompressor.
|
|
|
|
as.dc = nil
|
|
|
|
}
|
|
|
|
// Only initialize this state once per stream.
|
|
|
|
as.decompSet = true
|
|
|
|
}
|
|
|
|
err = recv(as.p, as.codec, as.s, as.dc, m, *as.callInfo.maxReceiveMessageSize, nil, as.decomp)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
if statusErr := as.s.Status().Err(); statusErr != nil {
|
|
|
|
return statusErr
|
|
|
|
}
|
|
|
|
return io.EOF // indicates successful end of stream.
|
|
|
|
}
|
|
|
|
return toRPCErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if channelz.IsOn() {
|
|
|
|
as.t.IncrMsgRecv()
|
|
|
|
}
|
|
|
|
if as.desc.ServerStreams {
|
|
|
|
// Subsequent messages should be received by subsequent RecvMsg calls.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special handling for non-server-stream rpcs.
|
|
|
|
// This recv expects EOF or errors, so we don't collect inPayload.
|
|
|
|
err = recv(as.p, as.codec, as.s, as.dc, m, *as.callInfo.maxReceiveMessageSize, nil, as.decomp)
|
|
|
|
if err == nil {
|
|
|
|
return toRPCErr(errors.New("grpc: client streaming protocol violation: get <nil>, want <EOF>"))
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
return as.s.Status().Err() // non-server streaming Recv returns nil on success
|
|
|
|
}
|
|
|
|
return toRPCErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *addrConnStream) finish(err error) {
|
|
|
|
as.mu.Lock()
|
|
|
|
if as.finished {
|
|
|
|
as.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
as.finished = true
|
|
|
|
if err == io.EOF {
|
|
|
|
// Ending a stream with EOF indicates a success.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if as.s != nil {
|
|
|
|
as.t.CloseStream(as.s, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
as.ac.incrCallsFailed()
|
|
|
|
} else {
|
|
|
|
as.ac.incrCallsSucceeded()
|
|
|
|
}
|
|
|
|
as.cancel()
|
|
|
|
as.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerStream defines the server-side behavior of a streaming RPC.
|
|
|
|
//
|
2022-11-16 10:32:17 -05:00
|
|
|
// Errors returned from ServerStream methods are compatible with the status
|
|
|
|
// package. However, the status code will often not match the RPC status as
|
|
|
|
// seen by the client application, and therefore, should not be relied upon for
|
|
|
|
// this purpose.
|
2017-04-17 18:08:24 -04:00
|
|
|
type ServerStream interface {
|
|
|
|
// SetHeader sets the header metadata. It may be called multiple times.
|
|
|
|
// When call multiple times, all the provided metadata will be merged.
|
|
|
|
// All the metadata will be sent out when one of the following happens:
|
|
|
|
// - ServerStream.SendHeader() is called;
|
|
|
|
// - The first response is sent out;
|
|
|
|
// - An RPC status is sent out (error or success).
|
|
|
|
SetHeader(metadata.MD) error
|
|
|
|
// SendHeader sends the header metadata.
|
|
|
|
// The provided md and headers set by SetHeader() will be sent.
|
|
|
|
// It fails if called multiple times.
|
|
|
|
SendHeader(metadata.MD) error
|
|
|
|
// SetTrailer sets the trailer metadata which will be sent with the RPC status.
|
|
|
|
// When called more than once, all the provided metadata will be merged.
|
|
|
|
SetTrailer(metadata.MD)
|
2019-04-12 14:36:17 -04:00
|
|
|
// Context returns the context for this stream.
|
|
|
|
Context() context.Context
|
|
|
|
// SendMsg sends a message. On error, SendMsg aborts the stream and the
|
|
|
|
// error is returned directly.
|
|
|
|
//
|
|
|
|
// SendMsg blocks until:
|
|
|
|
// - There is sufficient flow control to schedule m with the transport, or
|
|
|
|
// - The stream is done, or
|
|
|
|
// - The stream breaks.
|
|
|
|
//
|
|
|
|
// SendMsg does not wait until the message is received by the client. An
|
|
|
|
// untimely stream closure may result in lost messages.
|
|
|
|
//
|
|
|
|
// It is safe to have a goroutine calling SendMsg and another goroutine
|
|
|
|
// calling RecvMsg on the same stream at the same time, but it is not safe
|
|
|
|
// to call SendMsg on the same stream in different goroutines.
|
2023-07-19 18:26:26 -04:00
|
|
|
//
|
|
|
|
// It is not safe to modify the message after calling SendMsg. Tracing
|
|
|
|
// libraries and stats handlers may use the message lazily.
|
2023-11-01 11:03:44 -04:00
|
|
|
SendMsg(m any) error
|
2019-04-12 14:36:17 -04:00
|
|
|
// RecvMsg blocks until it receives a message into m or the stream is
|
|
|
|
// done. It returns io.EOF when the client has performed a CloseSend. On
|
|
|
|
// any non-EOF error, the stream is aborted and the error contains the
|
|
|
|
// RPC status.
|
|
|
|
//
|
|
|
|
// It is safe to have a goroutine calling SendMsg and another goroutine
|
|
|
|
// calling RecvMsg on the same stream at the same time, but it is not
|
|
|
|
// safe to call RecvMsg on the same stream in different goroutines.
|
2023-11-01 11:03:44 -04:00
|
|
|
RecvMsg(m any) error
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// serverStream implements a server side Stream.
|
|
|
|
type serverStream struct {
|
2018-06-08 05:26:10 -04:00
|
|
|
ctx context.Context
|
|
|
|
t transport.ServerTransport
|
|
|
|
s *transport.Stream
|
|
|
|
p *parser
|
|
|
|
codec baseCodec
|
|
|
|
|
|
|
|
cp Compressor
|
|
|
|
dc Decompressor
|
|
|
|
comp encoding.Compressor
|
|
|
|
decomp encoding.Compressor
|
|
|
|
|
2023-09-19 10:37:37 -04:00
|
|
|
sendCompressorName string
|
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
maxReceiveMessageSize int
|
|
|
|
maxSendMessageSize int
|
|
|
|
trInfo *traceInfo
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2022-11-21 10:03:02 -05:00
|
|
|
statsHandler []stats.Handler
|
2017-07-02 03:02:24 -04:00
|
|
|
|
2023-03-14 20:24:52 -04:00
|
|
|
binlogs []binarylog.MethodLogger
|
2019-04-12 14:36:17 -04:00
|
|
|
// serverHeaderBinlogged indicates whether server header has been logged. It
|
|
|
|
// will happen when one of the following two happens: stream.SendHeader(),
|
|
|
|
// stream.Send().
|
|
|
|
//
|
|
|
|
// It's only checked in send and sendHeader, doesn't need to be
|
|
|
|
// synchronized.
|
|
|
|
serverHeaderBinlogged bool
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
mu sync.Mutex // protects trInfo.tr after the service handler runs.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *serverStream) Context() context.Context {
|
2018-06-08 05:26:10 -04:00
|
|
|
return ss.ctx
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *serverStream) SetHeader(md metadata.MD) error {
|
|
|
|
if md.Len() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-16 10:32:17 -05:00
|
|
|
err := imetadata.Validate(md)
|
|
|
|
if err != nil {
|
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
return ss.s.SetHeader(md)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *serverStream) SendHeader(md metadata.MD) error {
|
2022-11-16 10:32:17 -05:00
|
|
|
err := imetadata.Validate(md)
|
|
|
|
if err != nil {
|
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ss.t.WriteHeader(ss.s, md)
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(ss.binlogs) != 0 && !ss.serverHeaderBinlogged {
|
2019-04-12 14:36:17 -04:00
|
|
|
h, _ := ss.s.Header()
|
2023-03-14 20:24:52 -04:00
|
|
|
sh := &binarylog.ServerHeader{
|
2019-04-12 14:36:17 -04:00
|
|
|
Header: h,
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
ss.serverHeaderBinlogged = true
|
2023-03-14 20:24:52 -04:00
|
|
|
for _, binlog := range ss.binlogs {
|
2023-09-19 10:37:37 -04:00
|
|
|
binlog.Log(ss.ctx, sh)
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
return err
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *serverStream) SetTrailer(md metadata.MD) {
|
|
|
|
if md.Len() == 0 {
|
|
|
|
return
|
|
|
|
}
|
2022-11-16 10:32:17 -05:00
|
|
|
if err := imetadata.Validate(md); err != nil {
|
|
|
|
logger.Errorf("stream: failed to validate md when setting trailer, err: %v", err)
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
ss.s.SetTrailer(md)
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
func (ss *serverStream) SendMsg(m any) (err error) {
|
2017-04-17 18:08:24 -04:00
|
|
|
defer func() {
|
|
|
|
if ss.trInfo != nil {
|
|
|
|
ss.mu.Lock()
|
|
|
|
if ss.trInfo.tr != nil {
|
|
|
|
if err == nil {
|
|
|
|
ss.trInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
|
|
|
|
} else {
|
2023-11-01 11:03:44 -04:00
|
|
|
ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []any{err}}, true)
|
2017-04-17 18:08:24 -04:00
|
|
|
ss.trInfo.tr.SetError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ss.mu.Unlock()
|
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
st, _ := status.FromError(toRPCErr(err))
|
|
|
|
ss.t.WriteStatus(ss.s, st)
|
2019-04-12 14:36:17 -04:00
|
|
|
// Non-user specified status was sent out. This should be an error
|
|
|
|
// case (as a server side Cancel maybe).
|
|
|
|
//
|
|
|
|
// This is not handled specifically now. User will return a final
|
|
|
|
// status from the service handler, we will log that error instead.
|
|
|
|
// This behavior is similar to an interceptor.
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
if channelz.IsOn() && err == nil {
|
|
|
|
ss.t.IncrMsgSent()
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
}()
|
2019-10-02 13:42:55 -04:00
|
|
|
|
2023-09-19 10:37:37 -04:00
|
|
|
// Server handler could have set new compressor by calling SetSendCompressor.
|
|
|
|
// In case it is set, we need to use it for compressing outbound message.
|
|
|
|
if sendCompressorsName := ss.s.SendCompress(); sendCompressorsName != ss.sendCompressorName {
|
|
|
|
ss.comp = encoding.GetCompressor(sendCompressorsName)
|
|
|
|
ss.sendCompressorName = sendCompressorsName
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:42:55 -04:00
|
|
|
// load hdr, payload, data
|
|
|
|
hdr, payload, data, err := prepareMsg(m, ss.codec, ss.cp, ss.comp)
|
2017-04-17 18:08:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-02 13:42:55 -04:00
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
// TODO(dfawley): should we be checking len(data) instead?
|
|
|
|
if len(payload) > ss.maxSendMessageSize {
|
|
|
|
return status.Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", len(payload), ss.maxSendMessageSize)
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
if err := ss.t.Write(ss.s, hdr, payload, &transport.Options{Last: false}); err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return toRPCErr(err)
|
|
|
|
}
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(ss.binlogs) != 0 {
|
2019-04-12 14:36:17 -04:00
|
|
|
if !ss.serverHeaderBinlogged {
|
|
|
|
h, _ := ss.s.Header()
|
2023-03-14 20:24:52 -04:00
|
|
|
sh := &binarylog.ServerHeader{
|
2019-04-12 14:36:17 -04:00
|
|
|
Header: h,
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
ss.serverHeaderBinlogged = true
|
2023-03-14 20:24:52 -04:00
|
|
|
for _, binlog := range ss.binlogs {
|
2023-09-19 10:37:37 -04:00
|
|
|
binlog.Log(ss.ctx, sh)
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2023-03-14 20:24:52 -04:00
|
|
|
sm := &binarylog.ServerMessage{
|
2019-04-12 14:36:17 -04:00
|
|
|
Message: data,
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
|
|
|
for _, binlog := range ss.binlogs {
|
2023-09-19 10:37:37 -04:00
|
|
|
binlog.Log(ss.ctx, sm)
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2022-11-21 10:03:02 -05:00
|
|
|
if len(ss.statsHandler) != 0 {
|
|
|
|
for _, sh := range ss.statsHandler {
|
|
|
|
sh.HandleRPC(ss.s.Context(), outPayload(false, m, data, payload, time.Now()))
|
|
|
|
}
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
func (ss *serverStream) RecvMsg(m any) (err error) {
|
2017-04-17 18:08:24 -04:00
|
|
|
defer func() {
|
|
|
|
if ss.trInfo != nil {
|
|
|
|
ss.mu.Lock()
|
|
|
|
if ss.trInfo.tr != nil {
|
|
|
|
if err == nil {
|
|
|
|
ss.trInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
|
|
|
|
} else if err != io.EOF {
|
2023-11-01 11:03:44 -04:00
|
|
|
ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []any{err}}, true)
|
2017-04-17 18:08:24 -04:00
|
|
|
ss.trInfo.tr.SetError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ss.mu.Unlock()
|
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
st, _ := status.FromError(toRPCErr(err))
|
|
|
|
ss.t.WriteStatus(ss.s, st)
|
2019-04-12 14:36:17 -04:00
|
|
|
// Non-user specified status was sent out. This should be an error
|
|
|
|
// case (as a server side Cancel maybe).
|
|
|
|
//
|
|
|
|
// This is not handled specifically now. User will return a final
|
|
|
|
// status from the service handler, we will log that error instead.
|
|
|
|
// This behavior is similar to an interceptor.
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
if channelz.IsOn() && err == nil {
|
|
|
|
ss.t.IncrMsgRecv()
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
}()
|
2019-04-12 14:36:17 -04:00
|
|
|
var payInfo *payloadInfo
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(ss.statsHandler) != 0 || len(ss.binlogs) != 0 {
|
2019-04-12 14:36:17 -04:00
|
|
|
payInfo = &payloadInfo{}
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
if err := recv(ss.p, ss.codec, ss.s, ss.dc, m, ss.maxReceiveMessageSize, payInfo, ss.decomp); err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
if err == io.EOF {
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(ss.binlogs) != 0 {
|
|
|
|
chc := &binarylog.ClientHalfClose{}
|
|
|
|
for _, binlog := range ss.binlogs {
|
2023-09-19 10:37:37 -04:00
|
|
|
binlog.Log(ss.ctx, chc)
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err == io.ErrUnexpectedEOF {
|
2018-06-08 05:26:10 -04:00
|
|
|
err = status.Errorf(codes.Internal, io.ErrUnexpectedEOF.Error())
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
return toRPCErr(err)
|
|
|
|
}
|
2022-11-21 10:03:02 -05:00
|
|
|
if len(ss.statsHandler) != 0 {
|
|
|
|
for _, sh := range ss.statsHandler {
|
|
|
|
sh.HandleRPC(ss.s.Context(), &stats.InPayload{
|
|
|
|
RecvTime: time.Now(),
|
|
|
|
Payload: m,
|
|
|
|
// TODO truncate large payload.
|
2023-09-19 10:37:37 -04:00
|
|
|
Data: payInfo.uncompressedBytes,
|
|
|
|
Length: len(payInfo.uncompressedBytes),
|
|
|
|
WireLength: payInfo.compressedLength + headerLen,
|
|
|
|
CompressedLength: payInfo.compressedLength,
|
2022-11-21 10:03:02 -05:00
|
|
|
})
|
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2023-03-14 20:24:52 -04:00
|
|
|
if len(ss.binlogs) != 0 {
|
|
|
|
cm := &binarylog.ClientMessage{
|
2019-04-12 14:36:17 -04:00
|
|
|
Message: payInfo.uncompressedBytes,
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
|
|
|
for _, binlog := range ss.binlogs {
|
2023-09-19 10:37:37 -04:00
|
|
|
binlog.Log(ss.ctx, cm)
|
2023-03-14 20:24:52 -04:00
|
|
|
}
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil
|
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
|
|
|
|
// MethodFromServerStream returns the method string for the input stream.
|
|
|
|
// The returned string is in the format of "/service/method".
|
|
|
|
func MethodFromServerStream(stream ServerStream) (string, bool) {
|
|
|
|
return Method(stream.Context())
|
|
|
|
}
|
2019-10-02 13:42:55 -04:00
|
|
|
|
|
|
|
// prepareMsg returns the hdr, payload and data
|
|
|
|
// using the compressors passed or using the
|
|
|
|
// passed preparedmsg
|
2023-11-01 11:03:44 -04:00
|
|
|
func prepareMsg(m any, codec baseCodec, cp Compressor, comp encoding.Compressor) (hdr, payload, data []byte, err error) {
|
2019-10-02 13:42:55 -04:00
|
|
|
if preparedMsg, ok := m.(*PreparedMsg); ok {
|
|
|
|
return preparedMsg.hdr, preparedMsg.payload, preparedMsg.encodedData, nil
|
|
|
|
}
|
|
|
|
// The input interface is not a prepared msg.
|
|
|
|
// Marshal and Compress the data at this point
|
|
|
|
data, err = encode(codec, m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
compData, err := compress(data, cp, comp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
hdr, payload = msgHeader(data, compData)
|
|
|
|
return hdr, payload, data, nil
|
|
|
|
}
|