Exemplo n.º 1
0
        private void AddGrpcChannel(string address, int port, AgentService agentService)
        {
            var newChannle = new Channel(address, port, ChannelCredentials.Insecure, new List <ChannelOption>());

            var newPair = new AgentServiceChannelPair
            {
                AgentService = agentService,
                Channel      = newChannle
            };

            ConnectedAgentServiceChannels.Add(newPair);
            _logger.Info($"添加新的service: {newPair.AgentService?.Service}:{newPair.AgentService?.ID}  {newPair.AgentService?.Address}:{newPair.AgentService?.Port}, ");
        }
        /// <summary>
        /// 判断channel状态,如果channel状态不正常则移除
        /// </summary>
        /// <param name="choosePair"></param>
        /// <returns></returns>
        internal bool CheckAndProcessChannelStatus(AgentServiceChannelPair choosePair)
        {
            //当channel相关的service shutdown之后,该状态一直会处于connecting的状态
            //如果此时采用的是random port,问题比较严重
            //所以,一旦服务检测到挂了之后,就直接清除该connection
            if (choosePair.Channel.State == ChannelState.Shutdown ||
                choosePair.Channel.State == ChannelState.TransientFailure ||
                choosePair.Channel.State == ChannelState.Connecting)
            {
                ConnectedAgentServiceChannels.Remove(choosePair);
                _logger.Error(
                    $"当前Channel异常,状态:{choosePair.Channel.State}  ServiceId:{choosePair.AgentService.ID} ,已经被移除");

                return(false);
            }

            return(true);
        }
        private void AddGrpcChannel(string address, int port, AgentService agentService)
        {
            var chanelOptions = new List <ChannelOption>
            {
                new ChannelOption("grpc.keepalive_time_ms", 1000 * 60 * 3),
                new ChannelOption("grpc.keepalive_timeout_ms", 1000 * 10),
                new ChannelOption("grpc.http2.min_time_between_pings_ms", 1000 * 10),
                new ChannelOption("grpc.http2.max_pings_without_data", 0),
                new ChannelOption("grpc.keepalive_permit_without_calls", 1)
            };
            var newChannle = new Channel(address, port, ChannelCredentials.Insecure, chanelOptions);

            var newPair = new AgentServiceChannelPair
            {
                AgentService = agentService,
                Channel      = newChannle
            };

            ConnectedAgentServiceChannels.Add(newPair);
            _logger.Info($"添加新的service: {newPair.AgentService?.Service}:{newPair.AgentService?.ID}  {newPair.AgentService?.Address}:{newPair.AgentService?.Port}, ");
        }