/// <summary>
 /// 拦截阻塞调用
 /// </summary>
 /// <param name="request"></param>
 /// <param name="context"></param>
 /// <param name="continuation"></param>
 /// <typeparam name="TRequest"></typeparam>
 /// <typeparam name="TResponse"></typeparam>
 /// <returns></returns>
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request,
                                                                   ClientInterceptorContext <TRequest, TResponse> context,
                                                                   BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     LogCall(context.Method);
     return(continuation(request, context));
 }
Ejemplo n.º 2
0
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(
     TRequest request,
     ClientInterceptorContext <TRequest, TResponse> context,
     BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     return(base.BlockingUnaryCall(request, context, continuation));
 }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(
            TRequest request,
            ClientInterceptorContext <TRequest, TResponse> context,
            BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            ClientRpcScope <TRequest, TResponse> rpcScope = null;

            try
            {
                rpcScope = new ClientRpcScope <TRequest, TResponse>(context, this.options);
                rpcScope.RecordRequest(request);
                var response = continuation(request, rpcScope.Context);
                rpcScope.RecordResponse(response);
                rpcScope.Complete();
                return(response);
            }
            catch (Exception e)
            {
                rpcScope?.CompleteWithException(e);
                throw;
            }
            finally
            {
                rpcScope?.RestoreParentActivity();
                rpcScope?.Dispose();
            }
        }
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context,
                                                                   BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     // Authorize any method if the user is logged in.
     addJwtHeaders(ref context);
     return(base.BlockingUnaryCall(request, context, continuation));
 }
Ejemplo n.º 5
0
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(
     TRequest request,
     ClientInterceptorContext <TRequest, TResponse> context,
     BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     this._logger.LogInformation("BlockingUnaryCall called");
     return(base.BlockingUnaryCall(request, context, continuation));
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Intercept and add headers to a BlockingUnaryCall.
        /// </summary>
        /// <param name="request">The request to intercept.</param>
        /// <param name="context">The client interceptor context to add headers to.</param>
        /// <param name="continuation">The continuation of the request after all headers have been added.</param>
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(
            TRequest request,
            ClientInterceptorContext <TRequest, TResponse> context,
            BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            AddCallerMetadata(ref context);

            return(continuation(request, context));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 拦截阻塞调用
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <param name="continuation"></param>
        /// <typeparam name="TRequest"></typeparam>
        /// <typeparam name="TResponse"></typeparam>
        /// <returns></returns>
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request,
                                                                          ClientInterceptorContext <TRequest, TResponse> context,
                                                                          BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            var clientId = context.Options.Headers?.GetClientId() ?? "";

            CheckRepeat(clientId, ServiceProvider.GetJsonProvider().Serializer(request), request.GetGenericTypeName());
            return(continuation(request, context));
        }
Ejemplo n.º 8
0
        private BlockingUnaryCallContinuation <TRequest, TResponse> getBlockingUnaryCallChain <TRequest, TResponse>(Interceptor interceptor, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
            where TRequest : class
            where TResponse : class
        {
            BlockingUnaryCallContinuation <TRequest, TResponse> fun = (fRequest, fContext) =>
            {
                return(interceptor.BlockingUnaryCall(fRequest, fContext, continuation));
            };

            return(fun);
        }
Ejemplo n.º 9
0
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest requestDto, ClientInterceptorContext <TRequest, TResponse> context,
                                                                          BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            var callInvoker = Config.Channel.CreateCallInvoker();
            var auc         = AsyncUnaryCall(requestDto, context, (req, ctx) => {
                context = CreateContext(requestDto, context);
                return(callInvoker.AsyncUnaryCall(context.Method, context.Host, context.Options, requestDto));
            });
            var ret = auc.GetAwaiter().GetResult();

            return(ret);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// BlockingUnaryCall
 /// </summary>
 /// <typeparam name="TRequest"></typeparam>
 /// <typeparam name="TResponse"></typeparam>
 /// <param name="request"></param>
 /// <param name="context"></param>
 /// <param name="continuation"></param>
 /// <returns></returns>
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(
     TRequest request
     , ClientInterceptorContext <TRequest, TResponse> context
     , BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     return(Call(context
                 , () =>
     {
         return continuation(request, context);
     }
                 , request) as TResponse);
 }
Ejemplo n.º 11
0
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context,
                                                                          BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            _logWriter.LogMessage($"REQUEST: {context.Method.FullName}");
            _logWriter.LogObject(request);
            var response = base.BlockingUnaryCall(request, context, continuation);

            //_logWriter.LogMessage(string.Empty);
            // _logWriter.LogMessage("RESPONSE:");
            // _logWriter.LogObject(response);
            return(response);
        }
Ejemplo n.º 12
0
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(
     TRequest request,
     ClientInterceptorContext <TRequest, TResponse> context,
     BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     try
     {
         return(base.BlockingUnaryCall(request, context, continuation));
     }
     catch (RpcException ex)
     {
         CallInterceptor.OnError(new ClientCallInterceptorContext(context.Options, context.Host, context.Method), ex);
         throw;
     }
 }
Ejemplo n.º 13
0
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(
     TRequest request,
     ClientInterceptorContext <TRequest, TResponse> context,
     BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     try
     {
         return(continuation(request, context));
     }
     catch (Exception exception)
     {
         _logger.LogError(exception, "Context: {@Context} failed.", context);
         throw new RpcException(new Status(StatusCode.Unknown, "Request failed.", exception));
     }
 }
Ejemplo n.º 14
0
    public override TResponse BlockingUnaryCall <TRequest, TResponse>(
        TRequest request, ClientInterceptorContext <TRequest, TResponse> context,
        BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
    {
        SecretKeyEncryption?ske = null;

        byte[]? key = null;

        if (request is ISessionSecuredMessage ssmRequ)
        {
            if (ske == null || key == null)
            {
                (ske, key) = ResolveSessionKey(context.Options.Headers);
            }

            try
            {
                ssmRequ.Encrypt(ske, key);
            }
            catch (Exception ex)
            {
                throw new RpcException(new Status(StatusCode.Internal,
                                                  "failed to encrypt session-secured input message: " + ex.Message));
            }
        }

        var response = base.BlockingUnaryCall(request, context, continuation);

        if (response is ISessionSecuredMessage ssmResp)
        {
            if (ske == null || key == null)
            {
                (ske, key) = ResolveSessionKey(context.Options.Headers);
            }

            try
            {
                ssmResp.Decrypt(ske, key);
            }
            catch (Exception ex)
            {
                throw new RpcException(new Status(StatusCode.Internal,
                                                  "failed to decrypt session-secured reply message: " + ex.Message));
            }
        }

        return(response);
    }
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context,
                                                                          BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            string calledMethod = context.Method.Name;

            Console.WriteLine($"{calledMethod}: {request}");
            TResponse response = null;

            try
            {
                response = base.BlockingUnaryCall(request, context, continuation);
            }
            catch (Exception e)
            {
                Console.WriteLine($"{calledMethod}: {e.Message}");
            }
            Console.WriteLine($"{calledMethod}: {response}");
            return(response);
        }
Ejemplo n.º 16
0
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request,
                                                                          ClientInterceptorContext <TRequest, TResponse> context,
                                                                          BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            long startTicks = timeSource.GetTimestampTicks();
            var  tid        = Thread.CurrentThread.ManagedThreadId;

            var response = continuation(request, context);

            long endTicks = timeSource.GetTimestampTicks();

            traceLogger.TraceEvent(
                context.Method.Name,
                EventType.Sync,
                request.GetType(),
                timeSource.GetDurationUs(startTicks, endTicks),
                timeSource.ConvertTicksToUs(startTicks),
                tid);

            return(response);
        }
Ejemplo n.º 17
0
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context,
                                                                          BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            var method     = context.Method.Name;
            var controller = context.Method.ServiceName;

            context.Options.Headers?.Add(GrpcSourceAppNameHeader, AppName);
            context.Options.Headers?.Add(GrpcSourceAppVersionHeader, AppVersion);
            context.Options.Headers?.Add(GrpcSourceAppHostHeader, AppHost);

            using (ClientGrpcCallProcessCount.WithLabels(HostName, controller, method).TrackInProgress())
            {
                using (ClientGrpcCallDelaySec.Labels(HostName, controller, method).NewTimer())
                {
                    ClientGrpcCallInCount.WithLabels(HostName, controller, method).Inc();

                    try
                    {
                        var resp = continuation(request, context);

                        ClientGrpcCallOutCount.WithLabels(HostName, controller, method, "success").Inc();

                        return(resp);
                    }
                    catch (Exception ex)
                    {
                        ClientGrpcCallOutCount.WithLabels(HostName, controller, method, "exception").Inc();
                        Activity.Current?.RecordException(ex);
                        if (request != null)
                        {
                            Activity.Current?.AddTag("grpc-request", JsonSerializer.Serialize(request));
                        }
                        throw;
                    }
                }
            }
        }
Ejemplo n.º 18
0
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            context = SetTraceIdHeader(context);
            var model = new MonitorModel
            {
                ClientIp       = context.Host,
                RequestUrl     = context.Method.FullName,
                RequestData    = request?.ToJson(),
                RequestHeaders = context.Options.Headers.ToDictionary(p => p.Key, p => p.Value),
                TraceId        = context.Options.Headers?.Where(p => p.Key == Consts.TraceId).FirstOrDefault()?.Value
            };

            try
            {
                var result = continuation(request, context);
                model.Status = "ok";
                return(result);
            }
            catch (Exception ex)
            {
                SetExceptionData(ex, model);
                model.Status    = "error";
                model.Exception = ex.GetFlatException();
                LoggerAccessor.Instance.OnLoggerError(ex, LogType.ClientLog);
                throw ex;
            }
            finally
            {
                model.ResponseTime = DateTime.Now;
                LoggerAccessor.Instance.OnLoggerMonitor(model.ToJson(), LogType.ClientLog);
            }
        }
Ejemplo n.º 19
0
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     return(continuation(request, GetNewContext(context)));
 }
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     SaveExecutionContextTo(EnsureHeaders(ref context));
     return(base.BlockingUnaryCall(request, context, continuation));
 }
Ejemplo n.º 21
0
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     return(new InterceptedClientHandler <TRequest, TResponse>(_tracer, context)
            .BlockingUnaryCall(request, continuation));
 }
Ejemplo n.º 22
0
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     try
     {
         return(base.BlockingUnaryCall(request, context, continuation));
     }
     catch (RpcException ex) when(ex is IRpcExceptionDetail)
     {
         throw RpcExceptionDecorator.Decorate(ex);
     }
 }
Ejemplo n.º 23
0
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     return(base.BlockingUnaryCall(request, AppendContextWithTokenMetadata(context), continuation));
 }
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            var response = continuation(request, ConfigureContext(context, out var cts));

            cts?.Dispose();
            return(response);
        }
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            var       span = BuildClientSpan <TRequest, TResponse>(context);
            TResponse res;

            try
            {
                res = base.BlockingUnaryCall(request, context, continuation);
            }
            finally
            {
                span.Finish();
            }
            //TODO: Errors?
            return(res);
        }
Ejemplo n.º 26
0
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            var info    = context.Method.FullName.Split('/');
            var service = info[1];
            var method  = info[2];

            OngoingRequests.Labels(service, method).Inc();

            using (RequestResponseHistogram.Labels(service, method).NewTimer())
            {
                try
                {
                    return(continuation(request, context));
                }
                catch (RpcException e)
                {
                    TotalErrors.Labels(service, method, e.StatusCode.ToString()).Inc();
                    throw;
                }
                finally
                {
                    OngoingRequests.Labels(service, method).Dec();
                }
            }
        }
Ejemplo n.º 27
0
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
 {
     try
     {
         return(base.BlockingUnaryCall(request, context, continuation));
     }
     catch (RpcException ex)
     {
         ///记录日志
         throw;
     }
 }
Ejemplo n.º 28
0
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            var watch    = Stopwatch.StartNew();
            var response = base.BlockingUnaryCall(request, context, continuation);

            watch.Stop();
            Console.WriteLine($"调用服务:{context.Method.FullName},发送消息{request.ToString()}次完成,耗时{watch.ElapsedMilliseconds}ms");
            return(response);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// 同步简单调用
        /// </summary>
        /// <typeparam name="TRequest"></typeparam>
        /// <typeparam name="TResponse"></typeparam>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <param name="continuation"></param>
        /// <returns></returns>
        public override TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
        {
            Console.WriteLine("客户端调用执行开始");
            var response = continuation(request, context);

            Console.WriteLine("客户端调用执行结束");
            return(response);
        }
Ejemplo n.º 30
0
 /// <summary>
 /// Intercepts a blocking invocation of a simple remote call.
 /// </summary>
 /// <param name="request">The request message of the invocation.</param>
 /// <param name="context">
 /// The <see cref="Grpc.Core.Interceptors.ClientInterceptorContext{TRequest, TResponse}"/>
 /// associated with the current invocation.
 /// </param>
 /// <param name="continuation">
 /// The callback that continues the invocation process.
 /// This can be invoked zero or more times by the interceptor.
 /// The interceptor can invoke the continuation passing the given
 /// request value and context arguments, or substitute them as it sees fit.
 /// </param>
 /// <returns>
 /// The response message of the current invocation.
 /// The interceptor can simply return the return value of the
 /// continuation delegate passed to it intact, or an arbitrary
 /// value as it sees fit.
 /// </returns>
 public virtual TResponse BlockingUnaryCall <TRequest, TResponse>(TRequest request, ClientInterceptorContext <TRequest, TResponse> context, BlockingUnaryCallContinuation <TRequest, TResponse> continuation)
     where TRequest : class
     where TResponse : class
 {
     return(continuation(request, context));
 }